Class: Tweeb::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/tweeb_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, options = {}) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tweeb_client.rb', line 27

def initialize(username, password, options={})
  @username = username
  @password = password
  @options = {
    :public_refresh => 60
  }.merge(options)
  self.class.basic_auth(@username, @password)
  @symbolizer = lambda {|hash| hash['user'].symbolize_keys; hash.symbolize_keys; return hash }
  @masher = lambda {|hash| Mash.new(hash) }
  @rate_limit_total = 150
  @rate_limit_reset = nil
  @rate_limit_remaining = nil
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#rate_limit_remainingObject (readonly)

Returns the value of attribute rate_limit_remaining.



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

def rate_limit_remaining
  @rate_limit_remaining
end

#rate_limit_resetObject (readonly)

Returns the value of attribute rate_limit_reset.



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

def rate_limit_reset
  @rate_limit_reset
end

#rate_limit_totalObject (readonly)

Returns the value of attribute rate_limit_total.



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

def rate_limit_total
  @rate_limit_total
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#am_i_following?(target) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
261
262
# File 'lib/tweeb_client.rb', line 258

def am_i_following?(target)
  rel = friendship_info(target)
  return true if rel[:source][:following]
  false
end

#befriend(user, options = {:follow => true}) ⇒ Object Also known as: follow



214
215
216
217
# File 'lib/tweeb_client.rb', line 214

def befriend(user, options={:follow => true})
  json = self.class.post("/friendships/create.json", :query => {:id => user}.merge(options))
  @masher.call(json)
end

#delete_direct_message(id) ⇒ Object



209
210
211
212
# File 'lib/tweeb_client.rb', line 209

def delete_direct_message(id)
  json = self.class.delete("/direct_messages/destroy.json", :query => {:id => id})
  @masher.call(json)
end

#delete_status(id) ⇒ Object



129
130
131
132
# File 'lib/tweeb_client.rb', line 129

def delete_status(id)
  json = self.class.delete("/statuses/destroy/#{id}.json")
  @masher.call(json)
end

#direct_message(recipient, message) ⇒ Object



204
205
206
207
# File 'lib/tweeb_client.rb', line 204

def direct_message(recipient, message)
  json = self.class.post("/direct_messages/new.json", :query => {:user => recipient, :text => message})
  @masher.call(json)
end

#direct_message_inbox(options = {}) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/tweeb_client.rb', line 171

def direct_message_inbox(options={})
  if options.empty?
    json = self.class.get("/direct_messages.json")
  else
    if options[:count] and options[:count] > 200
      raise Tweeb::ClientError, "Count must be less than 200"
    end
    json = self.class.get("/direct_messages.json", :query => options)
  end
  timeline = Array.new
  json.each do |tweet|
    mash = @masher.call(tweet)
    timeline << mash
  end
  timeline
end

#direct_messages_sent(options = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/tweeb_client.rb', line 188

def direct_messages_sent(options={})
  if options.empty?
    json = self.class.get("/direct_messages/sent.json")
  else
    if options[:count] and options[:count] > 200
      raise Tweeb::ClientError, "Count must be less than 200"
    end
    json = self.class.get("/direct_messages/sent.json", :query => options)
  end
  json.each do |tweet|
    mash = @masher.call(tweet)
    timeline << mash
  end
  timeline
end

#following_me?(target) ⇒ Boolean

Returns:

  • (Boolean)


252
253
254
255
256
# File 'lib/tweeb_client.rb', line 252

def following_me?(target)
  rel = friendship_info(target)
  return true if rel[:source][:followed_by]
  false
end

#friends(options = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/tweeb_client.rb', line 152

def friends(options={})
  if options.empty?
    json = self.class.get("/statuses/friends.json")
  else
    json = self.class.get("/statuses/friends.json", :query => options)
  end
  timeline = Array.new
  json.each do |tweet|
    mash = @masher.call(tweet)
    timeline << mash
  end
  timeline
end

#friends_timeline(options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tweeb_client.rb', line 83

def friends_timeline(options={})
  if options.empty?
    json = self.class.get("/statuses/friends_timeline.json")
  else
    if options[:count] && options[:count] > 200
      raise Tweeb::ClientError, "Count must be less than 200"
    end
    json = self.class.get("/statuses/friends_timeline.json", :query => options)
  end
  timeline = Array.new
  json.each do |tweet|
    mash = @masher.call(tweet)
    timeline << mash
  end
  timeline
end

#friendship_exists?(target, source = nil) ⇒ Boolean

Returns:

  • (Boolean)


246
247
248
249
250
# File 'lib/tweeb_client.rb', line 246

