Skip to content

Commit

Permalink
predictable sorting of parameters when searching
Browse files Browse the repository at this point in the history
  • Loading branch information
benben committed May 18, 2012
1 parent 597986c commit 7ec075e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/helpers.rb
Expand Up @@ -92,11 +92,11 @@ def output options={}
output[:paging] = {}
params[:offset] = 0 if params[:offset].nil?
query_string = ""
request.env['rack.request.query_hash'].each { |k,v| query_string += "#{k}=#{v}&" unless k == 'offset' }
request.env['rack.request.query_hash'].each { |k,v| query_string += "#{k}=#{v}&" unless ['offset', 'limit', 'api_key'].include? k}

search = request.env['REQUEST_URI'].match(/[\w\/]+(\/search\/?\?)/) ? Regexp.last_match(1) : "?"

url = API_URL+params[:source]+'/'+params[:model]+search+query_string
url = "#{API_URL}#{params[:source]}/#{params[:model]}#{search}api_key=#{params[:api_key]}&#{query_string}limit=#{params[:limit].to_s}&"

#only next if limit+offset < count
ne = params[:offset] + params[:limit]
Expand Down
8 changes: 4 additions & 4 deletions test/pagination_test.rb
Expand Up @@ -88,30 +88,30 @@ def setup
should 'have the correct request url' do
get '/mediahandbook/companies', :limit => 5, :offset => 2
assert_status 200
assert_equal "#{API_URL}mediahandbook/companies?limit=5&api_key=#{@api_user.single_access_token}&offset=7", last_result['paging']['next']
assert_equal "#{API_URL}mediahandbook/companies?api_key=#{@api_user.single_access_token}&limit=5&offset=7", last_result['paging']['next']
end

should 'have the correct request url when searching' do
#trailing ? is a hack for testing
get '/mediahandbook/companies/search?', :q => 'a', :limit => 5, :offset => 2
assert_status 200
assert_equal "#{API_URL}mediahandbook/companies/search?q=a&limit=5&api_key=#{@api_user.single_access_token}&offset=7", last_result['paging']['next']
assert_equal "#{API_URL}mediahandbook/companies/search?api_key=#{@api_user.single_access_token}&q=a&limit=5&offset=7", last_result['paging']['next']
end
end

context 'The previous field' do
should 'have the correct request url' do
get '/mediahandbook/companies', :limit => 2, :offset => 5
assert_status 200
assert_equal "#{API_URL}mediahandbook/companies?limit=2&api_key=#{@api_user.single_access_token}&offset=3", last_result['paging']['previous']
assert_equal "#{API_URL}mediahandbook/companies?api_key=#{@api_user.single_access_token}&limit=2&offset=3", last_result['paging']['previous']
end

should 'have the correct request url when searching' do
#trailing ? is a hack for testing
get '/mediahandbook/companies/search?', :q => 'a', :limit => 2, :offset => 5
#assert_status 200
puts last_response.inspect
assert_equal "#{API_URL}mediahandbook/companies/search?q=a&limit=2&api_key=#{@api_user.single_access_token}&offset=3", last_result['paging']['previous']
assert_equal "#{API_URL}mediahandbook/companies/search?api_key=#{@api_user.single_access_token}&q=a&limit=2&offset=3", last_result['paging']['previous']
end
end
end

0 comments on commit 7ec075e

Please sign in to comment.