Class: TwitterGetter::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, password = nil) ⇒ Base

Returns a new instance of Base.



21
22
23
24
# File 'lib/twitter_getter.rb', line 21

def initialize(user=nil, password=nil)
  @user = user
  @password = password
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



19
20
21
# File 'lib/twitter_getter.rb', line 19

def password
  @password
end

#userObject

Returns the value of attribute user.



19
20
21
# File 'lib/twitter_getter.rb', line 19

def user
  @user
end

Instance Method Details

#destroy_status(id) ⇒ Object

Raises:

  • (AuthenicationNeeded)


34
35
36
37
38
# File 'lib/twitter_getter.rb', line 34

def destroy_status(id)
  raise AuthenicationNeeded if @password.nil?
  t = JSON.parse(RestClient.post("http://#{@user}:#{@password}@twitter.com/statuses/destroy/#{id}.json", :id => id))
  Tweet.new(t)
end

#follow(id) ⇒ Object

Raises:

  • (AuthenicationNeeded)


68
69
70
71
72
73
74
75
76
77
# File 'lib/twitter_getter.rb', line 68

def follow(id)
  raise AuthenicationNeeded if @password.nil?
  begin
    u = JSON.parse(RestClient.post("http://#{@user}:#{@password}@twitter.com/friendships/create/#{id}.json", :id => id))
    u['status'] ? User.new(u) : nil
  rescue RestClient::ResourceNotFound => e
    p "Error - #{e}" 
    nil
  end
end

#follower_idsObject



49
50
51
# File 'lib/twitter_getter.rb', line 49

def follower_ids
  JSON.parse(RestClient.get("http://twitter.com/followers/ids/#{self.user}.json"))
end

#friend_idsObject



45
46
47
# File 'lib/twitter_getter.rb', line 45

def friend_ids
  JSON.parse(RestClient.get("http://twitter.com/friends/ids/#{self.user}.json"))
end

#rate_limit_statusObject



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

def rate_limit_status
  results = JSON.parse(RestClient.get("http://#{@user}:#{@password}@twitter.com/account/rate_limit_status.json"))
end

#search(term, opts = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/twitter_getter.rb', line 83

def search(term, opts = {})
  if opts[:max_pages]
    max_pages = opts[:max_pages]
    opts.delete(:max_pages)
  end
  term = CGI.escape(term)
  opts = {
    :since_id => 10,
    :rpp => 50
  }.merge(opts)
  params = hash2params(opts)
  tweets = []
  JSON.parse(RestClient.get("http://search.twitter.com/search.json?q=#{term}#{params}"))['results'].each do |t|
    tweets << Tweet.new(t)
  end
  results = tweets.size
  page = 2
  while results == 50 && page <= max_pages.to_i
    next_tweets = JSON.parse(RestClient.get("http://search.twitter.com/search.json?q=#{term}#{params}&page=#{page}"))['results']
    results = next_tweets.size
    next_tweets.each do |t|
      tweets << Tweet.new(t)
    end
    page += 1
  end
  tweets.reverse
end

#status(id) ⇒ Object



40
41
42
43
# File 'lib/twitter_getter.rb', line 40

def status(id)
  t = JSON.parse(RestClient.get("http://twitter.com/statuses/show/#{id}.json"))
  Tweet.new(t)
end

#unfollow(id) ⇒ Object

Raises:

  • (AuthenicationNeeded)


57
58
59
60
61
62
63
64
65
66
# File 'lib/twitter_getter.rb', line 57

def unfollow(id)
  raise AuthenicationNeeded if @password.nil?
  begin
    u = JSON.parse(RestClient.post("http://#{@user}:#{@password}@twitter.com/friendships/destroy/#{id}.json", :id => id))
    u['status'] ? User.new(u) : nil
  rescue RestClient::ResourceNotFound => e
    p "Error - #{e}" 
    nil
  end
end

#unfriendly_idsObject



53
54
55
# File 'lib/twitter_getter.rb', line 53

def unfriendly_ids
  self.friend_ids - self.follower_ids
end

#update_status(message, in_reply_to_status_id = nil) ⇒ Object

Raises:

  • (AuthenicationNeeded)


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

def update_status(message, in_reply_to_status_id = nil)
  raise AuthenicationNeeded if @password.nil?
  t = JSON.parse(RestClient.post("http://#{@user}:#{@password}@twitter.com/statuses/update.json", 
             :status => message,
             :in_reply_to_status_id => in_reply_to_status_id))
  Tweet.new(t)
end