Class: BugGuide::Photo
- Inherits:
-
Object
- Object
- BugGuide::Photo
- Defined in:
- lib/bugguide/photo.rb
Overview
Represents a single photo on BugGuide. Several methods are intended for compatability with the DarwinCore SimpleMultimedia extention (rs.gbif.org/terms/1.0/Multimedia).
Instance Attribute Summary collapse
-
#city_location ⇒ Object
Returns the value of attribute city_location.
-
#county ⇒ Object
Returns the value of attribute county.
-
#date ⇒ Object
(also: #created)
Returns the value of attribute date.
-
#id ⇒ Object
(also: #identifier)
Returns the value of attribute id.
-
#state ⇒ Object
Returns the value of attribute state.
-
#taxon ⇒ Object
Returns the value of attribute taxon.
-
#thumbnail_url ⇒ Object
Returns the value of attribute thumbnail_url.
-
#title ⇒ Object
Returns the value of attribute title.
-
#url ⇒ Object
(also: #references)
Returns the value of attribute url.
Class Method Summary collapse
-
.search(options = {}) ⇒ Object
Search for photos.
Instance Method Summary collapse
-
#format ⇒ Object
DarwinCore Simple Multimedia media format, aka MIME type.
-
#initialize(options = {}) ⇒ Photo
constructor
A new instance of Photo.
-
#publisher ⇒ Object
DarwinCore Simple Multimedia publisher, always BugGuide in this case.
-
#type ⇒ Object
DarwinCore Simple Multimedia media type.
Constructor Details
#initialize(options = {}) ⇒ Photo
Returns a new instance of Photo.
9 10 11 12 13 |
# File 'lib/bugguide/photo.rb', line 9 def initialize( = {}) .each do |k,v| send("#{k}=", v) end end |
Instance Attribute Details
#city_location ⇒ Object
Returns the value of attribute city_location.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def city_location @city_location end |
#county ⇒ Object
Returns the value of attribute county.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def county @county end |
#date ⇒ Object Also known as: created
Returns the value of attribute date.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def date @date end |
#id ⇒ Object Also known as: identifier
Returns the value of attribute id.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def id @id end |
#state ⇒ Object
Returns the value of attribute state.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def state @state end |
#taxon ⇒ Object
Returns the value of attribute taxon.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def taxon @taxon end |
#thumbnail_url ⇒ Object
Returns the value of attribute thumbnail_url.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def thumbnail_url @thumbnail_url end |
#title ⇒ Object
Returns the value of attribute title.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def title @title end |
#url ⇒ Object Also known as: references
Returns the value of attribute url.
7 8 9 |
# File 'lib/bugguide/photo.rb', line 7 def url @url end |
Class Method Details
.search(options = {}) ⇒ Object
Search for photos. This method depends on BugGuide’s Advanced Search functionality, which will bail if your search returns too much results, so this will throw an exception in that case that you should be prepared to deal with.
36 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 81 82 |
# File 'lib/bugguide/photo.rb', line 36 def self.search( = {}) raise BugGuide::NoParametersException if .blank? url = "http://bugguide.net/adv_search/bgsearch.php?" .stringify_keys! headers = [:headers] || {} params = [] %w(user taxon description county city_location adult immature male female representative).each do |param| next if [param] != false && [param].blank? params << if [param] == true || [param] == false "#{param}=#{options[param] ? 1 : 0}" else "#{param}=#{options[param]}" end end states = [['state'], ['location']].flatten.compact.uniq params << states.map{|s| "location[]=#{s}"} unless states.blank? params << [['month']].flatten.map{|s| "month[]=#{s}"} unless ['month'].blank? url += URI.escape( params.join('&') ) photos = [] # puts "fetching #{url}" open(url, headers) do |response| html = Nokogiri::HTML(response.read.encode('UTF-8')) if html.to_s =~ /Too many results \(\d+\)/ raise BugGuide::TooManyResultsException end names = [] html.css('body > table tr').each do |tr| next if tr.css('th').size > 0 photos << BugGuide::Photo.new( thumbnail_url: tr.css('img')[0][:src], id: tr.children[1].text.to_i, url: tr.children[1].css('a')[0][:href], title: tr.children[2].text, date: tr.children[3].text, state: tr.children[4].text, county: tr.children[5].text, city_location: tr.children[6].text, taxon: BugGuide::Taxon.new( name: tr.children[7].text, id: tr.children[7].css('a')[0][:href].to_s[/\d+$/, 0], url: tr.children[7].css('a')[0][:href] ) ) end end photos end |
Instance Method Details
#format ⇒ Object
DarwinCore Simple Multimedia media format, aka MIME type
102 103 104 |
# File 'lib/bugguide/photo.rb', line 102 def format "image/jpeg" end |
#publisher ⇒ Object
DarwinCore Simple Multimedia publisher, always BugGuide in this case
107 108 109 |
# File 'lib/bugguide/photo.rb', line 107 def publisher "BugGuide" end |
#type ⇒ Object
DarwinCore Simple Multimedia media type
97 98 99 |
# File 'lib/bugguide/photo.rb', line 97 def type "StillImage" end |