Module: Casento

Defined in:
lib/casento.rb,
lib/casento/version.rb,
lib/casento/occurrence.rb

Defined Under Namespace

Classes: Occurrence

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.get_page(page, opts) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/casento.rb', line 25

def self.get_page( page, opts )
  opts.symbolize_keys!
  # puts "opts: #{opts.inspect}"
  url = "http://researcharchive.calacademy.org/research/entomology/EntInv/index.asp?"
  params = {
    "Page" => page,
    "xAction" => "Search"
  }
  params["Country"] = opts[:country] if opts[:country]
  params["StateProv"] = opts[:state] if opts[:state]
  params["County"] = opts[:county] if opts[:county]
  params["Ord"] = opts[:order] if opts[:order]
  %w(Family Genus Species Subspecies).each do |rank|
    val = opts[rank.to_sym] || opts[rank.downcase.to_sym]
    params[rank] = val if val
  end
  url = "#{url}#{URI.encode_www_form( params )}"
  # puts "opening #{url}"
  headers = %w(
    order
    family
    genus
    species
    subspecies
    country
    state
    county
    url
  )
  occurrences = []
  open( url ) do |response|
    # puts "response: #{response}"
    html = Nokogiri::HTML(response.read)
    html.xpath("//tr[td[@class='tdata']]").each do |tr|
      occ = Occurrence.new
      tr.css("td").each_with_index do |td, i|
        # puts "td: #{td}"
        val = if headers[i] == "url"
          td.at("a")[:href]
        else
          td.inner_text.to_s
        end
        val = val.gsub(/[[:space:]]+$/, "").strip
        next if val.blank?
        next if val =~ /not entered/
        # puts "setting #{headers[i]} to #{val}"
        occ.send("#{headers[i]}=", val)
      end
      occurrences << occ
    end
  end
  occurrences
end

.search(opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/casento.rb', line 13

def self.search( opts = {} )
  occurrences = []
  page = 1
  loop do
    page_results = get_page( page, opts )
    break if page_results.blank?
    occurrences += page_results
    page += 1
  end
  occurrences
end