def friendship_exists?(target, source=nil)
  rel = source.nil? ? friendship_info(target) : friendship_info(target, source)
  return true if rel[:source][:following] && rel[:target][:following]
  false
end

#friendship_info(target, source = nil) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/tweeb_client.rb', line 226

def friendship_info(target, source=nil)
  query = Hash.new
  if source
    if source.is_a?(Fixnum)
      query[:source_id] = source
    elsif source.is_a?(String)
      query[:source_screen_name] = source
    end
  end
  
  if target.is_a?(Fixnum)
    query[:target_id] = target
  elsif target.is_a?(String)
    query[:target_screen_name] = target
  end
  
  json = self.class.get("/friendships/show.json", :query => query)
  @masher.call(json)
end

#get_status(id) ⇒ Object



147
148
149
150
# File 'lib/tweeb_client.rb', line 147

def get_status(id)
  json = self.class.get("/statuses/show/#{id}.json")
  @masher.call(json)
end

#mentions(options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tweeb_client.rb', line 100

def mentions(options={})
  if options.empty?
    json = self.class.get("/statuses/mentions.json")
  else
    if options[:count] and options[:count] > 200
      raise Tweeb::ClientError, "Count must be less than 200"
    end
    json = self.class.get("/statuses/mentions.json", :query => options)
  end
  timeline = Array.new
  json.each do |tweet|
    mash = @masher.call(tweet)
    timeline << mash
  end
  timeline
end

#public_timeline(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tweeb_client.rb', line 49

def public_timeline(options={})
  if options.empty?
    json = self.class.get("/statuses/public_timeline.json")
  else
    if options[:count] && options[:count] > 200
      raise Tweeb::ClientError, "Count must be less than 200"
    end
    json = self.class.get("/statuses/public_timeline.json", :query => options)
  end
  timeline = Array.new
  json.each do |tweet|
    mash = @masher.call(tweet)
    timeline << mash
  end
  timeline
end

#rate_limitsObject



272
273
274
# File 'lib/tweeb_client.rb', line 272

def rate_limits
  {:total => @rate_limit_total, :remaining => @rate_limit_remaining, :reset_time => @rate_limit_reset}
end

#search(query, options = {}) ⇒ Object



134
135
136
# File 'lib/tweeb_client.rb', line 134

def search(query, options={})
  Tweeb::Search.search(query, options)
end

#show_user(id) ⇒ Object



166
167
168
169
# File 'lib/tweeb_client.rb', line 166

def show_user(id)
  json = self.class.get("/users/show/#{id}.json")
  @masher.call(json)
end

#timeline(type, options = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/tweeb_client.rb', line 41

def timeline(type, options={})
  case type
    when :public then public_timeline(options)
    when :user then user_timeline(options)
    when :friends then friends_timeline(options)
  end
end


138
139
140
141
142
143
144
145
# File 'lib/tweeb_client.rb', line 138

def trends(type=:trends)
  case type
    when :trends then Tweeb::Search.trends
    when :current then Tweeb::Search.current_trends
    when :daily then Tweeb::Search.daily_trends
    when :weekly then Tweeb::Search.weekly_trends
  end
end

#unfriend(user) ⇒ Object Also known as: unfollow



220
221
222
223
# File 'lib/tweeb_client.rb', line 220

def unfriend(user)
  json = self.class.delete("/friendships/destroy.json", :query => {:id => user})
  @masher.call(json)
end

#update_rate_limitsObject



264
265
266
267
268
269
270
# File 'lib/tweeb_client.rb', line 264

def update_rate_limits
  json = self.class.get("/account/rate_limit_status.json")
  @rate_limit_total = json["hourly_limit"].to_i
  @rate_limit_remaining = json["remaining_hits"].to_i
  @rate_limit_reset = Time.parse(json["reset_time"])
  rate_limits
end

#update_status(message, reply_id = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/tweeb_client.rb', line 117

def update_status(message, reply_id=nil)
  if message.length > 140
    raise Tweeb::ClientError, "Message must be 140 characters or less"
  end
  if reply_id
    json = self.class.post("/statuses/update.json", :query => {:status => message, :in_reply_to_status_id => reply_id})
  else
    json = self.class.post("/statuses/update.json", :query => {:status => message})
  end
  @masher.call(json)
end

#user_timeline(options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tweeb_client.rb', line 66

def user_timeline(options={})
  if options.empty?
    json = self.class.get("/statuses/user_timeline.json")
  else
    if options[:count] && options[:count] > 200
      raise Tweeb::ClientError, "Count must be less than 200"
    end
    json = self.class.get("/statuses/user_timeline.json", :query => options)
  end
  timeline = Array.new
  json.each do |tweet|
    mash = @masher.call(tweet)
    timeline << mash
  end
  timeline
end