Module: FulfilApi::Resource::Relation::QueryMethods
- Included in:
- FulfilApi::Resource::Relation
- Defined in:
- lib/fulfil_api/resource/relation/query_methods.rb
Overview
The QueryMethods extends the relation by
adding query methods to it.
Instance Method Summary collapse
-
#find_by(conditions) ⇒ FulfilApi::Resource?
Finds the first resource that matches the given conditions.
-
#limit(value) ⇒ FulfilApi::Resource::Relation
Limits the number of resources returned by Fulfil’s API.
-
#select(*fields) ⇒ FulfilApi::Resource::Relation
Specifies the fields to include in the response from Fulfil’s API.
-
#where(conditions) ⇒ FulfilApi::Resource::Relation
Adds filter conditions for querying Fulfil’s API.
Instance Method Details
#find_by(conditions) ⇒ FulfilApi::Resource?
Unlike the other methods in this module, ‘#find_by` will immediately trigger an HTTP request to retrieve the resource, rather than allowing for lazy evaluation.
Finds the first resource that matches the given conditions.
It constructs a query using the ‘where` method, limits the result to one record,
and then returns the first result.
20 21 22 |
# File 'lib/fulfil_api/resource/relation/query_methods.rb', line 20 def find_by(conditions) where(conditions).limit(1).first end |
#limit(value) ⇒ FulfilApi::Resource::Relation
If not specified, Fulfil’s API defaults to returning up to 500 resources per call.
Limits the number of resources returned by Fulfil’s API. This is useful when only
a specific number of resources are needed.
31 32 33 34 35 |
# File 'lib/fulfil_api/resource/relation/query_methods.rb', line 31 def limit(value) clone.tap do |relation| relation.request_limit = value end end |
#select(*fields) ⇒ FulfilApi::Resource::Relation
Specifies the fields to include in the response from Fulfil’s API. By default, only
the ID is returned.
Supports dot notation for nested data fields, though not all nested data may be available
depending on the API's limitations.
51 52 53 54 55 56 |
# File 'lib/fulfil_api/resource/relation/query_methods.rb', line 51 def select(*fields) clone.tap do |relation| relation.fields.concat(fields.map(&:to_s)) relation.fields.uniq! end end |
#where(conditions) ⇒ FulfilApi::Resource::Relation
Enhance the #where method to allow more natural and flexible queries.
Adds filter conditions for querying Fulfil’s API. Conditions should be formatted
as arrays according to the Fulfil API documentation.
68 69 70 71 72 73 |
# File 'lib/fulfil_api/resource/relation/query_methods.rb', line 68 def where(conditions) clone.tap do |relation| relation.conditions << conditions.flatten relation.conditions.uniq! end end |