Class: Featherdust

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

Constant Summary collapse

VERSION =
'0.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, num_posts = 20) ⇒ Featherdust

Returns a new instance of Featherdust.



11
12
13
14
15
# File 'lib/featherdust.rb', line 11

def initialize(username, num_posts = 20)
  @username = username
  @num_posts = num_posts
  @raw_posts = Array.new
end

Instance Attribute Details

#num_postsObject

Returns the value of attribute num_posts.



10
11
12
# File 'lib/featherdust.rb', line 10

def num_posts
  @num_posts
end

#postsObject

Returns the value of attribute posts.



10
11
12
# File 'lib/featherdust.rb', line 10

def posts
  @posts
end

#raw_postsObject

Returns the value of attribute raw_posts.



10
11
12
# File 'lib/featherdust.rb', line 10

def raw_posts
  @raw_posts
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/featherdust.rb', line 10

def url
  @url
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/featherdust.rb', line 10

def username
  @username
end

Class Method Details

.run(username, number) ⇒ Object



17
18
19
20
21
# File 'lib/featherdust.rb', line 17

def self.run(username, number)
  twitter_list = Featherdust.new(username, number)

  puts twitter_list.display_statuses
end

Instance Method Details

#display_statusesObject



54
55
56
57
58
59
60
61
# File 'lib/featherdust.rb', line 54

def display_statuses
  tweets = get_statuses
  @posts = "<ul>"
  tweets.each do |tweet|
    @posts += "<li><p class=\"twitter_status\">#{tweet[:text]}</p>\n<span><a href=\"http://twitter.com/#{@username}/statuses/#{tweet[:id]}\" title=\"Status update on Twitter\"> on #{tweet[:created_at]}</a>\n</span></li>\n"
  end
  @posts += "</ul>"
end

#get_statusesObject



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
# File 'lib/featherdust.rb', line 27

def get_statuses
  if is_twitter_up? != "true"
    raise "Twitter is having issues.  Please check back later."
  else
    # status_list = Nokogiri::XML(Net::HTTP.get("twitter.com", "/statuses/user_timeline/#{@username}.xml?count=#{@num_posts}"))
    status_list = Nokogiri::XML(Net::HTTP.get("twitter.com", "/statuses/user_timeline/#{@username}.xml?count=#{@num_posts}"))
    status_list.xpath('/statuses/status').each do |statuses|
      status_hash = {}
      statuses.children.each do |status|
        if status.name == "user"
          status.children.each do |user|
            user.name == "id" ? key = "user_id".to_sym : key = user.name.to_sym
            value = user.inner_text
            status_hash.store(key,value)
          end
        else
          key = status.name.to_sym
          value = status.inner_text
          status_hash.store(key,value)
        end
      end
      @raw_posts << status_hash
    end
  end
  return @raw_posts
end

#is_twitter_up?Boolean

Returns:

  • (Boolean)


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

def is_twitter_up?
  twitter_status = Nokogiri::XML(Net::HTTP.get("twitter.com","/help/test.xml"))
  twitter_status.xpath("/ok").inner_text
end