From 5d4255ebabcdaaa172b300b1c26a6dd093aac0dd Mon Sep 17 00:00:00 2001 From: melissa Date: Sun, 19 May 2013 18:45:47 -0400 Subject: [PATCH 1/7] Add execution time attribute --- api.rb | 7 ++++--- spec/api_spec.rb | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/api.rb b/api.rb index f439be6..300787e 100644 --- a/api.rb +++ b/api.rb @@ -8,11 +8,12 @@ Rabl.register! class LogRequest - attr_reader :text, :time, :created_at + attr_reader :text, :time, :created_at, :execution_time def initialize(time, text) @text = text - @time = time + @time = time @created_at = Time.now + @execution_time = @created_at - @time end @@log = [] @@ -30,7 +31,7 @@ def self.clear_log! end -LogRequest.log_request Time.now, "Just do it alreay" +LogRequest.log_request Time.now, "Just do it already" get '/' do @logs = LogRequest.log diff --git a/spec/api_spec.rb b/spec/api_spec.rb index c51885f..97a4b21 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -42,6 +42,9 @@ def app it "should keep the time" do subject.time.should be_within(0.01).of(45.minutes.ago) end + it "should know how long it took to execute the request" do + subject.execution_time.should eq(0) + end describe ":log" do before do From f345bbe91a7f8fb3666f1e45bdb0f1c16d2fe3be Mon Sep 17 00:00:00 2001 From: melissa Date: Sun, 19 May 2013 23:32:04 -0400 Subject: [PATCH 2/7] Panda complete --- api.rb | 10 +++++----- spec/api_spec.rb | 15 +++++++++------ views/logs.rabl | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/api.rb b/api.rb index 300787e..5847bfc 100644 --- a/api.rb +++ b/api.rb @@ -9,16 +9,16 @@ class LogRequest attr_reader :text, :time, :created_at, :execution_time - def initialize(time, text) + def initialize(time, text, exec_time) @text = text @time = time @created_at = Time.now - @execution_time = @created_at - @time + @execution_time = exec_time end @@log = [] - def self.log_request(time, text) - @@log << LogRequest.new(time, text) + def self.log_request(time, text, exec_time) + @@log << LogRequest.new(time, text, exec_time) end def self.log @@ -31,7 +31,7 @@ def self.clear_log! end -LogRequest.log_request Time.now, "Just do it already" +LogRequest.log_request Time.now, "Just do it already", 5.minutes get '/' do @logs = LogRequest.log diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 97a4b21..b4a7b6d 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -13,7 +13,7 @@ def app before do LogRequest.clear_log! - LogRequest.log_request(6.seconds.ago.utc, "Hello World") + LogRequest.log_request(6.seconds.ago.utc, "Hello World", 11.hours) end it "should return json array of log request" do @@ -23,6 +23,9 @@ def app 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) + puts log_request + exec_time = log_request.fetch("execution_time") + exec_time.should be_within(1).of(11.hours) end it "not be ok with /wack" do @@ -34,7 +37,7 @@ def app describe LogRequest do - let(:subject) { LogRequest.new(45.minutes.ago, "Just Record it")} + let(:subject) { LogRequest.new(45.minutes.ago, "Just Record it", 5.seconds)} it "should have the text" do subject.text.should eq("Just Record it") @@ -42,15 +45,15 @@ def app it "should keep the time" do subject.time.should be_within(0.01).of(45.minutes.ago) end - it "should know how long it took to execute the request" do - subject.execution_time.should eq(0) + it "should know how long it took to execute" do + subject.execution_time.should be_within(0.01).of(5.seconds) end describe ":log" do before do LogRequest.clear_log! - LogRequest.log_request(Time.now, "Now") - LogRequest.log_request(Time.now, "Now") + LogRequest.log_request(Time.now, "Now", 3.minutes) + LogRequest.log_request(Time.now, "Now", 3.minutes) end it "should be an array-like thing" do LogRequest.log.count.should eq(2) 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 d69d5b5aaed8b582f2ba49a5abf9f3d47b2fc0e7 Mon Sep 17 00:00:00 2001 From: melissa Date: Sun, 26 May 2013 16:50:00 -0400 Subject: [PATCH 3/7] Tiger complete --- api.rb | 6 ++++++ spec/api_spec.rb | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/api.rb b/api.rb index 5847bfc..4740fe9 100644 --- a/api.rb +++ b/api.rb @@ -37,3 +37,9 @@ def self.clear_log! @logs = LogRequest.log render :rabl, :logs, :format => "json" end + +post '/' do + LogRequest.log_request params.fetch("time"), params.fetch("msg"), params.fetch("exec_time") + @logs = LogRequest.log + render :rabl, :logs, :format => "json" +end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index b4a7b6d..3020c4e 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -23,11 +23,20 @@ def app 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) - puts log_request exec_time = log_request.fetch("execution_time") exec_time.should be_within(1).of(11.hours) end + it "should be able to post a log" do + get "/" + json = JSON.parse(last_response.body) + count = json.count + post("/", { time: Time.now, msg: 'Posted request', exec_time: 3.minutes }) + json = JSON.parse(last_response.body) + total = json.count + total.should equal(count + 1) + end + it "not be ok with /wack" do get "/wack" last_response.should_not be_ok From ab74e338b207894f006f356f8fbc7fbf7a90714a Mon Sep 17 00:00:00 2001 From: Melissa Holmes Date: Mon, 3 Jun 2013 21:47:52 -0400 Subject: [PATCH 4/7] Add user specific logs for put and get requests --- api.rb | 36 +++++++++++++++++++++++++++++------- spec/api_spec.rb | 20 ++++++++++++++------ views/logs.rabl | 2 +- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/api.rb b/api.rb index 4740fe9..6440725 100644 --- a/api.rb +++ b/api.rb @@ -4,42 +4,64 @@ require 'sinatra' require "active_support/all" +require "cgi" Rabl.register! class LogRequest - attr_reader :text, :time, :created_at, :execution_time - def initialize(time, text, exec_time) + attr_reader :text, :time, :created_at, :execution_time, :user + + def initialize(time, text, exec_time, user) @text = text @time = time @created_at = Time.now @execution_time = exec_time + @user = user end @@log = [] - def self.log_request(time, text, exec_time) - @@log << LogRequest.new(time, text, exec_time) + def self.log_request(time, text, exec_time, user=nil) + if (user.nil?) + @@log << LogRequest.new(time, text, exec_time, User.None) + else + @@log << LogRequest.new(time, text, exec_time, user) + end end def self.log @@log end + def self.log_per_user(id) + @@log.select { |i| i.user == id } + end + def self.clear_log! @@log = [] end end -LogRequest.log_request Time.now, "Just do it already", 5.minutes +class User + def self.None + @id = -1 + end +end + +LogRequest.log_request(Time.now, "Just do it already", 5.minutes, "1011") get '/' do - @logs = LogRequest.log + if params != {} + puts "params: #{params}" + @logs = LogRequest.log_per_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("msg"), params.fetch("exec_time") + LogRequest.log_request params.fetch("time"), params.fetch("msg"), params.fetch("exec_time"), 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 3020c4e..9c8f88c 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -14,8 +14,9 @@ def app before do LogRequest.clear_log! LogRequest.log_request(6.seconds.ago.utc, "Hello World", 11.hours) + LogRequest.log_request(10.minutes.ago.utc, "User specific", 15.minutes, "951") end - + it "should return json array of log request" do get "/" json = JSON.parse(last_response.body) @@ -27,17 +28,23 @@ def app exec_time.should be_within(1).of(11.hours) end + it "should only return logs for a provided user id" do + get("/?user=951") + json = JSON.parse(last_response.body) + json.count.should eq(1) + end + it "should be able to post a log" do get "/" json = JSON.parse(last_response.body) count = json.count - post("/", { time: Time.now, msg: 'Posted request', exec_time: 3.minutes }) + post("/", { time: Time.now, msg: 'Posted request', exec_time: 3.minutes, user: "150" }) json = JSON.parse(last_response.body) total = json.count total.should equal(count + 1) end - it "not be ok with /wack" do + it "should not be ok with /wack" do get "/wack" last_response.should_not be_ok end @@ -46,7 +53,7 @@ def app describe LogRequest do - let(:subject) { LogRequest.new(45.minutes.ago, "Just Record it", 5.seconds)} + let(:subject) { LogRequest.new(45.minutes.ago, "Just Record it", 5.seconds, "811")} it "should have the text" do subject.text.should eq("Just Record it") @@ -61,8 +68,8 @@ def app describe ":log" do before do LogRequest.clear_log! - LogRequest.log_request(Time.now, "Now", 3.minutes) - LogRequest.log_request(Time.now, "Now", 3.minutes) + LogRequest.log_request(Time.now, "Now", 3.minutes, "500") + LogRequest.log_request(Time.now, "Now", 3.minutes, "500") end it "should be an array-like thing" do LogRequest.log.count.should eq(2) @@ -78,3 +85,4 @@ def app end end + diff --git a/views/logs.rabl b/views/logs.rabl index f0dcd09..6ec4d47 100644 --- a/views/logs.rabl +++ b/views/logs.rabl @@ -1,3 +1,3 @@ collection @logs -attributes :time, :text, :execution_time +attributes :user, :time, :text, :execution_time, :user From 251d6d4aa4c4c8f404fe7df3925f72cc9411891f Mon Sep 17 00:00:00 2001 From: Melissa Holmes Date: Mon, 3 Jun 2013 22:02:20 -0400 Subject: [PATCH 5/7] Remove puts --- api.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/api.rb b/api.rb index 6440725..70000e0 100644 --- a/api.rb +++ b/api.rb @@ -52,7 +52,6 @@ def self.None get '/' do if params != {} - puts "params: #{params}" @logs = LogRequest.log_per_user(params.fetch("user")) else @logs = LogRequest.log From 04943f489a4ff742767172bec0b809332a3b85d6 Mon Sep 17 00:00:00 2001 From: Melissa Holmes Date: Mon, 3 Jun 2013 23:07:18 -0400 Subject: [PATCH 6/7] Add 401 error to post request missing user information --- api.rb | 12 ++++++++---- spec/api_spec.rb | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/api.rb b/api.rb index 70000e0..b68612a 100644 --- a/api.rb +++ b/api.rb @@ -51,7 +51,7 @@ def self.None LogRequest.log_request(Time.now, "Just do it already", 5.minutes, "1011") get '/' do - if params != {} + if !params.fetch("user", nil).nil? @logs = LogRequest.log_per_user(params.fetch("user")) else @logs = LogRequest.log @@ -60,7 +60,11 @@ def self.None end post '/' do - LogRequest.log_request params.fetch("time"), params.fetch("msg"), params.fetch("exec_time"), params.fetch("user") - @logs = LogRequest.log - render :rabl, :logs, :format => "json" + if params.fetch("user", nil).nil? + halt 401, "Not authorized" + else + LogRequest.log_request params.fetch("time", ""), params.fetch("msg", ""), params.fetch("exec_time", ""), params.fetch("user", nil) + @logs = LogRequest.log + render :rabl, :logs, :format => "json" + end end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9c8f88c..1de89f0 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -43,6 +43,13 @@ def app total = json.count total.should equal(count + 1) end + + it "should return a 401 error when a put request does not contain a user id" do + post("/", { time: Time.now, msg: 'put request with user', exec_time: 35.seconds, user: "12" }) + last_response.should be_ok + post("/", { time: 48.seconds.ago, msg: 'put request without user', exec_time: 2.minutes }) + last_response.status.should eq(401) + end it "should not be ok with /wack" do get "/wack" From 136278cc79e55007ea1728a5c9b432afa0f84761 Mon Sep 17 00:00:00 2001 From: Melissa Holmes Date: Fri, 7 Jun 2013 20:05:18 -0400 Subject: [PATCH 7/7] Simplify get '/' cases --- api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api.rb b/api.rb index b68612a..172b3a7 100644 --- a/api.rb +++ b/api.rb @@ -51,7 +51,7 @@ def self.None LogRequest.log_request(Time.now, "Just do it already", 5.minutes, "1011") get '/' do - if !params.fetch("user", nil).nil? + if params["user"] @logs = LogRequest.log_per_user(params.fetch("user")) else @logs = LogRequest.log