Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #162 from shimomura1004/id/161
Browse files Browse the repository at this point in the history
add newer_than/older_than parameter to list api refs #161
  • Loading branch information
mallowlabs committed Jan 8, 2014
2 parents 1106176 + ba6af0e commit dc9f24a
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ gem 'haml-rails'
gem 'sass-rails'
gem 'compass-rails'
gem "execjs"
gem 'therubyracer', '0.10.2', :platform => :ruby
gem 'therubyracer', :platform => :ruby
gem 'uglifier', '>= 1.0.3'

# util
Expand Down
10 changes: 6 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ GEM
multi_json (>= 1.5)
launchy (2.3.0)
addressable (~> 2.3)
libv8 (3.3.10.4)
libv8 (3.16.14.3)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
Expand Down Expand Up @@ -180,6 +180,7 @@ GEM
rcov (0.9.11)
rdoc (3.12.2)
json (~> 1.4)
ref (1.0.5)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.11.0)
Expand Down Expand Up @@ -230,8 +231,9 @@ GEM
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
therubyracer (0.10.2)
libv8 (~> 3.3.10)
therubyracer (0.12.0)
libv8 (~> 3.16.14.0)
ref
thin (1.3.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
Expand Down Expand Up @@ -294,7 +296,7 @@ DEPENDENCIES
socky-client (>= 0.5.0.beta1)
socky-server (>= 0.5.0.beta1)
spork
therubyracer (= 0.10.2)
therubyracer
thin
uglifier (>= 1.0.3)
uuidtools
Expand Down
44 changes: 27 additions & 17 deletions app/controllers/api/v1/message_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,37 @@ class MessageController < ApplicationController
respond_to :json

def list
room_id = params[:room_id]
order = if params[:order] == 'asc'
:asc
elsif params[:order] == 'desc'
:desc
else
nil
end
room_id = params[:room_id]
since_id = params[:since_id]
newer_than = params[:newer_than]
until_id = params[:until_id]
older_than = params[:older_than]

if (since_id and newer_than) or (until_id and older_than)
render_error("duplicated parameters are specified", 403) and return
end

Room.with_room(room_id, current_user) do |room|
if room.nil?
render_error "room does not exist", 403
render_error("room does not exist", 403) and return unless room

count = params[:count] ? params[:count].to_i : 20
order = case params[:order]
when 'asc' then :asc
when 'desc' then :desc
else nil
end
from_id = since_id || newer_than
to_id = until_id || older_than
from = {:id => from_id, :include_boundary => (not since_id.blank?)} if from_id
to = {:id => to_id, :include_boundary => (not until_id.blank?)} if to_id

if from or to
messages = room.messages_between(from, to, count, order)
else
count = params[:count] ? params[:count].to_i : 20
if params[:until_id] or params[:since_id]
@messages = room.messages_between(params[:since_id], params[:until_id], count, order)
else
@messages = room.messages(count, order)
end
respond_with(@messages.map{|m| cache([m, :api]){to_json(m)} })
messages = room.messages(count, order)
end

respond_with(messages.map{|m| cache([m, :api]){to_json(m)}})
end
end

Expand Down
23 changes: 15 additions & 8 deletions app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,31 @@ def owner_and_members
end
end

def messages(offset, order = nil)
def messages(count, order = nil)
rel = Message.where("room_id" => id)
if order == nil or order == :desc
rel = rel.order_by(:_id.desc)
else
rel = rel.order_by(:_id.asc)
end
ret = rel.limit(offset).to_a
ret = rel.limit(count).to_a
ret.reverse! if order == nil
ret
end

def messages_between(from_id, to_id, count, order = nil)
where = {:room_id => self.id}
where[:_id.gte] = from_id if from_id
where[:_id.lte] = to_id if to_id
rel = Message.where(where)
only_to_id = ((not from_id) and to_id)
def messages_between(from, to, count, order = nil)
conditions = {:room_id => self.id}
if from
conditions[:_id.gte] = from[:id] if from[:include_boundary]
conditions[:_id.gt] = from[:id] unless from[:include_boundary]
end
if to
conditions[:_id.lte] = to[:id] if to[:include_boundary]
conditions[:_id.lt] = to[:id] unless to[:include_boundary]
end

rel = Message.where(conditions)
only_to_id = ((not from) and to)
if order == :desc or only_to_id
rel = rel.order_by(:_id.desc)
else
Expand Down
137 changes: 109 additions & 28 deletions spec/controllers/api/v1/message_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,72 +77,153 @@
it { should have_json("/id[text() = '#{@messages[-20].id}']") }
it { should_not have_json("/id[text() = '#{@messages[-21].id}']") }
end
describe "messages" do
subject { assigns[:messages] }
it { should have(20).items }
end
end

describe "since_id と until_id を指定" do
before {
get :list, :room_id => @room.id, :since_id => @messages[24].id, :until_id => @messages[26].id , :count => 2, :format => 'json'
}
describe "response" do
subject { response.body }
it { should have_json("/id[text() = '#{@messages[24].id}']") }
it { should have_json("/id[text() = '#{@messages[25].id}']") }
it { should_not have_json("/id[text() = '#{@messages[26].id}']") }
end
end

subject { assigns[:messages] }
it { should be_include(@messages[24]) }
it { should be_include(@messages[25]) }
it { should_not be_include(@messages[26]) }
describe "newer_than と older_than を指定" do
before {
get :list, :room_id => @room.id, :newer_than => @messages[24].id, :older_than => @messages[27].id , :count => 4, :format => 'json'
}
describe "response" do
subject { response.body }
it { should_not have_json("/id[text() = '#{@messages[24].id}']") }
it { should have_json("/id[text() = '#{@messages[25].id}']") }
it { should have_json("/id[text() = '#{@messages[26].id}']") }
it { should_not have_json("/id[text() = '#{@messages[27].id}']") }
end
end

describe "until_idを指定" do
before {
get :list, :room_id => @room.id, :until_id => @messages[25].id , :format => 'json'
}
describe "response" do
subject { response.body }
it { should have_json("/id[text() = '#{@messages[24].id}']") }
it { should have_json("/id[text() = '#{@messages[25].id}']") }
it { should_not have_json("/id[text() = '#{@messages[26].id}']") }
end
end

subject { assigns[:messages] }
it { should be_include(@messages[24]) }
it { should be_include(@messages[25]) }
it { should_not be_include(@messages[26]) }
describe "older_than を指定" do
before {
get :list, :room_id => @room.id, :older_than => @messages[25].id , :format => 'json'
}
describe "response" do
subject { response.body }
it { should have_json("/id[text() = '#{@messages[23].id}']") }
it { should have_json("/id[text() = '#{@messages[24].id}']") }
it { should_not have_json("/id[text() = '#{@messages[25].id}']") }
end
end

describe "since_idを指定" do
before {
get :list, :room_id => @room.id, :since_id => @messages[25].id , :format => 'json'
}

subject { assigns[:messages] }
it { should_not be_include(@messages[24]) }
it { should be_include(@messages[25]) }
it { should be_include(@messages[26]) }
describe "response" do
subject { response.body }
it { should_not have_json("/id[text() = '#{@messages[24].id}']") }
it { should have_json("/id[text() = '#{@messages[25].id}']") }
it { should have_json("/id[text() = '#{@messages[26].id}']") }
end
end

describe "order を指定" do
describe "newer_than を指定" do
before {
get :list, :room_id => @room.id, :since_id => @messages[30].id, :count => 10, :order => 'desc', :format => 'json'
get :list, :room_id => @room.id, :newer_than => @messages[25].id , :format => 'json'
}
describe "response" do
subject { response.body }
it { should_not have_json("/id[text() = '#{@messages[25].id}']") }
it { should have_json("/id[text() = '#{@messages[26].id}']") }
it { should have_json("/id[text() = '#{@messages[27].id}']") }
end
end

describe "パラメータ指定が無効" do
describe "newer_than と since_id を指定" do
before {
get :list, :room_id => @room.id, :newer_than => @messages[25].id, :since_id => @messages[25].id, :format => 'json'
}
describe "response" do
subject { response.body }
it { should have_json("/status[text() = 'error']") }
end
end
describe "older_than と until_id を指定" do
before {
get :list, :room_id => @room.id, :older_than => @messages[25].id, :until_id => @messages[25].id, :format => 'json'
}
describe "response" do
subject { response.body }
it { should have_json("/status[text() = 'error']") }
end
end
end

subject { assigns[:messages] }
it { should have(10).items }
it { should_not be_include(@messages[30]) }
it { should be_include(@messages[41]) }
it { should be_include(@messages[50]) }
describe "order を指定" do
describe "order に asc を指定" do
before {
get :list, :room_id => @room.id, :since_id => @messages[30].id, :count => 10, :order => 'asc', :format => 'json'
}
describe "response" do
subject { response.body }
it { should have_json("/id[text() = '#{@messages[30].id}']") }
it { should_not have_json("/id[text() = '#{@messages[40].id}']") }
end
describe "response length" do
subject { JSON.parse(response.body) }
it { should have(10).items }
end
end
describe "order に desc を指定" do
before {
get :list, :room_id => @room.id, :since_id => @messages[30].id, :count => 10, :order => 'desc', :format => 'json'
}
describe "response" do
subject { response.body }
it { should_not have_json("/id[text() = '#{@messages[30].id}']") }
it { should have_json("/id[text() = '#{@messages[41].id}']") }
it { should have_json("/id[text() = '#{@messages[50].id}']") }
end
describe "response length" do
subject { JSON.parse(response.body) }
it { should have(10).items }
end
end
end


describe "countを指定" do
before {
get :list, :room_id => @room.id, :count => 1 , :format => 'json'
}
subject { assigns[:messages] }
it { should have(1).items }
describe "response" do
subject { JSON.parse(response.body) }
it { should have(1).items }
end
end

describe "nickname" do
before {
get :list, :room_id => @room.nickname, :format => 'json'
}
subject { assigns[:messages] }
it { should have(20).items }
describe "response" do
subject { JSON.parse(response.body) }
it { should have(20).items }
end
end
end

Expand Down
Loading

0 comments on commit dc9f24a

Please sign in to comment.