Class: Featherdust

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

Constant Summary collapse

VERSION =
'0.0.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Featherdust

Returns a new instance of Featherdust.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/featherdust.rb', line 12

def initialize(*args)

  args.each do |a|
        if a.class == String
          @username = a
        elsif a.class == Hash
          @plain_text = a[:display] if a[:display] = "plain"
        elsif a.class == Fixnum
          @num_posts = a
        else
          raise "Wrong variable type."
        end      
      end
  
  @username = username
  @num_posts = num_posts
  @raw_posts = Array.new
  
  
  @url = Nokogiri::XML(Net::HTTP.get("twitter.com", "/statuses/user_timeline/#{@username}.xml?count=#{@num_posts}"))
end

Instance Attribute Details

#num_postsObject

Returns the value of attribute num_posts.



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

def num_posts
  @num_posts
end

#postsObject

Returns the value of attribute posts.



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

def posts
  @posts
end

#raw_postsObject

Returns the value of attribute raw_posts.



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

def raw_posts
  @raw_posts
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#display_statusesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/featherdust.rb', line 34

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

#get_statusesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/featherdust.rb', line 54

def get_statuses
  if is_twitter_up? == "true"
    status_list = @url
    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)


78
79
80
81
# File 'lib/featherdust.rb', line 78

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

#pretty_time(date) ⇒ Object



83
84
85
86
87
88
# File 'lib/featherdust.rb', line 83

def pretty_time(date)
  human_date = Date.parse(date)
  pretty_date = human_date.strftime("%A, %b %d")
  human_time = Time.parse(date)
  pretty_date += human_time.strftime(" at %I:%M %p")
end

#urlize(string) ⇒ Object



90
91
92
93
94
# File 'lib/featherdust.rb', line 90

def urlize(string)
  words = string.split(" ")
  urlized = words.map{ |word| word =~ /(ht|f)tp(s?)/ ? word = "<a href=\"#{word}\">#{word}</a>" : word = word }
  urlized.join(" ")
end