Class: Bananomia::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/bananomia/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Request

Returns a new instance of Request.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bananomia/request.rb', line 14

def initialize(**args)
  @endpoint = args[:endpoint]
  @verbose = args[:verbose]
  @q = args[:q]
  @families_collected = args[:families_collected]
  @families_identified = args[:families_identified]
  @date = args[:date]
  @strict = args[:strict]
  @callback = args[:callback]
  @is_public = args[:is_public]
  @has_occurrences = args[:has_occurrences]
  @dataset_id = args[:dataset_id]
  @occurrence_id = args[:occurrence_id]
  @names = args[:names]&.gsub('\n', "\n")&.gsub('\r', "\r")
  @limit = args[:limit]
  @page = args[:page]
  @options = args[:options] # TODO: not added at bananomia.rb
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



8
9
10
# File 'lib/bananomia/request.rb', line 8

def endpoint
  @endpoint
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/bananomia/request.rb', line 12

def options
  @options
end

#qObject

Returns the value of attribute q.



9
10
11
# File 'lib/bananomia/request.rb', line 9

def q
  @q
end

#verboseObject

Returns the value of attribute verbose.



10
11
12
# File 'lib/bananomia/request.rb', line 10

def verbose
  @verbose
end

Instance Method Details

#performObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bananomia/request.rb', line 33

def perform

  args = {
    q: @q,
    families_collected: @families_collected,
    families_identified: @families_identified,
    date: @date,
    strict: @strict,
    callback: @callback,
    names: @names,
    is_public: @is_public,
    has_occurrences: @has_occurrences,
    datasetKey: @dataset_id,
    occurrenceID: @occurrence_id,
    limit: @limit,
    page: @page
  }
  opts = args.delete_if { |_k, v| v.nil? }

  Faraday::Utils.default_space_encoding = "+"

  conn = if verbose
           Faraday.new(url: Bananomia.base_url) do |f|
             f.response :logger
             f.use Faraday::ColrapiErrors::Middleware
             f.adapter Faraday.default_adapter
           end
         else
           Faraday.new(url: Bananomia.base_url) do |f|
             f.use Faraday::ColrapiErrors::Middleware
             f.adapter Faraday.default_adapter
           end
         end

  conn.headers['Accept'] = 'application/ld+json,*/*'
  conn.headers[:user_agent] = make_user_agent
  conn.headers["X-USER-AGENT"] = make_user_agent

  res = conn.get(endpoint, opts)

  # Handles ChecklistBank endpoints that do not return JSON
  begin
    MultiJson.load(res.body)
  rescue MultiJson::ParseError
    res.body
  end
  
end