diff --git a/README.md b/README.md index da693b6..4d05ee0 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,54 @@ resource = CommentResource.new(connection: conn, scope: user) comments = resource.all #=> Will fetch from /users/123/comments ``` + +## Query Params + +If you would like to send a query along with your endpoint you can define the query keys like this: + +```ruby +class DropletResource < ResourceKit::Resource + resources do + action :all, 'GET /v2/droplets' do + query_keys :page, :per_page + handler(:ok) { |response| DropletMapping.extract_collection(response.body, :read) } + end + end +end +``` + +Then just pass the key values along with the method call: + +``` +resource.all(page: 2, per_page: 3) +``` + +If your resource uses non-ruby style keys, you can instead use a hash to map the non-ruby style key to +the ruby style: + +```ruby +class DropletResource < ResourceKit::Resource + resources do + action :all, 'GET /v2/droplets' do + query_keys :page, perPage: :per_page + handler(:ok) { |response| DropletMapping.extract_collection(response.body, :read) } + end + end +end +``` + +Then just pass the key values along with the method call: + +``` +resource.all(page: 2, per_page: 3) +``` + +And the params that will be sent will look like this: + +``` +/v2/droplets?page=2perPage=3 +``` + ## Test Helpers ResourceKit supplys test helpers that assist in certain things you'd want your resource classes to do. diff --git a/lib/resource_kit/endpoint_resolver.rb b/lib/resource_kit/endpoint_resolver.rb index 5c55822..2b13bff 100644 --- a/lib/resource_kit/endpoint_resolver.rb +++ b/lib/resource_kit/endpoint_resolver.rb @@ -37,7 +37,13 @@ def normalized_path_components(*components) def append_query_values(uri, values) pre_vals = uri.query_values || {} params = query_param_keys.each_with_object(pre_vals) do |key, query_values| - query_values[key] = values[key] if values.has_key?(key) + if key.class == Hash + key.each do |endpoint_key, key| + query_values[endpoint_key] = values[key] if values.has_key?(key) + end + else + query_values[key] = values[key] if values.has_key?(key) + end end URI.encode_www_form(params) diff --git a/resource_kit.gemspec b/resource_kit.gemspec index 10cebcc..34c2699 100644 --- a/resource_kit.gemspec +++ b/resource_kit.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.add_dependency 'addressable', '~> 2.3.6' + spec.add_dependency 'addressable', '~> 2.8' spec.add_development_dependency "bundler", "~> 1.6" spec.add_development_dependency "rake" diff --git a/spec/lib/resource_kit/action_spec.rb b/spec/lib/resource_kit/action_spec.rb index 63644fe..6aee4d7 100644 --- a/spec/lib/resource_kit/action_spec.rb +++ b/spec/lib/resource_kit/action_spec.rb @@ -66,6 +66,11 @@ action.query_keys :per_page, :page expect(action.query_keys).to include(:per_page, :page) end + + it 'allows setting known query parameters with non-ruby param mapped to ruby like keys' do + action.query_keys :page, { perPage: :per_page }, :id + expect(action.query_keys).to include(:page, {perPage: :per_page}, :id) + end end describe '#before_request' do @@ -84,4 +89,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/lib/resource_kit/endpoint_resolver_spec.rb b/spec/lib/resource_kit/endpoint_resolver_spec.rb index 27597ab..349ce0e 100644 --- a/spec/lib/resource_kit/endpoint_resolver_spec.rb +++ b/spec/lib/resource_kit/endpoint_resolver_spec.rb @@ -45,6 +45,17 @@ end end + context 'with query parameter mapped to different name' do + let(:query_param_keys) { [:page, { perPage: :per_page }] } + + it 'generates a URL with query parameters set correctly' do + endpoint = resolver.resolve(page: 3, per_page: 2) + + uri = Addressable::URI.parse(endpoint) + expect(uri.query_values).to eq("page" => '3', "perPage" => '2') + end + end + context 'with query parameters already appended' do let(:path) { '/:something/users?foo=bar' } let(:query_param_keys) { [:per_page, :page] } @@ -57,4 +68,4 @@ end end end -end \ No newline at end of file +end