Skip to content

Commit

Permalink
Merge pull request #16 from dstotz/add-only-method
Browse files Browse the repository at this point in the history
Add only method to return select fields
  • Loading branch information
jstotz committed Jun 12, 2018
2 parents c3b80ae + f27fb5e commit 284b5f6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ resource.sort(:description, :created_at).filter(:active => true).each do |item|
end
```

### Returning select fields
Resources have a `only` method that accepts any number of field keys to return only the selected fields. Note that each call to `only` overwrites any previous fields.

```ruby
resource.only(:id)
resource.only(:public_id, :updated_at)

# returns a new resource for chaining:
resource.only(:public_id, :updated_at).filter(:active => true).each do |item|
# ...
end
```

### Creating Resources

Create a new resource via POST:
Expand Down
13 changes: 13 additions & 0 deletions lib/springboard/client/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ def sort(*sorts)
query('sort' => sorts)
end

##
# Returns a new resource with the given fields added to the query string as _only parameters.
#
# @example
# resource.only('id', :public_id)
#
# @param [#to_s] returns One or more fields
#
# @return [Resource]
def only(*fields)
query('_only' => fields)
end

##
# Performs a request to get the first result of the first page of the
# collection and returns it.
Expand Down
10 changes: 10 additions & 0 deletions spec/springboard/client/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@
end
end

describe "only" do
it "should set the _only parameter based on the given values" do
expect(resource.only('f1', 'f2').uri.query).to eq('_only[]=f1&_only[]=f2')
end

it "should replace the existing _only parameters" do
expect(resource.only('f1').only('f2', 'f3').uri.query).to eq('_only[]=f2&_only[]=f3')
end
end

%w{count each each_page}.each do |method|
describe method do
it "should call the client's #{method} method with the resource's URI" do
Expand Down

0 comments on commit 284b5f6

Please sign in to comment.