Class: Reddit::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/reddit.rb

Constant Summary collapse

GuidRegExp =
/id=t3_(.+)$/
DescriptionRegExp =
/href="(.+)">\[link\]/
Urls =
{
  :hot  => "http://reddit.com/.rss",
  :new  => "http://reddit.com/new.rss",
}

Instance Method Summary collapse

Constructor Details

#initialize(section, page) ⇒ Reader

Returns a new instance of Reader.



41
42
43
44
# File 'lib/reddit.rb', line 41

def initialize(section, page)
  @section  = section
  @page     = page
end

Instance Method Details



72
73
74
# File 'lib/reddit.rb', line 72

def link_start
  @page * 25
end


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/reddit.rb', line 46

def links
  index=0
  collection = (page_data/:item).map do |item|
    rank        = link_start + (index += 1)
    site_id     = parse_guid((item/:guid).inner_html)
    title       = (item/:title).inner_html
    date        = (item/:"dc:date").inner_html
    url         = parse_description((item/:description).inner_html)
    
    Link.new(rank, site_id, title, date, url)
  end      
end

#page_dataObject



67
68
69
70
# File 'lib/reddit.rb', line 67

def page_data
  params = "?count=#{link_start}"
  doc = Hpricot.XML(open("#{Urls[@section]}#{params}"))
end

#parse_description(description) ⇒ Object



63
64
65
# File 'lib/reddit.rb', line 63

def parse_description(description)
  DescriptionRegExp.match(description)[1]
end

#parse_guid(guid) ⇒ Object



59
60
61
# File 'lib/reddit.rb', line 59

def parse_guid(guid)
  GuidRegExp.match(guid)[1]
end