Class: Taxa::PlantsOfTheWorldOnline::Client
- Inherits:
-
Object
- Object
- Taxa::PlantsOfTheWorldOnline::Client
- Defined in:
- lib/taxa/plants_of_the_world_online/client.rb
Overview
API client for Open Tree of Life
Constant Summary collapse
- POWO_URL =
'http://www.plantsoftheworldonline.org/api/2'
- FILTERS =
%w[accepted_names has_images families_f genus_f species_f infraspecific_f].freeze
Instance Attribute Summary collapse
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(**options) ⇒ Client
constructor
A new instance of Client.
- #lookup(id, include = nil) ⇒ Object
- #search(query, options = {}) ⇒ Object
Constructor Details
#initialize(**options) ⇒ Client
Returns a new instance of Client.
15 16 17 18 |
# File 'lib/taxa/plants_of_the_world_online/client.rb', line 15 def initialize(**) @options = @http_client = [:http_client] || Faraday.new end |
Instance Attribute Details
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
9 10 11 |
# File 'lib/taxa/plants_of_the_world_online/client.rb', line 9 def http_client @http_client end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/taxa/plants_of_the_world_online/client.rb', line 8 def @options end |
Instance Method Details
#lookup(id, include = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/taxa/plants_of_the_world_online/client.rb', line 33 def lookup(id, include = nil) raise ArgumentError, 'id can not be nil' if id.nil? include = Array.wrap(include) payload = {} payload.merge!({ fields: include.join(',') }) unless include.empty? response = @http_client.get("#{POWO_URL}/taxon/#{id}", payload, { 'Accept' => 'application/json' }) parse_response(response) end |
#search(query, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/taxa/plants_of_the_world_online/client.rb', line 20 def search(query, = {}) .transform_keys!(&:to_sym) page = [:page] || 0 filters = Array.wrap([:filters]) validate_search_parameters(query, page, filters) payload = { q: query, p: page } payload.merge!({ f: filters.join(',') }) unless filters.empty? response = @http_client.get("#{POWO_URL}/search", payload, { 'Accept' => 'application/json' }) parse_response(response) end |