Class: Twitter::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(email, password, options = {}) ⇒ Base

Initializes the configuration for making requests to twitter Twitter example:

Twitter.new('email/username', 'password')

Identi.ca example:

Twitter.new('email/username', 'password', :api_host => 'identi.ca/api')


14
15
16
17
18
19
# File 'lib/twitter/base.rb', line 14

def initialize(email, password, options={})
  @api_host = options.delete(:api_host) || 'twitter.com'
  @config, @config[:email], @config[:password] = options, email, password
  @proxy_host = options[:proxy_host]
  @proxy_port = options[:proxy_port]
end

Instance Method Details

#block(id) ⇒ Object

Blocks the user specified by id for the auth user



165
166
167
# File 'lib/twitter/base.rb', line 165

def block(id)
  users(request("blocks/create/#{id}.xml", :auth => true, :method => :post)).first
end

#create_favorite(id) ⇒ Object

Favorites the status specified by id for the auth user



155
156
157
# File 'lib/twitter/base.rb', line 155

def create_favorite(id)
  statuses(request("favorites/create/#{id}.xml", :auth => true, :method => :post)).first
end

#create_friendship(id_or_screenname) ⇒ Object

Befriends id_or_screenname for the auth user



114
115
116
# File 'lib/twitter/base.rb', line 114

def create_friendship(id_or_screenname)
  users(request("friendships/create/#{id_or_screenname}.xml", :auth => true, :method => :post)).first
end

#d(user, text) ⇒ Object

Sends a direct message text to user



109
110
111
# File 'lib/twitter/base.rb', line 109

def d(user, text)
  DirectMessage.new_from_xml(request('direct_messages/new.xml', :auth => true, :method => :post, :form_data => {'text' => text, 'user' => user}))
end

#destroy(id) ⇒ Object

Destroys a status by id



77
78
79
# File 'lib/twitter/base.rb', line 77

def destroy(id)
  call("destroy/#{id}")
end

#destroy_direct_message(id) ⇒ Object

destroys a give direct message by id if the auth user is a recipient



104
105
106
# File 'lib/twitter/base.rb', line 104

def destroy_direct_message(id)
  DirectMessage.new_from_xml(request("direct_messages/destroy/#{id}.xml", :auth => true, :method => :post))
end

#destroy_favorite(id) ⇒ Object

Un-favorites the status specified by id for the auth user



160
161
162
# File 'lib/twitter/base.rb', line 160

def destroy_favorite(id)
  statuses(request("favorites/destroy/#{id}.xml", :auth => true, :method => :post)).first
end

#destroy_friendship(id_or_screenname) ⇒ Object

Defriends id_or_screenname for the auth user



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

def destroy_friendship(id_or_screenname)
  users(request("friendships/destroy/#{id_or_screenname}.xml", :auth => true, :method => :post)).first
end

#direct_messages(options = {}) ⇒ Object Also known as: received_messages

Returns an array of all the direct messages for the authenticated user



91
92
93
94
# File 'lib/twitter/base.rb', line 91

def direct_messages(options={})
  doc = request(build_path('direct_messages.xml', parse_options(options)), {:auth => true, :since => options[:since]})
  (doc/:direct_message).inject([]) { |dms, dm| dms << DirectMessage.new_from_xml(dm); dms }
end

#favorites(options = {}) ⇒ Object

Returns the most recent favorite statuses for the autenticating user



150
151
152
# File 'lib/twitter/base.rb', line 150

def favorites(options={})
  statuses(request(build_path('favorites.xml', parse_options(options)), :auth => true))
end

waiting for twitter to correctly implement this in the api as it is documented



86
87
88
# File 'lib/twitter/base.rb', line 86

def featured
  users(call(:featured))
end

#follow(id_or_screenname) ⇒ Object

Turns notifications by id_or_screenname on for auth user.



140
141
142
# File 'lib/twitter/base.rb', line 140

def follow(id_or_screenname)
  users(request("notifications/follow/#{id_or_screenname}.xml", :auth => true, :method => :post)).first
end

#follower_ids(id_or_screenname = nil) ⇒ Object

Returns an array of user ids who are followers for the account or the option id/username passed in



55
56
57
58
59
# File 'lib/twitter/base.rb', line 55

def follower_ids(id_or_screenname = nil)
  path = id_or_screenname ? "followers/ids/#{id_or_screenname}.xml" : "followers/ids.xml"
  doc = request(path, :auth => true)
  (doc/:id).inject([]) {|ids, id| ids << id.innerHTML; ids}
end

#followers(options = {}) ⇒ Object

Returns an array of users who are following you



46
47
48
# File 'lib/twitter/base.rb', line 46

def followers(options={})
  users(call(:followers, {:args => parse_options(options)}))
end

#followers_for(id, options = {}) ⇒ Object



50
51
52
# File 'lib/twitter/base.rb', line 50

def followers_for(id, options={})
  followers(options.merge({:id => id}))
end

#friend_ids(id_or_screenname = nil) ⇒ Object

Returns an array of user ids who are friends for the account or the option id/username passed in



