Class: OpenFdaApi::QueryBuilder
- Inherits:
-
Object
- Object
- OpenFdaApi::QueryBuilder
- Defined in:
- lib/open_fda_api/query_builder.rb
Overview
A helper to build queries against the openFDA API
The API supports five query parameters. The basic building block of queries is the search parameter. Use it to “filter” requests to the API by looking in specific fields for matches. Each endpoint has its own unique fields that can be searched.
search:
What to search for, in which fields. If you don't specify a field to search, the API will search in every field.
sort:
Sort the results of the search by the specified field in ascending or descending order by using the
:asc or :desc modifier.
count:
Count the number of unique values of a certain field, for all the records that matched the search parameter.
By default, the API returns the 1000 most frequent values.
limit:
Return up to this number of records that match the search parameter. Currently, the largest allowed value for the
limit parameter is 1000.
skip:
Skip this number of records that match the search parameter, then return the matching records that follow.
Use in combination with limit to paginate results. Currently, the largest allowed value for the skip parameter
is 25000. See Paging if you require paging through larger result sets.
Instance Method Summary collapse
-
#build_query ⇒ Hash
The query string portion of a request.
-
#initialize(query_input:, valid_search_fields:) ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
Constructor Details
#initialize(query_input:, valid_search_fields:) ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/open_fda_api/query_builder.rb', line 30 def initialize(query_input:, valid_search_fields:) # TODO: Turn validations back on once we get basic functionality working; need to flex on different field types # validate_arguments!(valid_search_fields, query_input: query_input) warn "You've passed in a valid_search_fields arg but it isn't being used right now..." if valid_search_fields @search = build_query_string(query_fields: query_input.search) @sort = build_query_string(query_fields: query_input.sort) @count = build_query_string(query_fields: query_input.count) @skip = build_skip_string(query_input.skip) @limit = query_input.limit @api_key = query_input.api_key end |
Instance Method Details
#build_query ⇒ Hash
Returns the query string portion of a request.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/open_fda_api/query_builder.rb', line 43 def build_query { api_key: @api_key, search: @search, sort: @sort, count: @count, skip: @skip, limit: @limit }.compact.reject { |_k, v| v.to_s.empty? } end |