Class: Stars::Favstar

Inherits:
Service show all
Defined in:
lib/stars/services/favstar.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

posts, #username

Instance Attribute Details

#postsObject (readonly)

Returns the value of attribute posts.



10
11
12
# File 'lib/stars/services/favstar.rb', line 10

def posts
  @posts
end

Class Method Details

.more(post) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stars/services/favstar.rb', line 61

def self.more(post)
  # hardcode 17 to strip favstar domain for now
  html = get(post.url[17..200], :format => :html)

  output = ''

  Nokogiri::HTML(html).css('div[id^="faved_by_others"] img').collect do |img|
    output << "#{img.attributes['alt'].value}\n"
  end

  Nokogiri::HTML(html).css('div[id^="rt_by_others"] img').collect do |img|
    output << "    RT  #{img.attributes['alt'].value}\n"
  end

  output << "\n"
  output << "More info at:\n"
  output << "  #{post.url}\n\n"
end

Instance Method Details

#nameObject



12
13
14
# File 'lib/stars/services/favstar.rb', line 12

def name
  "favstar"
end

#parse_post(xml) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/stars/services/favstar.rb', line 34

def parse_post(xml)
  title = xml["title"]
  Post.new(:name        => parse_title(title),
           :url         => xml["guid"],
           :service     => name,
           :date        => DateTime.parse(xml["pubDate"]),
           :stars_count => parse_stars(title))
end

#parse_stars(title) ⇒ Object



57
58
59
# File 'lib/stars/services/favstar.rb', line 57

def parse_stars(title)
  title.match(/[\d]+/)[0].to_i
end

#parse_title(title) ⇒ Object

Parse the title from a Favstar RSS title.

title - a Favstar-formatted String (x stars: title here)

This splits on the first colon, and then use everything after that. To account for tweets with colons in them, we have to strip the first “: ” String we find, and then shift the String back two characters.



50
51
52
53
54
55
# File 'lib/stars/services/favstar.rb', line 50

def parse_title(title)
  strip = title.split(':').first
  title = title.gsub(strip,'')
  title = title[2..-1] if title[0..1] == ": "
  title
end

#parse_xmlObject



26
27
28
29
30
31
32
# File 'lib/stars/services/favstar.rb', line 26

def parse_xml
  @posts = []

  xml_feed.each do |xml|
    @posts << parse_post(xml)
  end
end

#xml_feedObject



21
22
23
24
# File 'lib/stars/services/favstar.rb', line 21

def xml_feed
  self.class.get("/users/#{username}/rss",
                  :format => :xml)['rss']['channel']['item']
end