From bd31d71349042d3cebc3b4276897e5d2827f8155 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Mon, 13 Jan 2014 19:02:09 -0800 Subject: [PATCH 1/5] panda --- Gemfile | 12 ++++----- Gemfile.lock | 43 +++++++++++++++++------------- api.rb | 24 ++++++++++------- spec/api_spec.rb | 68 +++++++++++++++++++++++++++--------------------- views/logs.rabl | 2 +- 5 files changed, 85 insertions(+), 64 deletions(-) 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..0530eae 100644 --- a/api.rb +++ b/api.rb @@ -3,36 +3,40 @@ 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 + def initialize(time, text, execution_time) @text = text @time = time - @created_at = Time.now + @execution_time = execution_time end @@log = [] - def self.log_request(time, text) - @@log << LogRequest.new(time, text) - end + def self.log_request(time, text, execution_time) + @@log << LogRequest.new(time, text, execution_time) + end def self.log @@log end def self.clear_log! - @@log = [] + @@log = [] end - end -LogRequest.log_request Time.now, "Just do it alreay" +LogRequest.log_request(Time.now, "Do it", "EXECUTE") + get '/' do @logs = LogRequest.log render :rabl, :logs, :format => "json" + end + + \ No newline at end of file diff --git a/spec/api_spec.rb b/spec/api_spec.rb index c51885f..4482e6c 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,63 @@ 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", "EXECUTE") +end + it "should return json array of log requests" do get "/" + puts last_response.body json = JSON.parse(last_response.body) log_request = json.first["logrequest"] log_request.fetch("text").should eq("Hello World") + log_request.fetch("execution_time").should eq("EXECUTE") + + time_in_utc = Time.parse(log_request.fetch("time")) time_in_utc.should be_within(1).of(6.seconds.ago.utc) + + end - it "not be ok with /wack" do - get "/wack" + it "not be oke with /wack" do + get "/wack" last_response.should_not be_ok 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", "EXECUTE")} 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" do + subject.execution_time.should eq("EXECUTE") + end + describe ":log" do #be able to log something + before do + LogRequest.clear_log! + LogRequest.log_request(Time.now, "Now", "EXECUTE") + LogRequest.log_request(Time.now, "Now", "EXECUTE") #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..f0dcd09 100644 --- a/views/logs.rabl +++ b/views/logs.rabl @@ -1,3 +1,3 @@ collection @logs -attributes :time, :text +attributes :time, :text, :execution_time From e93694ac9f9c53321f051f725e3af815327f567c Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Mon, 13 Jan 2014 20:04:53 -0800 Subject: [PATCH 2/5] panda, tiger --- api.rb | 12 ++++++++++-- spec/api_spec.rb | 31 +++++++++++++++++++------------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/api.rb b/api.rb index 0530eae..f4bd82d 100644 --- a/api.rb +++ b/api.rb @@ -30,7 +30,7 @@ def self.clear_log! end end -LogRequest.log_request(Time.now, "Do it", "EXECUTE") +LogRequest.log_request(Time.now, "Do it", Time.now) get '/' do @@ -39,4 +39,12 @@ def self.clear_log! end - \ No newline at end of file +post '/' do + LogRequest.log_request params.fetch("time"), params.fetch("text"), params.fetch("execution_time") + @logs = LogRequest.log + render :rabl, :logs, :format => "json" + #insert the record into the inner memory store + + + +end \ No newline at end of file diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 4482e6c..c4e1c3c 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -15,32 +15,39 @@ def app before do LogRequest.clear_log! - LogRequest.log_request(6.seconds.ago.utc, "Hello World", "EXECUTE") + LogRequest.log_request(6.seconds.ago.utc, "Hello World", 30.seconds.ago.utc) end it "should return json array of log requests" do get "/" - puts last_response.body json = JSON.parse(last_response.body) log_request = json.first["logrequest"] log_request.fetch("text").should eq("Hello World") - log_request.fetch("execution_time").should eq("EXECUTE") 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 oke 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}) + 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. - let(:subject) { LogRequest.new(45.minutes.ago, "Just Record It", "EXECUTE")} + let(:subject) { LogRequest.new(45.minutes.ago, "Just Record It", 30.minutes.ago)} it "should have the text" do subject.text.should eq("Just Record It") @@ -49,15 +56,15 @@ def app subject.time.should be_within(0.01).of(45.minutes.ago) end - it "should EXECUTE" do - subject.execution_time.should eq("EXECUTE") + 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", "EXECUTE") - LogRequest.log_request(Time.now, "Now", "EXECUTE") #be able to send in a period of time when called + 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) From 5c53d46c66726b0b1e3fcde660dee945fd5ecfae Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Mon, 13 Jan 2014 21:22:54 -0800 Subject: [PATCH 3/5] eagle --- api.rb | 31 +++++++++++++++++++++---------- spec/api_spec.rb | 7 ++----- views/logs.rabl | 2 +- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/api.rb b/api.rb index f4bd82d..03ab716 100644 --- a/api.rb +++ b/api.rb @@ -9,42 +9,53 @@ class LogRequest -attr_reader :text, :time, :execution_time - def initialize(time, text, execution_time) +attr_reader :text, :time, :execution_time, :user + def initialize(time, text, execution_time, user) @text = text @time = time @execution_time = execution_time + @user = user end @@log = [] - def self.log_request(time, text, execution_time) - @@log << LogRequest.new(time, text, execution_time) + def self.log_request(time, text, execution_time, user=nil) + if (user == nil) + @@log << LogRequest.new(time, text, execution_time, user=nil) + else + @@log << LogRequest.new(time, text, execution_time, user) + end end def self.log @@log end + def self.log_user(id) + @@log.select { |i| i.user == id } + end + def self.clear_log! @@log = [] end end -LogRequest.log_request(Time.now, "Do it", Time.now) +LogRequest.log_request(Time.now, "Do it", Time.now, '81910') get '/' do - @logs = LogRequest.log + 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") + LogRequest.log_request params.fetch("time"), params.fetch("text"), params.fetch("execution_time"), params.fetch("user") @logs = LogRequest.log render :rabl, :logs, :format => "json" #insert the record into the inner memory store - - - end \ No newline at end of file diff --git a/spec/api_spec.rb b/spec/api_spec.rb index c4e1c3c..fc91953 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -31,14 +31,11 @@ def app time_in_mars.should be_within(1).of(30.seconds.ago.utc) end - - - 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}) + 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) @@ -47,7 +44,7 @@ def app describe LogRequest do #variable that will be logged. - let(:subject) { LogRequest.new(45.minutes.ago, "Just Record It", 30.minutes.ago)} + 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") diff --git a/views/logs.rabl b/views/logs.rabl index f0dcd09..f55ee83 100644 --- a/views/logs.rabl +++ b/views/logs.rabl @@ -1,3 +1,3 @@ collection @logs -attributes :time, :text, :execution_time +attributes :time, :text, :execution_time, :user From 50835bfc777bd1dbca19dad6c3ffbcbeb4f7173d Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Tue, 14 Jan 2014 12:02:44 -0800 Subject: [PATCH 4/5] fixes --- api.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/api.rb b/api.rb index 03ab716..bf6ee0b 100644 --- a/api.rb +++ b/api.rb @@ -19,11 +19,7 @@ def initialize(time, text, execution_time, user) @@log = [] def self.log_request(time, text, execution_time, user=nil) - if (user == nil) - @@log << LogRequest.new(time, text, execution_time, user=nil) - else @@log << LogRequest.new(time, text, execution_time, user) - end end def self.log @@ -57,5 +53,10 @@ def self.clear_log! LogRequest.log_request params.fetch("time"), params.fetch("text"), params.fetch("execution_time"), params.fetch("user") @logs = LogRequest.log render :rabl, :logs, :format => "json" - #insert the record into the inner memory store -end \ No newline at end of file +end + +get '/hello/:user' do + LogRequest.log_request params.fetch("user") + @logs = LogRequest.log + render :rabl, :logs, :format => "json" +end From 3c0b7ac521a401d7c73ad36c5f05bfe779c5a602 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Tue, 14 Jan 2014 12:06:39 -0800 Subject: [PATCH 5/5] eagle --- api.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/api.rb b/api.rb index bf6ee0b..2c326ad 100644 --- a/api.rb +++ b/api.rb @@ -56,6 +56,7 @@ def self.clear_log! end get '/hello/:user' do + user = params[:user] LogRequest.log_request params.fetch("user") @logs = LogRequest.log render :rabl, :logs, :format => "json"