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

Commit

Permalink
Added: search API refs #166
Browse files Browse the repository at this point in the history
  • Loading branch information
mallowlabs committed Feb 7, 2016
1 parent dc47de2 commit b1cf272
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/controllers/api/v1/message_controller.rb
Expand Up @@ -97,6 +97,19 @@ def destroy
when :error_on_destroy then render_error "destroy error"
end
end

def search
room_id = params[:room_id]
query = params[:query]

messages = {}
Room.with_room(room_id, current_user) do |room|
return render_error("room does not exist", 403) unless room

messages = Message.find_by_text(:text => query, :rooms => [ room ], :limit => 20)
end unless query.blank?
render :json => messages.map { |m| { :room => m[:room], :messages => m[:messages].map(&:to_hash) } }
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -17,6 +17,7 @@
namespace(:v1) do
get 'room/list', :controller => 'room', :action => 'list'
get 'message/list', :controller => 'message', :action => 'list'
get 'message/search', :controller => 'message', :action => 'search'
get 'user', :controller => 'user', :action => 'show'
post 'user', :controller => 'user', :action => 'update'
get 'user/add_device', :controller => 'user', :action => 'add_device'
Expand Down
24 changes: 24 additions & 0 deletions spec/controllers/api/v1/message_controller_spec.rb
Expand Up @@ -425,4 +425,28 @@
its(:response_code) { should == 403 }
end
end

describe "search" do
context 'normal' do
before {
get :search, :room_id => @room.id, :query => 'hoge', :format => 'json'
}
subject { response.body }
it { should have_json("//body[text() = 'hoge']") }
end
context 'empty query' do
before {
get :search, :room_id => @room.id, :format => 'json'
}
subject { response }
its(:response_code) { should == 200 }
end
context 'private room' do
before {
get :search, :room_id => @private_room.id, :query => 'hoge', :format => 'json'
}
subject { response.body }
it { should have_json("/status[text() = 'error']") }
end
end
end

0 comments on commit b1cf272

Please sign in to comment.