Class: Chirpy

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

Overview

Chirpy is a simple Twitter client for Ruby, written using RestClient and Hpricot.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil) ⇒ Chirpy

Makes a new instance of Chirpy

Example: chirpy = Chirpy.new('username', 'password')

Authentication, however, is not required.

Example: chirpy = Chirpy.new



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

def initialize(username = nil, password  = nil)
  authenticate(username, password) if username and password
end

Class Method Details

.decode_entities(string) ⇒ Object

Decodes HTML entities.



91
92
93
# File 'lib/chirpy.rb', line 91

def self.decode_entities(string)
  HTMLEntities.new.decode(string)
end

.public_timelineObject

Gets the public timeline. Authentication is not required for this.



113
114
115
# File 'lib/chirpy.rb', line 113

def self.public_timeline
  get "statuses/public_timeline"
end

.remove_crap(string) ⇒ Object

Search results have bold tags and links in them. This removes it all.



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

def self.remove_crap(string)
  remove_tags(decode_entities(string))
end

.remove_tags(string) ⇒ Object

Removes tags.



85
86
87
# File 'lib/chirpy.rb', line 85

def self.remove_tags(string)
  string.gsub(/<[^>]+>/, '')
end

.search(query, params = {}) ⇒ Object

Searches Twitter. Supply a query and extra options in a hash (not required). Available options (go here for more details: apiwiki.twitter.com/Twitter-Search-API-Method)

  • :lang

  • :rpp

  • :page

  • :since_id

  • :geocode



105
106
107
# File 'lib/chirpy.rb', line 105

def self.search(query, params = {})
  get "search", params.merge({:q => query})
end

.testObject

– Help methods



525
526
527
# File 'lib/chirpy.rb', line 525

def self.test
  get "help/test"
end

.verify_credentials(username, password) ⇒ Object

Use this to check if a username and password are valid. Returns a Chirpy instance if valid, otherwise, false.



347
348
349
350
# File 'lib/chirpy.rb', line 347

def self.verify_credentials(username, password)
  chirpy = self.new(username, password)
  chirpy.verify_credentials
end

Instance Method Details

#authenticate(username, password) ⇒ Object

Tells Chirpy to use authentication.

