diff --git a/api.rb b/api.rb index f439be6..172b3a7 100644 --- a/api.rb +++ b/api.rb @@ -4,35 +4,67 @@ require 'sinatra' require "active_support/all" +require "cgi" Rabl.register! class LogRequest - attr_reader :text, :time, :created_at - def initialize(time, text) + attr_reader :text, :time, :created_at, :execution_time, :user + + def initialize(time, text, exec_time, user) @text = text - @time = time + @time = time @created_at = Time.now + @execution_time = exec_time + @user = user end @@log = [] - def self.log_request(time, text) - @@log << LogRequest.new(time, text) + 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 alreay" +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["user"] + @logs = LogRequest.log_per_user(params.fetch("user")) + else + @logs = LogRequest.log + end render :rabl, :logs, :format => "json" end + +post '/' do + 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 c51885f..1de89f0 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -13,9 +13,10 @@ 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) + 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) @@ -23,9 +24,34 @@ 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) + exec_time = log_request.fetch("execution_time") + 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, user: "150" }) + json = JSON.parse(last_response.body) + 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 "not be ok with /wack" do + it "should not be ok with /wack" do get "/wack" last_response.should_not be_ok end @@ -34,7 +60,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, "811")} it "should have the text" do subject.text.should eq("Just Record it") @@ -42,12 +68,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" 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, "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) @@ -63,3 +92,4 @@ def app end end + diff --git a/views/logs.rabl b/views/logs.rabl index a9ac504..6ec4d47 100644 --- a/views/logs.rabl +++ b/views/logs.rabl @@ -1,3 +1,3 @@ collection @logs -attributes :time, :text +attributes :user, :time, :text, :execution_time, :user