Class: TaxonReporter::EolDataSource

Inherits:
DataSource show all
Defined in:
lib/taxon_reporter/eol_data_source.rb

Constant Summary collapse

PAGES_DATA =
{
  "eol_id" => ["identifier"],
  "richness" => ["richness_score"],
  "ranks" => ["taxonConcepts", "taxonRank"],
  "scientificName" => ["scientificName"],
  "he_ids" => ["taxonConcepts", ["identifier", "nameAccordingTo"]]
}
DATA_SOURCE_NAME =
"EOL"
BAD_PROVIDERS =
["GBIF Nub Taxonomy"]
HE_DATA =
{
  "children" => ["children", "taxonConceptID"]
}
@@fields =
{}

Class Method Summary collapse

Methods inherited from DataSource

get_api_response, get_api_result, get_api_results, process_response

Class Method Details

.add_children(results) ⇒ Object



48
49
50
51
52
# File 'lib/taxon_reporter/eol_data_source.rb', line 48

def self.add_children(results)
  children = eol_children(filter_hes(results["he_ids"]))
  results["children"] = children if children.length > 0
  results
end

.add_field(field_name) ⇒ Object



66
67
68
69
# File 'lib/taxon_reporter/eol_data_source.rb', line 66

def self.add_field(field_name)
  @@fields[field_name] = TaxonReporter::Field.new(DATA_SOURCE_NAME, field_name) unless @@fields.member?(field_name)
  @@fields[field_name]
end

.eol_children(he_ids) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/taxon_reporter/eol_data_source.rb', line 89

def self.eol_children(he_ids)
  result = Set.new
  if he_ids
    he_ids.each do |he_id|
      result += (get_api_results(hierarchy_entries_url(he_id), HE_DATA)["children"] || [])
    end
  end
  result.sort
end

.filter_hes(hes_with_provider) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/taxon_reporter/eol_data_source.rb', line 75

def self.filter_hes(hes_with_provider)
  if hes_with_provider
    result = Set.new
    hes_with_provider.each do |he, provider|
      result << he unless BAD_PROVIDERS.member?(provider)
    end
    result
  end
end

.get_eol_ids_from_name(name) ⇒ Object

Set of ids



20
21
22
# File 'lib/taxon_reporter/eol_data_source.rb', line 20

def self.get_eol_ids_from_name(name) # Set of ids
  get_api_result(search_url(name), ["results", "id"])
end

.hierarchy_entries_url(id) ⇒ Object



99
# File 'lib/taxon_reporter/eol_data_source.rb', line 99

def self.hierarchy_entries_url(id); "http://eol.org/api/hierarchy_entries/#{id}.json?common_names=false&synonyms=false&cache_ttl=86400"; end

.pages_url(id) ⇒ Object



54
55
56
# File 'lib/taxon_reporter/eol_data_source.rb', line 54

def self.pages_url(id)
  "http://eol.org/api/pages/#{id}.json?images=0&videos=0&sounds=0&maps=0&text=0&iucn=false&details=false&common_names=false&synonyms=false&references=false&cache_ttl=86400"
end

.records(id) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/taxon_reporter/eol_data_source.rb', line 40

def self.records(id)
  result = []
  add_children(get_api_results(pages_url(id), PAGES_DATA)).each do |k, v|
    result.push(TaxonReporter::Record.new(add_field(k), v))
  end
  result
end

.search_url(name) ⇒ Object



24
# File 'lib/taxon_reporter/eol_data_source.rb', line 24

def self.search_url(name); "http://eol.org/api/search/#{URI::escape(name)}.json?exact=1&cache_ttl=86400"; end

.taxons_from_id(id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/taxon_reporter/eol_data_source.rb', line 26

def self.taxons_from_id(id)
  result = []
  records = self.records(id)
  if records.length > 0
    taxon = TaxonReporter::Taxon.new(self.records(id))
    result << taxon
    children = taxon.values(@@fields['children'])
    if children
      children.each {|c| result += taxons_from_id(c)}
    end
  end
  result
end

.taxons_from_name(name) ⇒ Object



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

def self.taxons_from_name(name)
  result = []
  ids = self.get_eol_ids_from_name(name)
  if ids
    ids.each do |id|
      result += self.taxons_from_id(id)
    end
  end
  result
end