Class: Tuiter::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
# File 'lib/tuiter/client.rb', line 6

def initialize(options = {})
  @pid = Process.pid
  @logger = options[:logger] || Logger.new('tuiter.log')
  @username = options[:username]
  @password = options[:password]
  @use_proxy = setup_a_proxy?
  log("initialize()")
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/tuiter/client.rb', line 4

def password
  @password
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/tuiter/client.rb', line 4

def username
  @username
end

Instance Method Details

#direct_list(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tuiter/client.rb', line 49

def direct_list(options = {})
  url = 'http://twitter.com/direct_messages.json'
  params = parse_options(options) || ""

  if res = request(url+params)
    data = JSON.parse(res)
    return data.map { |d| DirectMessage.new(d) }
  else
    return nil
  end
end

#direct_new(user, text) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tuiter/client.rb', line 32

def direct_new(user, text)
  log("direct_new() sending: #{text} to #{user}")
  url = URI.parse('http://twitter.com/direct_messages/new.json')
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth @username, @password
  req.set_form_data({'user'=>user, 'text'=>text })
  res = new_http_for(url).start {|http| http.request(req) }
  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    log("direct_new() success: OK")
    return res # OK
  else
    log("direct_new() error: #{res.error!}")
    res.error!
  end
end

#follows_me?(id) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
210
211
# File 'lib/tuiter/client.rb', line 204

def follows_me?(id)
  if res = request("http://twitter.com/friendships/exists.json?user_a=#{id}&user_b=#{@username}")
    return true if res == "true"
    return false
  else
    return nil
  end
end

#friendship_new(user, follow = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tuiter/client.rb', line 61

def friendship_new(user, follow = nil)
  log("friendship_new() following: #{user}")
  url = URI.parse("http://twitter.com/friendships/create/#{user}.json")
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth @username, @password
  req.set_form_data({'follow'=>"true"}) if follow
  res = new_http_for(url).start {|http| http.request(req) }
  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    log("friendship_new() success: OK")
    return res # OK
  else
    log("friendship_new() error: #{res.error!}")
    res.error!
  end
end

#get_clientObject



172
173
174
175
176
177
178
# File 'lib/tuiter/client.rb', line 172

def get_client
  if res = request("http://twitter.com/users/show/#{@username}.json")
    return UserExtended.new(JSON.parse(res))
  else
    return nil
  end
end

#get_followers(options = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tuiter/client.rb', line 102

def get_followers(options = {})
  if options[:id]
    query = "http://twitter.com/statuses/followers/#{options[:id]}.json"
  else
    query = "http://twitter.com/statuses/followers.json"
  end
  if options[:page]
    params = "?page=#{options[:page]}"
  else
    params = ""
  end
  if res = request(query+params)
    data = JSON.parse(res)
    return data.map { |d| User.new(d) }
  else
    return nil
  end
end

#get_followers_idsObject



121
122
123
124
125
126
127
# File 'lib/tuiter/client.rb', line 121

def get_followers_ids 
  if res = request("http://twitter.com/followers/ids/#{username}.json")
    return JSON.parse(res)
  else
    return nil
  end
end

#get_friends(options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/tuiter/client.rb', line 129

def get_friends(options = {})
  if options[:id]
    query = "http://twitter.com/statuses/friends/#{options[:id]}.json"
  else
    query = "http://twitter.com/statuses/friends.json"
  end
  if options[:page]
    params = "?page=#{options[:page]}"
  else
    params = ""
  end
  if res = request(query+params)
    data = JSON.parse(res)
    return data.map { |d| User.new(d) }
  else
    return nil
  end
end

#get_replies(options = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tuiter/client.rb', line 148

def get_replies(options = {})
  query = "http://twitter.com/statuses/replies.json"
  if options[:since]
    params = "?since=#{options[:since]}"
  elsif options[:since_id]
    params = "?since_id=#{options[:since_id]}"
  else
    params = ""
  end
  if options[:page]
    if params == ""
        params = "?page=#{options[:page]}"
    else
        params = params + "&" + "page=#{options[:page]}"
    end
  end
  if res = request(query+params)
    data = JSON.parse(res)
    return data.map { |d| Status.new(d) }
  else
    return nil
  end
end

#get_status(id) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/tuiter/client.rb', line 188

def get_status(id)
  if res = request("http://twitter.com/statuses/show/#{id}.json")
    return Status.new(JSON.parse(res))
  else
    return nil
  end
end

#get_user(id) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/tuiter/client.rb', line 180

def get_user(id)
  if res = request("http://twitter.com/users/show/#{id}.json")
    return UserExtended.new(JSON.parse(res))
  else
    return nil
  end
end

#rate_limitObject



196
197
198
199
200
201
202
# File 'lib/tuiter/client.rb', line 196

def rate_limit
  if res = request("http://twitter.com/account/rate_limit_status.json")
    return RateLimit.new(JSON.parse(res))
  else
    return nil
  end
end

#remove_friendship(user, follow = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tuiter/client.rb', line 78

def remove_friendship(user, follow = nil)
  log("friendship_new() following: #{user}")
  url = URI.parse("http://twitter.com/friendships/destroy/#{user}.json")
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth @username, @password
  res = new_http_for(url).start {|http| http.request(req) }
  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    log("remove_friendship() success: OK")
    return res # OK
  else
    log("remove_friendship() error: #{res.error!}")
    res.error!
  end
end

#update(status, in_reply_to_status_id = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tuiter/client.rb', line 15

def update(status, in_reply_to_status_id = nil)
  log("update() sending: #{status}")
  url = URI.parse('http://twitter.com/statuses/update.json')
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth @username, @password
  req.set_form_data({'status'=>status, 'in_reply_to_status_id'=>in_reply_to_status_id })
  res = new_http_for(url).start {|http| http.request(req) }
  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    log("update() success: OK")
    return res # OK
  else
    log("update() error: #{res.to_s}")
    res.error!
  end
end

#verify_credentials?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# File 'lib/tuiter/client.rb', line 94

def verify_credentials?
  if res = request("http://twitter.com/account/verify_credentials.json")
    return UserExtended.new(JSON.parse(res))
  else
    return nil
  end
end