39
40
41
42
43
# File 'lib/twitter/base.rb', line 39

def friend_ids(id_or_screenname = nil)
  path = id_or_screenname ? "friends/ids/#{id_or_screenname}.xml" : "friends/ids.xml"
  doc = request(path, :auth => true)
  (doc/:id).inject([]) {|ids, id| ids << id.innerHTML; ids}
end

#friends(options = {}) ⇒ Object

Returns an array of users who are in your friends list



29
30
31
# File 'lib/twitter/base.rb', line 29

def friends(options={})
  users(call(:friends, {:args => parse_options(options)}))
end

#friends_for(id, options = {}) ⇒ Object

Returns an array of users who are friends for the id or username passed in



34
35
36
# File 'lib/twitter/base.rb', line 34

def friends_for(id, options={})
  friends(options.merge({:id => id}))
end

#friendship_exists?(user_a, user_b) ⇒ Boolean

Returns true if friendship exists, false if it doesn’t.

Returns:

  • (Boolean)


124
125
126
127
# File 'lib/twitter/base.rb', line 124

def friendship_exists?(user_a, user_b)
  doc = request(build_path("friendships/exists.xml", {:user_a => user_a, :user_b => user_b}), :auth => true)
  doc.at('friends').innerHTML == 'true' ? true : false
end

#leave(id_or_screenname) ⇒ Object

Turns notifications by id_or_screenname off for auth user.



145
146
147
# File 'lib/twitter/base.rb', line 145

def leave(id_or_screenname)
  users(request("notifications/leave/#{id_or_screenname}.xml", :auth => true, :method => :post)).first
end

#post(status, options = {}) ⇒ Object Also known as: update

Posts a new update to twitter for auth user.



175
176
177
178
179
180
# File 'lib/twitter/base.rb', line 175

def post(status, options={})
  form_data = {'status' => status}
  form_data.merge!({'source' => options[:source]}) if options[:source]
  form_data.merge!({'in_reply_to_status_id' => options[:in_reply_to_status_id]}) if options[:in_reply_to_status_id]
  Status.new_from_xml(request('statuses/update.xml', :auth => true, :method => :post, :form_data => form_data))
end

#rate_limit_statusObject



81
82
83
# File 'lib/twitter/base.rb', line 81

def rate_limit_status
  RateLimitStatus.new_from_xml request("account/rate_limit_status.xml", :auth => true)
end

#replies(options = {}) ⇒ Object

Returns an array of statuses that are replies



72
73
74
# File 'lib/twitter/base.rb', line 72

def replies(options={})
  statuses(call(:replies, :since => options[:since], :args => parse_options(options)))
end

#sent_messages(options = {}) ⇒ Object

Returns direct messages sent by auth user



98
99
100
101
# File 'lib/twitter/base.rb', line 98

def sent_messages(options={})
  doc = request(build_path('direct_messages/sent.xml', parse_options(options)), {:auth => true, :since => options[:since]})
  (doc/:direct_message).inject([]) { |dms, dm| dms << DirectMessage.new_from_xml(dm); dms }
end

#status(id) ⇒ Object

Returns a single status for a given id



62
63
64
# File 'lib/twitter/base.rb', line 62

def status(id)
  statuses(call("show/#{id}")).first
end

#timeline(which = :friends, options = {}) ⇒ Object

Returns an array of statuses for a timeline; Defaults to your friends timeline.

Raises:



22
23
24
25
26
# File 'lib/twitter/base.rb', line 22

def timeline(which=:friends, options={})
  raise UnknownTimeline unless [:friends, :public, :user].include?(which)
  auth = which.to_s.include?('public') ? false : true
  statuses(call("#{which}_timeline", :auth => auth, :since => options[:since], :args => parse_options(options)))
end

#unblock(id) ⇒ Object

Unblocks the user specified by id for the auth user



170
171
172
# File 'lib/twitter/base.rb', line 170

def unblock(id)
  users(request("blocks/destroy/#{id}.xml", :auth => true, :method => :post)).first
end

#update_delivery_device(device) ⇒ Object

Updates your deliver device and returns Twitter::User object



135
136
137
# File 'lib/twitter/base.rb', line 135

def update_delivery_device(device)
  users(request(build_path('account/update_delivery_device.xml', {'device' => device}), :auth => true, :method => :post)).first
end

#update_location(location) ⇒ Object

Updates your location and returns Twitter::User object



130
131
132
# File 'lib/twitter/base.rb', line 130

def update_location(location)
  users(request(build_path('account/update_location.xml', {'location' => location}), :auth => true, :method => :post)).first
end

#user(id_or_screenname) ⇒ Object

returns all the profile information and the last status for a user



67
68
69
# File 'lib/twitter/base.rb', line 67

def user(id_or_screenname)
  users(request("users/show/#{id_or_screenname}.xml", :auth => true)).first
end

#verify_credentialsObject

Verifies the credentials for the auth user.

raises Twitter::CantConnect on failure.


185
186
187
# File 'lib/twitter/base.rb', line 185

def verify_credentials
  request('account/verify_credentials.xml', :auth => true)
end