Class: Checkerberry::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#make_user_agent

Constructor Details

#initialize(**args) ⇒ Request

Returns a new instance of Request.



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

def initialize(**args)
  @endpoint = args[:endpoint]
  @verbose = args[:verbose]
  @method = args[:method] || :get
  @names = args[:names]
  @data_sources = args[:data_sources]
  @vernacular_languages = args[:vernacular_languages]
  @with_all_matches = args[:with_all_matches]
  @with_capitalization = args[:with_capitalization]
  @with_species_group = args[:with_species_group]
  @with_uninomial_fuzzy_match = args[:with_uninomial_fuzzy_match]
  @with_stats = args[:with_stats]
  @min_taxon_threshold = args[:min_taxon_threshold]
  @query = args[:query]
  @parent_taxon = args[:parent_taxon]
  @name_string = args[:name_string]
  @genus = args[:genus]
  @species = args[:species]
  @species_any = args[:species_any]
  @infraspecies = args[:infraspecies]
  @author = args[:author]
  @year = args[:year]
  @year_start = args[:year_start]
  @year_end = args[:year_end]
  @with_all_results = args[:with_all_results]
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#performObject



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
81
82
83
84
85
86
87
88
89
90
# File 'lib/checkerberry/request.rb', line 39

def perform
  args = {
    nameStrings: @names,
    dataSources: @data_sources,
    vernaculars: @vernacular_languages,
    withAllMatches: @with_all_matches,
    withCapitalization: @with_capitalization,
    withSpeciesGroup: @with_species_group,
    withUninomialFuzzyMatch: @with_uninomial_fuzzy_match,
    withStats: @with_stats,
    mainTaxonThreshold: @min_taxon_threshold,
    query: @query,
    parentTaxon: @parent_taxon,
    nameString: @name_string,
    genus: @genus,
    species: @species,
    speciesAny: @species_any,
    speciesInfra: @infraspecies,
    author: @author,
    year: @year,
    withAllResults: @with_all_results
  }
  opts = args.delete_if { |_k, v| v.nil? }

  if @year_start && @year_end
    opts[:yearRange] = { yearStart: @year_start, yearEnd: @year_end }
  end

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

  conn.headers['Accept'] = '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

  begin
    MultiJson.load(res.body)
  rescue MultiJson::ParseError
    res.body
  end
end