Example: chirpy.authenticate('username', 'password)



55
56
57
58
# File 'lib/chirpy.rb', line 55

def authenticate(username, password)
  @username = username
  @password = password
end

#block(user) ⇒ Object

Makes the authenticated user block someone. Authentication required.



483
484
485
# File 'lib/chirpy.rb', line 483

def block(user)
  post "blocks/create/#{user}"
end

#block_exists(user_or_params) ⇒ Object

Checks if the authenticated user is blocking someone. Pass in a username or a hash with one of the following options:

  • :user_id

  • :screen_name

Authentication required.



501
502
503
504
# File 'lib/chirpy.rb', line 501

def block_exists(user_or_params)
  args = [user_or_params]
  get path_from_args('block/exists', args), params_from_args(args)
end

#blocking(params = {}) ⇒ Object

Returns a list of people the authenticated user is blocking. You can pass :page => x if you want to.

Authentication required.



511
512
513
# File 'lib/chirpy.rb', line 511

def blocking(params = {})
  get "blocks/blocking", params
end

#blocking_idsObject

Returns a list of the ids of the people the authenticated user is blocking.

Authentication required.



519
520
521
# File 'lib/chirpy.rb', line 519

def blocking_ids
  get "blocks/blocking/ids"
end

#create_favorite(id) ⇒ Object

Adds a tweet to the authenticated user’s favorites.

Authentication required.



444
445
446
# File 'lib/chirpy.rb', line 444

def create_favorite(id)
  post "favorites/create/#{id}", {}
end

#create_friendship(user = nil, params = {}) ⇒ Object

Creates a friendship between authenticated user and another user. You may supply a hash as the only argument. Authentication required.

Optional parameters:

  • :user_id

  • :screen_name

  • :follow (automatically set to true)



284
285
286
287
# File 'lib/chirpy.rb', line 284

def create_friendship(user = nil, params = {})
  args = [user, params]
  post path_from_args('friendships/create', args), {:follow => true}.merge(params_from_args(args))
end

#destroy_block(user) ⇒ Object

Removes the authenticated user’s block. Authentication required.



490
491
492
# File 'lib/chirpy.rb', line 490

def destroy_block(user)
  delete "blocks/destroy/#{user}"
end

#destroy_direct_message(direct_message_id) ⇒ Object

Destroys a direct message.

Authentication required.



269
270
271
# File 'lib/chirpy.rb', line 269

def destroy_direct_message(direct_message_id)
  delete "direct_messages/destroy/#{direct_message_id}"
end

#destroy_favorite(id) ⇒ Object

Removes a tweet from the authenticated user’s favorites

Authentication required, Strong Bad.



452
453
454
# File 'lib/chirpy.rb', line 452

def destroy_favorite(id)
  delete "favorites/destroy/#{id}"
end

#destroy_friendship(user, params = {}) ⇒ Object

Destroys a friendship between the authenticated user and another user. You may supply a hash as the only argument. Authentication required.

Optional parameters:

  • :user_id

  • :screen_name



297
298
299
300
# File 'lib/chirpy.rb', line 297

def destroy_friendship(user, params = {})
  args = [user, params]
  delete path_from_args('friendships/destroy', args), params_from_args(args)
end

#destroy_status(status_id) ⇒ Object

Destroys one of the authenticated user’s tweets.

Authentication required.



183
184
185
# File 'lib/chirpy.rb', line 183

def destroy_status(status_id)
  delete "statuses/destroy/#{status_id}"
end

#direct_messages(params = {}) ⇒ Object

Gets a list of the messages sent to the authenticated user. Authentication required.

Optional parameters:

  • :since_id

  • :max_id

  • :count

  • :page



240
241
242
# File 'lib/chirpy.rb', line 240

def direct_messages(params = {})
  get "direct_messages", params
end

#direct_messages_new(recipient, text) ⇒ Object

Sends a direct message.

Authentication required.



260
261
262
263
# File 'lib/chirpy.rb', line 260

def direct_messages_new(recipient, text)
  post_params = {:user => recipient, :text => text}
  post "direct_messages/new", post_params
end

#direct_messages_sent(params = {}) ⇒ Object

Gets a list of the messages sent by the authenticated user. Authentication required.

Optional parameters:

  • :since_id

  • :max_id

  • :page



252
253
254
# File 'lib/chirpy.rb', line 252

def direct_messages_sent(params = {})
  get "direct_messages/sent", params
end

#end_sessionObject

Ends the session of the authenticated user



374
375
376
# File 'lib/chirpy.rb', line 374

def end_session
  post "account/end_session", :post => {}
end

#favorites(user = nil, params = {}) ⇒ Object

Gets a list of a user’s favorites. You may supply a hash as the only argument. Authentication required.

Optional parameters:

  • :id

  • :page



435
436
437
438
# File 'lib/chirpy.rb', line 435

def favorites(user = nil, params = {})
  args = [user, params]
  get path_from_args('favorites', args), params_from_args(args)
end

#follow(user_or_params) ⇒ Object

Makes the authenticated user follow a new person. Authentication required. Pass a username or a hash with one of the following options:

  • :user_id

  • :screen_name



463
464
465
466
# File 'lib/chirpy.rb', line 463

def follow(user_or_params)
  args = [user_or_params]
  post path_from_args('notifications/follow', args), params_from_args(args).merge({:post => {}})
end

#followers(user = nil, params = {}) ⇒ Object

Gets a list of a user’s followers. If no user is supplied, the authenticated user will be used. However, you need to authenticate whether or not you supply the user parameter. Authentication required. You may supply a hash as the only argument.

Optional parameters:

  • :user_id

  • :screen_name

  • :page



226
227
228
229
# File 'lib/chirpy.rb', line 226

def followers(user = nil, params = {})
  args = [user, params]
  get path_from_args('statuses/followers', args), params_from_args(args)
end

#followers_ids(user = nil, params = {}) ⇒ Object

Returns ids for someone’s followers. You may supply a hash as the only argument.

Optional parameters:

  • :user_id

  • :screen_name

  • :page



337
338
339
340
# File 'lib/chirpy.rb', line 337

def followers_ids(user = nil, params = {})
  args = [user, params]
  get path_from_args('followers/ids', args), params_from_args(params)
end

#friends(user = nil, params = {}) ⇒ Object

Gets a list of a user’s friends. If no user is supplied, the authenticated user will be used. you may supply a hash as the only argument.

Optional parameters:

  • :user_id

  • :screen_name

  • :page



210
211
212
213
# File 'lib/chirpy.rb', line 210

def friends(user = nil, params = {})
  args = [user, params]
  get path_from_args('statuses/friends', args), params_from_args(args)
end

#friends_ids(user = nil, params = {}) ⇒ Object

Returns ids for someone’s friends. You may supply a hash as the only argument.

Optional parameters:

  • :user_id

  • :screen_name

  • :page



325
326
327
328
# File 'lib/chirpy.rb', line 325

def friends_ids(user = nil, params = {})
  args = [user, params]
  get path_from_args('friends/ids', args), params_from_args(params)
end

#friends_timeline(params = {}) ⇒ Object

Gets the authenticated user’s friends’ timeline. Authentication required.

Optional parameters:

  • :since_id

  • :max_id

  • :count

  • :page



131
132
133
# File 'lib/chirpy.rb', line 131

def friends_timeline(params = {})
  get "statuses/friends_timeline", params
end

#friendship_exists?(user_a, user_b) ⇒ Boolean

Checks if a friendship exists between two users; returns true or false if no error occured. If an error did occur, it returns the usual object.

Authentication required.

Returns:

  • (Boolean)


307
308
309
310
311
312
313
314
# File 'lib/chirpy.rb', line 307

def friendship_exists?(user_a, user_b)
  response = get "friendships/exists", {:user_a => user_a, :user_b => user_b}
  if response.ok?
    response.data.%('friends').inner_html == 'true'
  else
    response
  end
end

#leave(user_or_params) ⇒ Object

Unfollows a person the authenticated user is following. Authentication required. Pass a username or a hash with one of the following options:

  • :user_id

  • :screen_name



473
474
475
476
# File 'lib/chirpy.rb', line 473

def leave(user_or_params)
  args = [user_or_params]
  post path_from_args('notifications/leave', args), params_from_args(args).merge({:post => {}})
end

#mentions(params = {}) ⇒ Object

Gets mentions for the authenticated user. Authentication required.

Optional parameters:

  • :since_id

  • :max_id

  • :count

  • :page



161
162
163
# File 'lib/chirpy.rb', line 161

def mentions(params = {})
  get "statuses/mentions", params
end

#public_timelineObject

Instance method for public timeline



119
120
121
# File 'lib/chirpy.rb', line 119

def public_timeline
  Chirpy.public_timeline
end

#rate_limit_status(params) ⇒ Object

Gets information on rate limiting. Specify :authenticate => false to see rate limiting for current ip



368
369
370
# File 'lib/chirpy.rb', line 368

def rate_limit_status(params)
  get "account/rate_limit_status", params
end

#show_status(status_id) ⇒ Object

Shows a specific tweet. Authentication is only required if author is protected.



169
170
171
# File 'lib/chirpy.rb', line 169

def show_status(status_id)
  get "statuses/show/#{status_id}"
end

#show_user(user = nil, params = {}) ⇒ Object

Shows details for a specific user. Authentication is only required if the user is protected. you may supply a hash as the only argument.

Optional parameters:

  • :user_id

  • :screen_name



196
197
198
199
# File 'lib/chirpy.rb', line 196

def show_user(user = nil, params = {})
  args = [user, params]
  get path_from_args('users/show', args), params_from_args(params)
end

#unauthenticateObject

Turns authentication off.



62
63
64
65
# File 'lib/chirpy.rb', line 62

def unauthenticate()
  @username = nil
  @password = nil
end

#update_delivery_device(device) ⇒ Object

Updates the authenticated user’s delivery device. Must be one of:

  • sms

  • im

  • none



383
384
385
# File 'lib/chirpy.rb', line 383

def update_delivery_device(device)
  post "account/update_delivery_device", :post => {:device => device}
end

#update_profile(params) ⇒ Object

Updates the user’s profile information. Pass in a hash with symbols as keys.

From: apiwiki.twitter.com/Twitter-REST-API-Method%3A-account%C2%A0update_profile

  • :name, 20 characters max.

  • :email, 40 characters max. Must be a valid email address.

  • :url, 100 characters max. Will be prepended with “http://” if not present.

  • :location, 30 characters max. The contents are not normalized or geocoded in any way.

  • :descriptionm 160 characters max.



421
422
423
# File 'lib/chirpy.rb', line 421

def update_profile(params)
  post 'account/update_profile', :post => params
end

#update_profile_colors(colors) ⇒ Object

Updates the authenticated user’s colors (on their Twitter page).

Please supply a hash with hexadecimal colors (e.g. “fff” or “ffffff”). Don’t include the “#” character in front of the color code. Here are the different values you can customize:

  • :background_color

  • :text_color

  • :sidebar_color

  • :sidebar_fill_color

  • :sidebar_border_color



398
399
400
401
402
403
404
405
406
407
# File 'lib/chirpy.rb', line 398

def update_profile_colors(colors)
  return unless colors.is_a?(Hash)
  post_data = {}
  
  colors.each_pair do |key, value|
    post_data.store("profile_#{key}", value.gsub(/#/, ''))
  end
  
  post "account/update_profile_colors", :post => post_data
end

#update_status(status) ⇒ Object

Updates the status of the authenticated user. Authentication required, silly.



175
176
177
# File 'lib/chirpy.rb', line 175

def update_status(status)
  post "statuses/update", :post => {:status => status}
end

#user_timeline(user = nil, params = {}) ⇒ Object

Gets a list of status updates from a specific user. If no user is supplied, the authenticated user will be used. you may supply a hash as the only argument. Authentication required.

Optional parameters:

  • :user_id

  • :screen_name

  • :since_id

  • :max_id

  • :count

  • :page



148
149
150
151
# File 'lib/chirpy.rb', line 148

def user_timeline(user = nil, params = {})
  args = [user, params]
  get path_from_args('statuses/user_timeline', args), params_from_args(args)
end

#verify_credentialsObject

Use this to check if an instance’s username and password are valid.

Authentication required.



356
357
358
359
360
361
362
363
# File 'lib/chirpy.rb', line 356

def verify_credentials
  if auth_supplied?
    response = get "account/verify_credentials"
    response.ok? ? response : false
  else
    false
  end
end