Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
json (1.7.3)
multi_json (1.3.6)
rabl (0.6.13)
activesupport (4.0.2)
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.9)
json (1.8.1)
minitest (4.7.5)
multi_json (1.8.4)
rabl (0.9.3)
activesupport (>= 2.3.14)
multi_json (~> 1.0)
rack (1.4.1)
rack-protection (1.2.0)
rack (1.5.2)
rack-protection (1.5.2)
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)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
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.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.5)
sinatra (1.4.4)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
thread_safe (0.1.3)
atomic
tilt (1.4.1)
tzinfo (0.3.38)

PLATFORMS
ruby
Expand Down
44 changes: 38 additions & 6 deletions api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
Rabl.register!

class LogRequest
attr_reader :text, :time, :created_at
def initialize(time, text)
attr_reader :text, :time, :created_at, :execution_time, :user_code
def initialize(user_code, time, execution_time, text)
@text = text
@time = time
@created_at = Time.now
@execution_time = Time.now
@user_code = user_code
end

@@log = []
def self.log_request(time, text)
@@log << LogRequest.new(time, text)
def self.log_request(user_code, time, execution_time, text)
@@log << LogRequest.new(user_code, time, execution_time, text)
end

def self.log
Expand All @@ -27,12 +29,42 @@ def self.log
def self.clear_log!
@@log = []
end
end

class User
@@unique_codes = []

attr_reader :code
def initialize
@code = create_unique_code
@@unique_codes << @code
end

def create_unique_code
@@unique_codes.length + 1
end
end

LogRequest.log_request Time.now, "Just do it alreay"
# LogRequest.log_request Time.now, 2.hours.from_now, "Just do it alreay"

get '/' do
@logs = LogRequest.log
if params['user_code']
@logs = LogRequest.log.select { |log|
log if log.user_code == params['user_code'].to_i }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax is close, but not really there. a select block doesn't return the thing inside the block as a map does. Instead, it only keeps the things in the collection if select is true.

example: http://rubyfiddle.com/riddles/e0745

else
@logs = LogRequest.log
end
render :rabl, :logs, :format => "json"
end

post '/' do
if params.empty? or params['post'].nil? or params['post']['user_code'].nil?
status 401
erb :'401'
else
request = JSON.parse(params['post'])
LogRequest.log_request request['user_code'], request['time'], request['execution_time'], request['text']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably got a little old writing it out. Did you know you could do:

LogRequest.log_request request.values_at(*%w(user_code time execution_time text))

If that interests you, check out:

  1. http://rubyfiddle.com/riddles/755a2
  2. http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-values_at
  3. http://endofline.wordpress.com/2011/01/21/the-strange-ruby-splat/

@logs = LogRequest.log
render :rabl, :logs, :format => "json"
end
end
91 changes: 85 additions & 6 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require_relative "../api"
require "rspec"
require "rack/test"
require 'spec_helper'
require 'json'

set :environment, :test

Expand All @@ -11,18 +13,69 @@ def app
Sinatra::Application
end

let(:time) { Time.now }
let(:user) {double(code: 1)}
let(:user2) {double(code: 2)}
def log_request(response)
JSON.parse(last_response.body)
end


before do
LogRequest.clear_log!
LogRequest.log_request(6.seconds.ago.utc, "Hello World")
LogRequest.log_request(user.code, 6.seconds.ago.utc, time.utc, "Hello World")
LogRequest.log_request(user.code, 6.seconds.ago.utc, time.utc, "Hello World")
LogRequest.log_request(user2.code, 6.seconds.ago.utc, time.utc, "user 2")
end

it "should return json array of log request" do
get "/"
json = JSON.parse(last_response.body)
log_request = json.first["logrequest"]
# json = JSON.parse(last_response.body)
log_request = log_request(last_response).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)

execution_time = Time.parse(log_request.fetch("execution_time"))
execution_time.should be_within(1).of(time.utc)

new_user = log_request.fetch('user_code')
new_user.should eq(user.code)
end

it 'should return all the logs' do
get '/'
user_codes = log_request(last_response).map { |log| log['logrequest']['user_code'] }
user_codes.count.should eq(3)
end

it 'should return users logs' do
get '/', 'user_code' => user.code
user_codes = log_request(last_response).map { |log| log['logrequest']['user_code'] }
user_codes.should include(1)
user_codes.should_not include(2)
user_codes.count.should eq(2)
end

it 'should return user2 logs' do
get '/', 'user_code' => user2.code
user_codes = log_request(last_response).map { |log| log['logrequest']['user_code'] }
user_codes.should include(2)
user_codes.should_not include(1)
user_codes.count.should eq(1)
end

it 'should store the request in memory from a post' do
request = {'user_code' => "#{user.code}", 'text' => 'a post', 'time' => "#{Time.now}", 'execution_time' => "#{Time.now}" }.to_json
post "/", 'post' => request
log_request = log_request(last_response).last['logrequest']
log_request.fetch('text').should eq('a post')
end

it 'should always post with a user_code' do
post '/'
last_response.status.should eq(401)
end

it "not be ok with /wack" do
Expand All @@ -34,7 +87,10 @@ def app

describe LogRequest do

let(:subject) { LogRequest.new(45.minutes.ago, "Just Record it")}
let(:time) { Time.now }
let(:user) { double(code: 1) }
let(:subject) { LogRequest.new(user.code, 45.minutes.ago, time, "Just Record it")}


it "should have the text" do
subject.text.should eq("Just Record it")
Expand All @@ -43,11 +99,19 @@ def app
subject.time.should be_within(0.01).of(45.minutes.ago)
end

it 'should tell the execution time' do
subject.execution_time.should be_within(1).of(time)
end

it 'should tell the user code' do
subject.user_code.should eq(user.code)
end

describe ":log" do
before do
LogRequest.clear_log!
LogRequest.log_request(Time.now, "Now")
LogRequest.log_request(Time.now, "Now")
LogRequest.log_request(user.code, Time.now, Time.now, "Now")
LogRequest.log_request(user.code, Time.now, Time.now, "Now")
end
it "should be an array-like thing" do
LogRequest.log.count.should eq(2)
Expand All @@ -63,3 +127,18 @@ def app

end
end

describe User do

let(:user1) {User.new}
let(:user2) {User.new}

before do
LogRequest.clear_log!
end

it 'should have a unique code' do
user1.code.should eq(1)
user2.code.should eq(2)
end
end
2 changes: 1 addition & 1 deletion views/logs.rabl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
collection @logs

attributes :time, :text
attributes :user_code, :time, :text, :execution_time