Class: TaxonReporter::DataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/taxon_reporter/data_source.rb

Direct Known Subclasses

EolDataSource

Class Method Summary collapse

Class Method Details

.get_api_response(url) ⇒ Object



20
# File 'lib/taxon_reporter/data_source.rb', line 20

def self.get_api_response(url); HTTParty.get(url); end

.get_api_result(url, path) ⇒ Object



6
# File 'lib/taxon_reporter/data_source.rb', line 6

def self.get_api_result(url, path); process_response(get_api_response(url), path); end

.get_api_results(url, spec) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/taxon_reporter/data_source.rb', line 8

def self.get_api_results(url, spec)
  response = get_api_response(url)
  if response
    result = {}
    spec.each do |key, path|
      value = process_response(response, path)
      result[key] = value if value
    end
    result
  end
end

.process_response(response, keys) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/taxon_reporter/data_source.rb', line 22

def self.process_response(response, keys)
  if response.class == Array
    results = Set.new
    response.each {|r|
      s = process_response(r, keys)
      results += s unless s.nil?
    }
    results.length == 0 ? nil : results
  elsif keys == []
    response ? Set.new([response]) : nil
  elsif response.respond_to?(:keys)
    key = keys[0]
    if key.class == Array
      Set.new([key.map {|k| response[k]}])
    else
      process_response(response[key], keys[1..-1])
    end
  end
end