Class: Taxa::PlantsOfTheWorldOnline::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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)
  @options = options
  @http_client = options[:http_client] || Faraday.new
end

Instance Attribute Details

#http_clientObject (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

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/taxa/plants_of_the_world_online/client.rb', line 8

def options
  @options
end

Instance Method Details

#lookup(id, include = nil) ⇒ Object

Raises:

  • (ArgumentError)


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, options = {})
  options.transform_keys!(&:to_sym)

  page = options[:page] || 0
  filters = Array.wrap(options[: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