Class: BugGuide::Photo

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  options.each do |k,v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#city_locationObject

Returns the value of attribute city_location.



7
8
9
# File 'lib/bugguide/photo.rb', line 7

def city_location
  @city_location
end

#countyObject

Returns the value of attribute county.



7
8
9
# File 'lib/bugguide/photo.rb', line 7

def county
  @county
end

#dateObject Also known as: created

Returns the value of attribute date.



7
8
9
# File 'lib/bugguide/photo.rb', line 7

def date
  @date
end

#idObject Also known as: identifier

Returns the value of attribute id.



7
8
9
# File 'lib/bugguide/photo.rb', line 7

def id
  @id
end

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/bugguide/photo.rb', line 7

def state
  @state
end

#taxonObject

Returns the value of attribute taxon.



7
8
9
# File 'lib/bugguide/photo.rb', line 7

def taxon
  @taxon
end

#thumbnail_urlObject

Returns the value of attribute thumbnail_url.



7
8
9
# File 'lib/bugguide/photo.rb', line 7

def thumbnail_url
  @thumbnail_url
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/bugguide/photo.rb', line 7

def title
  @title
end

#urlObject 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.

Parameters:

  • options (Hash) (defaults to: {})

    You need to choose enough to filter the results adequately user: user ID taxon: ancestor taxon ID description: free text search in the description of the photo month: numerical month of the year, 1-12 location: two-letter US state or Canadian province code, e.g. AK, BC, WA, OR,

    CA, etc. You can specify multiple states as an array of these codes.
    

    county: County or region name. No controlled voabulary, so you can use “Madera”

    but also "Sierra"
    

    city_location: City or location name. Like county it’s free text. adult: Boolean immature: Boolean male: Boolean female: Boolean representative: Boolean. It’s not clear to me what this means on BugGuide.

Raises:



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(options = {})
  raise BugGuide::NoParametersException if options.blank?
  url = "http://bugguide.net/adv_search/bgsearch.php?"
  options.stringify_keys!
  headers = options[:headers] || {}
  params = []
  %w(user taxon description county city_location adult immature male female representative).each do |param|
    next if options[param] != false && options[param].blank?
    params << if options[param] == true || options[param] == false
      "#{param}=#{options[param] ? 1 : 0}"
    else
      "#{param}=#{options[param]}"
    end
  end
  states = [options['state'], options['location']].flatten.compact.uniq
  params << states.map{|s| "location[]=#{s}"} unless states.blank?
  params << [options['month']].flatten.map{|s| "month[]=#{s}"} unless options['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

#formatObject

DarwinCore Simple Multimedia media format, aka MIME type



102
103
104
# File 'lib/bugguide/photo.rb', line 102

def format
  "image/jpeg"
end

#publisherObject

DarwinCore Simple Multimedia publisher, always BugGuide in this case



107
108
109
# File 'lib/bugguide/photo.rb', line 107

def publisher
  "BugGuide"
end

#typeObject

DarwinCore Simple Multimedia media type



97
98
99
# File 'lib/bugguide/photo.rb', line 97

def type
  "StillImage"
end