Module: Scrapers::NasaApod

Defined in:
lib/scrapers/nasa_apod.rb

Constant Summary collapse

NASA_APOD_URL =
"http://asterisk.apod.com/library/APOD/APOD%20mirror/astropix.html"

Class Method Summary collapse

Class Method Details

.scrape(url = nil) ⇒ Object



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
# File 'lib/scrapers/nasa_apod.rb', line 30

def scrape(url=nil)
  url ||= NASA_APOD_URL
  apod = Hash.new
  Mechanize.start do |m|

    m.get url
    
    # APOD has a funky entry page, but we want the actual page
    prev = m.current_page.link_with(:text => '<').href
    m.get prev
    canonical = m.current_page.link_with(:text => '>' ).href
    m.get canonical

    m.current_page.tap do |page|
      apod[:title] = page.title.strip
      apod[:link] = page.uri.to_s
      apod[:description] = (page/("body")).text
      apod[:pubDate] = page.response['date'].to_s
      apod[:guid] = page.uri.to_s
      apod[:content_encoded] = (page/("body")).to_html            
    end

  end
  apod
end