Skip to content

Commit

Permalink
Merge pull request #34 from ahx/add-other-params
Browse files Browse the repository at this point in the history
Add arbitrary query params
  • Loading branch information
doughsay authored Apr 11, 2019
2 parents 3683e3a + c66baf9 commit 8b0e189
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ people = store.from(:people).filter(name: "Alice").all
#=> [#<Lurch::Resource[Person] id: "2", name: "Alice">]
```

## Other query parameters

You can add arbitrary parameters as well. Note that your server should adhere to [JSON:API's query parameter constraints](https://jsonapi.org/format/#query-parameters).

```ruby
people = store.from(:people).params(someQuery: "blue").all
# => GET /people?someQuery=blue
```

## Relationships

Lurch can fetch *has-many* and *has-one* relationships from the server when they are provided as *related links*:
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* [x] Configurable pluralization/singularization (don't assume urls and types are always plural)
* [x] Handle paginated results
* [ ] Allow arbitrary headers
* [ ] Allow arbitrary query params
* [x] Allow arbitrary query params
* [ ] Singleton resources
* [ ] Handle links better?
* [x] >= 90% test coverage
Expand Down
14 changes: 12 additions & 2 deletions lib/lurch/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(store, inflector)
@include = []
@fields = Hash.new { [] }
@sort = []
@params = {}
@page = {}
end

Expand Down Expand Up @@ -38,6 +39,11 @@ def page(params)
self
end

def params(params)
@params.merge!(params)
self
end

def type(type)
@type = Inflector.decode_type(type)
self
Expand Down Expand Up @@ -98,13 +104,13 @@ def uri_builder

def to_query
QueryBuilder.new(
{
other_params.merge!(
filter: filter_query,
include: include_query,
fields: fields_query,
sort: sort_query,
page: page_query
}.merge(other_uri_params)
).merge!(other_uri_params)
).encode
end

Expand All @@ -113,6 +119,10 @@ def other_uri_params
{}
end

def other_params
@inflector.encode_keys(@params)
end

def filter_query
@inflector.encode_keys(@filter)
end
Expand Down
17 changes: 16 additions & 1 deletion test/lurch/test_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,23 @@ def test_page
assert_requested(stub)
end

def test_params
stub = stub_get(
"#{person_type}?baz[boom]=baum&foo=bar",
@response_factory.no_content_response
)

@store
.from(:people)
.params(foo: 'bar', baz: { boom: 'baum' })
.all

assert_requested(stub)
end

def test_all
stub = stub_get(
"#{person_type}?filter[name]=Alice&filter[foo][]=bar&filter[foo][]=baz&include=#{@inflector.encode_key(:phone_numbers)},friends,friends.#{@inflector.encode_key(:phone_numbers)}&fields[#{person_type}]=name&fields[#{phone_number_type}]=name,number&sort=name,-foo,bar&page[number]=12&page[size]=50",
"#{person_type}?filter[name]=Alice&filter[foo][]=bar&filter[foo][]=baz&include=#{@inflector.encode_key(:phone_numbers)},friends,friends.#{@inflector.encode_key(:phone_numbers)}&fields[#{person_type}]=name&fields[#{phone_number_type}]=name,number&boom=baum&sort=name,-foo,bar&page[number]=12&page[size]=50",
@response_factory.no_content_response
)

Expand All @@ -92,6 +106,7 @@ def test_all
.include(:phone_numbers, :friends, "friends.phone_numbers")
.fields([:name])
.fields(:phone_number, [:name, :number])
.params(boom: 'baum')
.sort(:name, {foo: :desc}, {bar: :asc})
.page(number: 12, size: 50)

Expand Down

0 comments on commit 8b0e189

Please sign in to comment.