Class: Plazucchini::Request

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/plazucchini/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#deep_transform_keys, #make_user_agent, #to_snake_case

Constructor Details

#initialize(**args) ⇒ Request

Returns a new instance of Request.



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

def initialize(**args)
  @endpoint = args[:endpoint]
  @verbose = args[:verbose]
  @method = args[:method] || :get
  @format = args[:format] || :json
  @limit = args[:limit]
  @order_mode = args[:order_mode]
  @author = args[:author]
  @collector = args[:collector]
  @doi = args[:doi]
  @uuid = args[:uuid]
  @user = args[:user]
  @status = args[:status]
  @collection_code = args[:collection_code]
  @journal = args[:journal]
  @kingdom_epithet = args[:kingdom_epithet]
  @phylum_epithet = args[:phylum_epithet]
  @class_epithet = args[:class_epithet]
  @order_epithet = args[:order_epithet]
  @family_epithet = args[:family_epithet]
  @genus_epithet = args[:genus_epithet]
  @species_epithet = args[:species_epithet]
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



11
12
13
# File 'lib/plazucchini/request.rb', line 11

def endpoint
  @endpoint
end

#verboseObject

Returns the value of attribute verbose.



11
12
13
# File 'lib/plazucchini/request.rb', line 11

def verbose
  @verbose
end

Instance Method Details

#performObject



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/plazucchini/request.rb', line 37

def perform
  args = {
    limit: @limit,
    orderMode: @order_mode,
    DOI: @doi,
    UUID: @uuid,
    author: @author,
    collector: @collector,
    user: @user,
    status: @status,
    collectionCode: @collection_code,
    journal: @journal,
    kingdom: @kingdom_epithet,
    phylum: @phylum_epithet,
    class: @class_epithet,
    order: @order_epithet,
    family: @family_epithet,
    genus: @genus_epithet,
    species: @species_epithet
  }
  # Only include format param for JSON (XML endpoints don't accept format param)
  args[:format] = @format if @format == :json
  opts = args.delete_if { |_k, v| v.nil? }

  conn = Faraday.new(url: Plazucchini.base_url) do |f|
    f.request :json
    f.response :logger if @verbose
    f.use Faraday::PlazucchiniErrors::Middleware
    f.adapter Faraday.default_adapter
  end

  conn.headers['Accept'] = @format == :xml ? 'application/xml' : 'application/json'
  conn.headers['Content-Type'] = 'application/json' if @method == :post
  conn.headers[:user_agent] = make_user_agent
  conn.headers["X-USER-AGENT"] = make_user_agent

  res = if @method == :post
          conn.post(endpoint, opts.to_json)
        else
          conn.get(endpoint, opts)
        end

  parse_response(res.body)
end