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
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion lib/resource_kit/endpoint_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion resource_kit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion spec/lib/resource_kit/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,4 +89,4 @@
end
end
end
end
end
13 changes: 12 additions & 1 deletion spec/lib/resource_kit/endpoint_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
Expand All @@ -57,4 +68,4 @@
end
end
end
end
end