Class: Chirpy
- Inherits:
-
Object
- Object
- Chirpy
- Defined in:
- lib/chirpy.rb
Overview
Chirpy is a simple Twitter client for Ruby, written using RestClient and Hpricot.
Class Method Summary collapse
-
.decode_entities(string) ⇒ Object
Decodes HTML entities.
-
.public_timeline ⇒ Object
Gets the public timeline.
-
.remove_crap(string) ⇒ Object
Search results have bold tags and links in them.
-
.remove_tags(string) ⇒ Object
Removes tags.
-
.search(query, params = {}) ⇒ Object
Searches Twitter.
-
.test ⇒ Object
– Help methods.
-
.verify_credentials(username, password) ⇒ Object
Use this to check if a username and password are valid.
Instance Method Summary collapse
-
#authenticate(username, password) ⇒ Object
Tells Chirpy to use authentication.
-
#block(user) ⇒ Object
Makes the authenticated user block someone.
-
#block_exists(user_or_params) ⇒ Object
Checks if the authenticated user is blocking someone.
-
#blocking(params = {}) ⇒ Object
Returns a list of people the authenticated user is blocking.
-
#blocking_ids ⇒ Object
Returns a list of the ids of the people the authenticated user is blocking.
-
#create_favorite(id) ⇒ Object
Adds a tweet to the authenticated user’s favorites.
-
#create_friendship(user = nil, params = {}) ⇒ Object
Creates a friendship between authenticated user and another user.
-
#destroy_block(user) ⇒ Object
Removes the authenticated user’s block.
-
#destroy_favorite(id) ⇒ Object
Removes a tweet from the authenticated user’s favorites.
-
#destroy_friendship(user, params = {}) ⇒ Object
Destroys a friendship between the authenticated user and another user.
-
#destroy_status(status_id) ⇒ Object
Destroys one of the authenticated user’s tweets.
-
#direct_messages(params = {}) ⇒ Object
Gets a list of the messages sent to the authenticated user.
-
#direct_messages_new(recipient, text) ⇒ Object
Sends a direct message.
-
#direct_messages_sent(params = {}) ⇒ Object
Gets a list of the messages sent by the authenticated user.
-
#end_session ⇒ Object
Ends the session of the authenticated user.
-
#favorites(user = nil, params = {}) ⇒ Object
Gets a list of a user’s favorites.
-
#follow(user_or_params) ⇒ Object
Makes the authenticated user follow a new person.
-
#followers(user = nil, params = {}) ⇒ Object
Gets a list of a user’s followers.
-
#followers_ids(user = nil, params = {}) ⇒ Object
Returns ids for someone’s followers.
-
#friends(user = nil, params = {}) ⇒ Object
Gets a list of a user’s friends.
-
#friends_ids(user = nil, params = {}) ⇒ Object
Returns ids for someone’s friends.
-
#friends_timeline(params = {}) ⇒ Object
Gets the authenticated user’s friends’ timeline.
-
#friendship_exists?(user_a, user_b) ⇒ Boolean
Checks if a friendship exists between two users; returns true or false if no error occured.
-
#initialize(username = nil, password = nil) ⇒ Chirpy
constructor
Makes a new instance of Chirpy.
-
#leave(user_or_params) ⇒ Object
Unfollows a person the authenticated user is following.
-
#mentions(params = {}) ⇒ Object
Gets mentions for the authenticated user.
-
#public_timeline ⇒ Object
Instance method for public timeline.
-
#rate_limit_status(params) ⇒ Object
Gets information on rate limiting.
-
#show_status(status_id) ⇒ Object
Shows a specific tweet.
-
#show_user(user = nil, params = {}) ⇒ Object
Shows details for a specific user.
-
#unauthenticate ⇒ Object
Turns authentication off.
-
#update_delivery_device(device) ⇒ Object
Updates the authenticated user’s delivery device.
-
#update_profile(params) ⇒ Object
Updates the user’s profile information.
-
#update_profile_colors(colors) ⇒ Object
Updates the authenticated user’s colors (on their Twitter page).
-
#update_status(status) ⇒ Object
Updates the status of the authenticated user.
-
#user_timeline(user = nil, params = {}) ⇒ Object
Gets a list of status updates from a specific user.
-
#verify_credentials ⇒ Object
Use this to check if an instance’s username and password are valid.
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_timeline ⇒ Object
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) (decode_entities(string)) end |
.remove_tags(string) ⇒ Object
Removes tags.
85 86 87 |
# File 'lib/chirpy.rb', line 85 def self.(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 |
.test ⇒ Object
– Help methods
517 518 519 |
# File 'lib/chirpy.rb', line 517 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.
339 340 341 342 |
# File 'lib/chirpy.rb', line 339 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.
475 476 477 |
# File 'lib/chirpy.rb', line 475 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.
493 494 495 496 |
# File 'lib/chirpy.rb', line 493 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.
503 504 505 |
# File 'lib/chirpy.rb', line 503 def blocking(params = {}) get "blocks/blocking", params end |
#blocking_ids ⇒ Object
Returns a list of the ids of the people the authenticated user is blocking.
Authentication required.
511 512 513 |
# File 'lib/chirpy.rb', line 511 def blocking_ids get "blocks/blocking/ids" end |
#create_favorite(id) ⇒ Object
Adds a tweet to the authenticated user’s favorites.
Authentication required.
436 437 438 |
# File 'lib/chirpy.rb', line 436 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)
276 277 278 279 |
# File 'lib/chirpy.rb', line 276 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.
482 483 484 |
# File 'lib/chirpy.rb', line 482 def destroy_block(user) delete "blocks/destroy/#{user}" end |
#destroy_favorite(id) ⇒ Object
Removes a tweet from the authenticated user’s favorites
Authentication required, Strong Bad.
444 445 446 |
# File 'lib/chirpy.rb', line 444 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
289 290 291 292 |
# File 'lib/chirpy.rb', line 289 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 (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 (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 (params = {}) get "direct_messages/sent", params end |
#end_session ⇒ Object
Ends the session of the authenticated user
366 367 368 |
# File 'lib/chirpy.rb', line 366 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
427 428 429 430 |
# File 'lib/chirpy.rb', line 427 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
455 456 457 458 |
# File 'lib/chirpy.rb', line 455 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
329 330 331 332 |
# File 'lib/chirpy.rb', line 329 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
317 318 319 320 |
# File 'lib/chirpy.rb', line 317 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.
299 300 301 302 303 304 305 306 |
# File 'lib/chirpy.rb', line 299 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
465 466 467 468 |
# File 'lib/chirpy.rb', line 465 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_timeline ⇒ Object
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
360 361 362 |
# File 'lib/chirpy.rb', line 360 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 |
#unauthenticate ⇒ Object
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
375 376 377 |
# File 'lib/chirpy.rb', line 375 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.
413 414 415 |
# File 'lib/chirpy.rb', line 413 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
390 391 392 393 394 395 396 397 398 399 |
# File 'lib/chirpy.rb', line 390 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_credentials ⇒ Object
Use this to check if an instance’s username and password are valid.
Authentication required.
348 349 350 351 352 353 354 355 |
# File 'lib/chirpy.rb', line 348 def verify_credentials if auth_supplied? response = get "account/verify_credentials" response.ok? ? response : false else false end end |