diff --git a/Gemfile b/Gemfile index 27ff65f..93224d6 100644 --- a/Gemfile +++ b/Gemfile @@ -2,11 +2,11 @@ source "https://rubygems.org" gem "json" gem "sinatra" -gem "activesupport", ">= 3.2.5" +gem "activesupport", ">=3.2.5" gem "rabl" gem "builder" - -group :test do - gem "rspec" - gem "rack-test" -end + + group :test do + gem "rspec" + gem "rack-test" + end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index a96293a..cf6a3f9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,35 +1,42 @@ GEM remote: https://rubygems.org/ specs: - activesupport (3.2.5) - i18n (~> 0.6) - multi_json (~> 1.0) - builder (3.0.0) - diff-lcs (1.1.3) - i18n (0.6.0) + activesupport (4.0.0) + i18n (~> 0.6, >= 0.6.4) + minitest (~> 4.2) + multi_json (~> 1.3) + thread_safe (~> 0.1) + tzinfo (~> 0.3.37) + atomic (1.1.14) + builder (3.2.2) + diff-lcs (1.2.5) + i18n (0.6.5) json (1.7.3) - multi_json (1.3.6) - rabl (0.6.13) + minitest (4.7.5) + multi_json (1.8.2) + rabl (0.9.3) activesupport (>= 2.3.14) - multi_json (~> 1.0) rack (1.4.1) rack-protection (1.2.0) rack - rack-test (0.6.1) + rack-test (0.6.2) rack (>= 1.0) - rspec (2.10.0) - rspec-core (~> 2.10.0) - rspec-expectations (~> 2.10.0) - rspec-mocks (~> 2.10.0) - rspec-core (2.10.1) - rspec-expectations (2.10.0) - diff-lcs (~> 1.1.3) - rspec-mocks (2.10.1) + rspec (2.14.1) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rspec-core (2.14.7) + rspec-expectations (2.14.4) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.4) sinatra (1.3.2) rack (~> 1.3, >= 1.3.6) rack-protection (~> 1.2) tilt (~> 1.3, >= 1.3.3) + thread_safe (0.1.3) + atomic tilt (1.3.3) + tzinfo (0.3.38) PLATFORMS ruby diff --git a/api.rb b/api.rb index f439be6..2c326ad 100644 --- a/api.rb +++ b/api.rb @@ -3,36 +3,61 @@ Bundler.require require 'sinatra' -require "active_support/all" +require 'active_support/all' Rabl.register! + class LogRequest - attr_reader :text, :time, :created_at - def initialize(time, text) +attr_reader :text, :time, :execution_time, :user + def initialize(time, text, execution_time, user) @text = text @time = time - @created_at = Time.now + @execution_time = execution_time + @user = user end @@log = [] - def self.log_request(time, text) - @@log << LogRequest.new(time, text) - end + def self.log_request(time, text, execution_time, user=nil) + @@log << LogRequest.new(time, text, execution_time, user) + end def self.log @@log end - def self.clear_log! - @@log = [] + def self.log_user(id) + @@log.select { |i| i.user == id } end + def self.clear_log! + @@log = [] + end end -LogRequest.log_request Time.now, "Just do it alreay" +LogRequest.log_request(Time.now, "Do it", Time.now, '81910') + get '/' do + if params != {} + puts "params: #{params}" + @logs = LogRequest.log_user(params.fetch("user")) + else + @logs = LogRequest.log + end + render :rabl, :logs, :format => "json" + +end + +post '/' do + LogRequest.log_request params.fetch("time"), params.fetch("text"), params.fetch("execution_time"), params.fetch("user") + @logs = LogRequest.log + render :rabl, :logs, :format => "json" +end + +get '/hello/:user' do + user = params[:user] + LogRequest.log_request params.fetch("user") @logs = LogRequest.log render :rabl, :logs, :format => "json" end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index c51885f..fc91953 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -1,7 +1,9 @@ require_relative "../api" require "rspec" +require "active_support/all" require "rack/test" + set :environment, :test describe "The Api" do @@ -11,55 +13,67 @@ def app Sinatra::Application end - before do - LogRequest.clear_log! - LogRequest.log_request(6.seconds.ago.utc, "Hello World") - end - - it "should return json array of log request" do +before do + LogRequest.clear_log! + LogRequest.log_request(6.seconds.ago.utc, "Hello World", 30.seconds.ago.utc) +end + it "should return json array of log requests" do get "/" json = JSON.parse(last_response.body) log_request = json.first["logrequest"] log_request.fetch("text").should eq("Hello World") + + time_in_utc = Time.parse(log_request.fetch("time")) time_in_utc.should be_within(1).of(6.seconds.ago.utc) + + time_in_mars = Time.parse(log_request.fetch("execution_time")) + time_in_mars.should be_within(1).of(30.seconds.ago.utc) end - it "not be ok with /wack" do - get "/wack" - last_response.should_not be_ok + it "should post a log" do + get "/" + json = JSON.parse(last_response.body) + count = json.count + post("/", {time: Time.now, text: 'awesome', execution_time: Time.now, user: '892' }) + json = JSON.parse(last_response.body) + new_count = json.count + new_count.should equal(count + 1) end end +describe LogRequest do #variable that will be logged. -describe LogRequest do - - let(:subject) { LogRequest.new(45.minutes.ago, "Just Record it")} + let(:subject) { LogRequest.new(45.minutes.ago, "Just Record It", 30.minutes.ago, "9220")} it "should have the text" do - subject.text.should eq("Just Record it") + subject.text.should eq("Just Record It") end it "should keep the time" do subject.time.should be_within(0.01).of(45.minutes.ago) end - describe ":log" do - before do - LogRequest.clear_log! - LogRequest.log_request(Time.now, "Now") - LogRequest.log_request(Time.now, "Now") - end - it "should be an array-like thing" do - LogRequest.log.count.should eq(2) - end - it "should request LogRequest" do - LogRequest.log.first.should be_a(LogRequest) - end - - it "can clear out the log" do - LogRequest.clear_log! - LogRequest.log.should be_empty - end + it "should execute the time" do + subject.execution_time.should be_within(0.01).of(30.minutes.ago) + end + describe ":log" do #be able to log something + before do + LogRequest.clear_log! + LogRequest.log_request(Time.now, "Now", Time.now) + LogRequest.log_request(Time.now, "Now", Time.now) #be able to send in a period of time when called + end + it "should be an array-like thing" do + LogRequest.log.count.should eq(2) end + it "should request LogRequest" do + LogRequest.log.first.should be_a(LogRequest) + end + + it "can clear out the log" do + LogRequest.clear_log! + LogRequest.log.should be_empty + end + + end end diff --git a/views/logs.rabl b/views/logs.rabl index a9ac504..f55ee83 100644 --- a/views/logs.rabl +++ b/views/logs.rabl @@ -1,3 +1,3 @@ collection @logs -attributes :time, :text +attributes :time, :text, :execution_time, :user