Class: BugGuide::Taxon
- Inherits:
-
Object
- Object
- BugGuide::Taxon
- Defined in:
- lib/bugguide/taxon.rb
Overview
Represents a single taxon on BugGuide
One thing to keep in mind is that this will generally be instantiated from search results, so for certain methods, like ‘ancestry`, it will need to perform an additional request to retrieve the relevant data.
Several methods are intended for compatability with the DarwinCore SimpleMultimedia extention (rs.gbif.org/terms/1.0/Multimedia).
Constant Summary collapse
- NAME_PATTERN =
/[\w\s\-\'\.]+/
Instance Attribute Summary collapse
-
#common_name ⇒ Object
(also: #vernacularName)
Returns the value of attribute common_name.
-
#id ⇒ Object
(also: #taxonID)
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#scientific_name ⇒ Object
(also: #scientificName)
Returns the value of attribute scientific_name.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
-
.find(id) ⇒ Object
Find a single BugGuide taxon given its node ID.
-
.search(name, options = {}) ⇒ Object
Search for taxa, returns matching BugGuide::Taxon instances.
Instance Method Summary collapse
-
#ancestors ⇒ Object
All ancestor taxa of this taxon, or its classification if you prefer that terminology.
-
#higherClassification ⇒ Object
DarwinCore-compliant taxonomic classification.
-
#initialize(options = {}) ⇒ Taxon
constructor
A new instance of Taxon.
-
#rank ⇒ Object
(also: #taxonRank)
Taxonomic rank, e.g.
- #rank=(new_rank) ⇒ Object
-
#taxonomy_html ⇒ Object
HTML source of the taxon’s taxonomy page on BugGuide as a Nokogiri document.
Constructor Details
#initialize(options = {}) ⇒ Taxon
Returns a new instance of Taxon.
16 17 18 19 20 21 |
# File 'lib/bugguide/taxon.rb', line 16 def initialize( = {}) .each do |k,v| send("#{k}=", v) end self.url ||= "http://bugguide.net/node/view/#{id}" end |
Instance Attribute Details
#common_name ⇒ Object Also known as: vernacularName
Returns the value of attribute common_name.
14 15 16 |
# File 'lib/bugguide/taxon.rb', line 14 def common_name @common_name end |
#id ⇒ Object Also known as: taxonID
Returns the value of attribute id.
14 15 16 |
# File 'lib/bugguide/taxon.rb', line 14 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'lib/bugguide/taxon.rb', line 14 def name @name end |
#scientific_name ⇒ Object Also known as: scientificName
Returns the value of attribute scientific_name.
14 15 16 |
# File 'lib/bugguide/taxon.rb', line 14 def scientific_name @scientific_name end |
#url ⇒ Object
Returns the value of attribute url.
14 15 16 |
# File 'lib/bugguide/taxon.rb', line 14 def url @url end |
Class Method Details
.find(id) ⇒ Object
Find a single BugGuide taxon given its node ID
112 113 114 115 116 117 118 |
# File 'lib/bugguide/taxon.rb', line 112 def self.find(id) taxon = BugGuide::Taxon.new(id: id) taxon.name = taxon.taxonomy_html.css('.node-title h1').text taxon.scientific_name = taxon.taxonomy_html.css('.node-title i').text taxon.common_name = taxon.taxonomy_html.css('.node-title').text.split('-').last taxon end |
.search(name, options = {}) ⇒ Object
Search for taxa, returns matching BugGuide::Taxon instances
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/bugguide/taxon.rb', line 90 def self.search(name, = {}) # For reference, http://bugguide.net/adv_search/taxon.php?q=Sphecidae returns # 117327||Apoid Wasps (Apoidea)- traditional Sphecidae|2302 135|Sphecidae|Thread-waisted Wasps|2700 url = "http://bugguide.net/adv_search/taxon.php?q=#{URI.escape(name)}" headers = [:headers] || {} f = open(url) taxa = [] open(url, headers) do |f| f.read.split("\n").each do |row| row = row.split('|').compact.map(&:strip) taxa << BugGuide::Taxon.new( id: row[0], name: row[1], scientific_name: row[1], common_name: row[2] ) end end taxa end |
Instance Method Details
#ancestors ⇒ Object
All ancestor taxa of this taxon, or its classification if you prefer that terminology.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bugguide/taxon.rb', line 57 def ancestors return @ancestors if @ancestors @ancestors = [] nbsp = Nokogiri::HTML(" ").text @ancestors = taxonomy_html.css('.bgpage-roots a').map do |a| next unless a['href'] =~ /node\/view\/\d+\/tree/ t = BugGuide::Taxon.new( id: a['href'].split('/')[-2], name: a.text.gsub(nbsp, ' '), url: a['href'], rank: a['title'] ) if name_matches = t.name.match(/(#{NAME_PATTERN})\s+\((#{NAME_PATTERN})\)/) t.common_name = name_matches[1] t.scientific_name = name_matches[2] elsif name_matches = t.name.match(/(#{NAME_PATTERN})\s+\-\s+(#{NAME_PATTERN})/) t.common_name = name_matches[2] t.scientific_name = name_matches[1] end next if t.scientific_name == scientific_name t end.compact end |
#higherClassification ⇒ Object
DarwinCore-compliant taxonomic classification
135 136 137 |
# File 'lib/bugguide/taxon.rb', line 135 def higherClassification ancestors.map(&:scientific_name).join(' | ') end |
#rank ⇒ Object Also known as: taxonRank
Taxonomic rank, e.g. kingdom, phylum, order, etc.
51 52 53 54 |
# File 'lib/bugguide/taxon.rb', line 51 def rank return @rank if @rank @rank = taxonomy_html.css('.bgpage-roots a').last['title'].downcase end |
#rank=(new_rank) ⇒ Object
45 46 47 48 |
# File 'lib/bugguide/taxon.rb', line 45 def rank=(new_rank) @rank = new_rank.downcase @rank = nil if @rank == 'no taxon' end |
#taxonomy_html ⇒ Object
HTML source of the taxon’s taxonomy page on BugGuide as a Nokogiri document
82 83 84 85 86 87 |
# File 'lib/bugguide/taxon.rb', line 82 def taxonomy_html return @taxonomy_html if @taxonomy_html open("http://bugguide.net/node/view/#{id}/tree") do |response| @taxonomy_html = Nokogiri::HTML(response.read) end end |