craigtmackenzie-twitter4r 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +129 -0
- data/MIT-LICENSE +20 -0
- data/README +37 -0
- data/TODO +7 -0
- data/bin/t4rsh +80 -0
- data/lib/twitter.rb +34 -0
- data/lib/twitter/client.rb +24 -0
- data/lib/twitter/client/account.rb +24 -0
- data/lib/twitter/client/auth.rb +27 -0
- data/lib/twitter/client/base.rb +93 -0
- data/lib/twitter/client/blocks.rb +35 -0
- data/lib/twitter/client/favorites.rb +53 -0
- data/lib/twitter/client/friendship.rb +35 -0
- data/lib/twitter/client/graph.rb +37 -0
- data/lib/twitter/client/messaging.rb +79 -0
- data/lib/twitter/client/profile.rb +29 -0
- data/lib/twitter/client/search.rb +27 -0
- data/lib/twitter/client/status.rb +46 -0
- data/lib/twitter/client/timeline.rb +72 -0
- data/lib/twitter/client/user.rb +65 -0
- data/lib/twitter/config.rb +77 -0
- data/lib/twitter/console.rb +31 -0
- data/lib/twitter/core.rb +137 -0
- data/lib/twitter/ext.rb +2 -0
- data/lib/twitter/ext/stdlib.rb +52 -0
- data/lib/twitter/extras.rb +39 -0
- data/lib/twitter/meta.rb +56 -0
- data/lib/twitter/model.rb +356 -0
- data/lib/twitter/version.rb +19 -0
- data/spec/twitter/client/account_spec.rb +28 -0
- data/spec/twitter/client/auth_spec.rb +34 -0
- data/spec/twitter/client/base_spec.rb +242 -0
- data/spec/twitter/client/blocks_spec.rb +76 -0
- data/spec/twitter/client/favorites_spec.rb +183 -0
- data/spec/twitter/client/friendship_spec.rb +76 -0
- data/spec/twitter/client/graph_spec.rb +67 -0
- data/spec/twitter/client/messaging_spec.rb +135 -0
- data/spec/twitter/client/profile_spec.rb +91 -0
- data/spec/twitter/client/search_spec.rb +24 -0
- data/spec/twitter/client/status_spec.rb +92 -0
- data/spec/twitter/client/timeline_spec.rb +79 -0
- data/spec/twitter/client/user_spec.rb +203 -0
- data/spec/twitter/client_spec.rb +2 -0
- data/spec/twitter/config_spec.rb +86 -0
- data/spec/twitter/console_spec.rb +15 -0
- data/spec/twitter/core_spec.rb +127 -0
- data/spec/twitter/ext/stdlib_spec.rb +59 -0
- data/spec/twitter/extras_spec.rb +46 -0
- data/spec/twitter/meta_spec.rb +90 -0
- data/spec/twitter/model_spec.rb +487 -0
- data/spec/twitter/version_spec.rb +19 -0
- metadata +114 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
@@BLOCK_URIS = {
|
3
|
+
:add => '/blocks/create',
|
4
|
+
:remove => '/blocks/destroy',
|
5
|
+
}
|
6
|
+
|
7
|
+
# Provides access to the Twitter Block API.
|
8
|
+
#
|
9
|
+
# You can add and remove blocks to users using this method.
|
10
|
+
#
|
11
|
+
# <tt>action</tt> can be any of the following values:
|
12
|
+
# * <tt>:add</tt> - to add a block, you would use this <tt>action</tt> value
|
13
|
+
# * <tt>:remove</tt> - to remove a block use this.
|
14
|
+
#
|
15
|
+
# The <tt>value</tt> must be either the user screen name, integer unique user ID or Twitter::User
|
16
|
+
# object representation.
|
17
|
+
#
|
18
|
+
# Examples:
|
19
|
+
# screen_name = 'dictionary'
|
20
|
+
# client.block(:add, 'dictionary')
|
21
|
+
# client.block(:remove, 'dictionary')
|
22
|
+
# id = 1260061
|
23
|
+
# client.block(:add, id)
|
24
|
+
# client.block(:remove, id)
|
25
|
+
# user = Twitter::User.find(id, client)
|
26
|
+
# client.block(:add, user)
|
27
|
+
# client.block(:remove, user)
|
28
|
+
def block(action, value)
|
29
|
+
raise ArgumentError, "Invalid friend action provided: #{action}" unless @@BLOCK_URIS.keys.member?(action)
|
30
|
+
value = value.to_i unless value.is_a?(String)
|
31
|
+
uri = "#{@@BLOCK_URIS[action]}/#{value}.json"
|
32
|
+
response = http_connect {|conn| create_http_get_request(uri) }
|
33
|
+
bless_model(Twitter::User.unmarshal(response.body))
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
# Why Twitter.com developers can't correctly document their API, I do not know!
|
3
|
+
@@FAVORITES_URIS = {
|
4
|
+
:add => '/favourings/create',
|
5
|
+
:remove => '/favourings/destroy',
|
6
|
+
}
|
7
|
+
|
8
|
+
# Provides access to the Twitter list favorites API.
|
9
|
+
#
|
10
|
+
# You can access the authenticated [Twitter] user's favorites list using this method.
|
11
|
+
#
|
12
|
+
# By default you will receive the last twenty statuses added to your favorites list.
|
13
|
+
# To get a previous page you can provide options to this method. For example,
|
14
|
+
# statuses = client.favorites(:page => 2)
|
15
|
+
# The above one-liner will get the second page of favorites for the authenticated user.
|
16
|
+
def favorites(options = nil)
|
17
|
+
def uri_suffix(opts); opts && opts[:page] ? "?page=#{opts[:page]}" : ""; end
|
18
|
+
uri = '/favorites.json' + uri_suffix(options)
|
19
|
+
response = http_connect {|conn| create_http_get_request(uri) }
|
20
|
+
bless_models(Twitter::Status.unmarshal(response.body))
|
21
|
+
end
|
22
|
+
|
23
|
+
# Provides access to the Twitter add/remove favorite API.
|
24
|
+
#
|
25
|
+
# You can add and remove favorite status using this method.
|
26
|
+
#
|
27
|
+
# <tt>action</tt> can be any of the following values:
|
28
|
+
# * <tt>:add</tt> - to add a status to your favorites, you would use this <tt>action</tt> value
|
29
|
+
# * <tt>:remove</tt> - to remove an status from your existing favorites list use this.
|
30
|
+
#
|
31
|
+
# The <tt>value</tt> must be either the status object to add or remove or
|
32
|
+
# the integer unique status ID.
|
33
|
+
#
|
34
|
+
# Examples:
|
35
|
+
# id = 126006103423
|
36
|
+
# client.favorite(:add, id)
|
37
|
+
# client.favorite(:remove, id)
|
38
|
+
# status = Twitter::Status.find(id, client)
|
39
|
+
# client.favorite(:add, status)
|
40
|
+
# client.favorite(:remove, status)
|
41
|
+
def favorite(action, value)
|
42
|
+
raise ArgumentError, "Invalid favorite action provided: #{action}" unless @@FAVORITES_URIS.keys.member?(action)
|
43
|
+
value = value.to_i.to_s unless value.is_a?(String)
|
44
|
+
uri = "#{@@FAVORITES_URIS[action]}/#{value}.json"
|
45
|
+
case action
|
46
|
+
when :add
|
47
|
+
response = http_connect {|conn| create_http_post_request(uri) }
|
48
|
+
when :remove
|
49
|
+
response = http_connect {|conn| create_http_delete_request(uri) }
|
50
|
+
end
|
51
|
+
bless_model(Twitter::Status.unmarshal(response.body))
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
@@FRIENDSHIP_URIS = {
|
3
|
+
:add => '/friendships/create',
|
4
|
+
:remove => '/friendships/destroy',
|
5
|
+
}
|
6
|
+
|
7
|
+
# Provides access to the Twitter Friendship API.
|
8
|
+
#
|
9
|
+
# You can add and remove friends using this method.
|
10
|
+
#
|
11
|
+
# <tt>action</tt> can be any of the following values:
|
12
|
+
# * <tt>:add</tt> - to add a friend, you would use this <tt>action</tt> value
|
13
|
+
# * <tt>:remove</tt> - to remove an existing friend from your friends list use this.
|
14
|
+
#
|
15
|
+
# The <tt>value</tt> must be either the user to befriend or defriend's
|
16
|
+
# screen name, integer unique user ID or Twitter::User object representation.
|
17
|
+
#
|
18
|
+
# Examples:
|
19
|
+
# screen_name = 'dictionary'
|
20
|
+
# client.friend(:add, 'dictionary')
|
21
|
+
# client.friend(:remove, 'dictionary')
|
22
|
+
# id = 1260061
|
23
|
+
# client.friend(:add, id)
|
24
|
+
# client.friend(:remove, id)
|
25
|
+
# user = Twitter::User.find(id, client)
|
26
|
+
# client.friend(:add, user)
|
27
|
+
# client.friend(:remove, user)
|
28
|
+
def friend(action, value)
|
29
|
+
raise ArgumentError, "Invalid friend action provided: #{action}" unless @@FRIENDSHIP_URIS.keys.member?(action)
|
30
|
+
value = value.to_i unless value.is_a?(String)
|
31
|
+
uri = "#{@@FRIENDSHIP_URIS[action]}/#{value}.json"
|
32
|
+
response = http_connect {|conn| create_http_post_request(uri) }
|
33
|
+
bless_model(Twitter::User.unmarshal(response.body))
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
@@GRAPH_URIS = {
|
3
|
+
:friends => '/friends/ids',
|
4
|
+
:followers => '/followers/ids',
|
5
|
+
}
|
6
|
+
|
7
|
+
# Provides access to the Twitter Social Graphing API.
|
8
|
+
#
|
9
|
+
# You can retrieve the full graph of a user's friends or followers in one method call.
|
10
|
+
#
|
11
|
+
# <tt>action</tt> can be any of the following values:
|
12
|
+
# * <tt>:friends</tt> - retrieves ids of all friends of a given user.
|
13
|
+
# * <tt>:followers</tt> - retrieves ids of all followers of a given user.
|
14
|
+
#
|
15
|
+
# The <tt>value</tt> must be either the user screen name, integer unique user ID or Twitter::User
|
16
|
+
# object representation.
|
17
|
+
#
|
18
|
+
# Examples:
|
19
|
+
# screen_name = 'dictionary'
|
20
|
+
# client.graph(:friends, 'dictionary')
|
21
|
+
# client.graph(:followers, 'dictionary')
|
22
|
+
# id = 1260061
|
23
|
+
# client.graph(:friends, id)
|
24
|
+
# client.graph(:followers, id)
|
25
|
+
# user = Twitter::User.find(id, client)
|
26
|
+
# client.graph(:friends, user)
|
27
|
+
# client.graph(:followers, user)
|
28
|
+
def graph(action, value = nil)
|
29
|
+
raise ArgumentError, "Invalid friend action provided: #{action}" unless @@GRAPH_URIS.keys.member?(action)
|
30
|
+
id = value.to_i unless value.nil? || value.is_a?(String)
|
31
|
+
id ||= value
|
32
|
+
id ||= @login
|
33
|
+
uri = "#{@@GRAPH_URIS[action]}.json"
|
34
|
+
response = http_connect {|conn| create_http_get_request(uri, :id => id) }
|
35
|
+
JSON.parse(response.body)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
|
3
|
+
@@MESSAGING_URIS = {
|
4
|
+
:received => '/direct_messages.json',
|
5
|
+
:sent => '/direct_messages/sent.json',
|
6
|
+
:post => '/direct_messages/new.json',
|
7
|
+
:delete => '/direct_messages/destroy',
|
8
|
+
}
|
9
|
+
|
10
|
+
# Provides access to Twitter's Messaging API for received and
|
11
|
+
# sent direct messages.
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
# received_messages = @twitter.messages(:received)
|
15
|
+
#
|
16
|
+
# An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
|
17
|
+
# is given. Valid actions are:
|
18
|
+
# * +:received+
|
19
|
+
# * +:sent+
|
20
|
+
def messages(action, options = {})
|
21
|
+
raise ArgumentError, "Invalid messaging action: #{action}" unless [:sent, :received].member?(action)
|
22
|
+
uri = @@MESSAGING_URIS[action]
|
23
|
+
response = http_connect {|conn| create_http_get_request(uri, options) }
|
24
|
+
bless_models(Twitter::Message.unmarshal(response.body))
|
25
|
+
end
|
26
|
+
|
27
|
+
# Provides access to Twitter's Messaging API for sending and deleting
|
28
|
+
# direct messages to other users.
|
29
|
+
#
|
30
|
+
# <tt>action</tt> can be:
|
31
|
+
# * <tt>:post</tt> - to send a new direct message, <tt>value</tt>, to <tt>user</tt> given.
|
32
|
+
# * <tt>:delete</tt> - to delete direct message with message ID <tt>value</tt>.
|
33
|
+
#
|
34
|
+
# <tt>value</tt> should be:
|
35
|
+
# * <tt>String</tt> when action is <tt>:post</tt>. Will be the message text sent to given <tt>user</tt>.
|
36
|
+
# * <tt>Integer</tt> or <tt>Twitter::Message</tt> object when action is <tt>:delete</tt>. Will refer to the unique message ID to delete. When passing in an instance of <tt>Twitter::Message</tt> that Status will be
|
37
|
+
#
|
38
|
+
# <tt>user</tt> should be:
|
39
|
+
# * <tt>Twitter::User</tt>, <tt>Integer</tt> or <tt>String</tt> object when <tt>action</tt> is <tt>:post</tt>. The <tt>Integer</tt> must be the unique ID of the Twitter user you wish to send the direct message to and any <tt>String</tt>s passed in must be the screen name of the user you wish to send the direct message to.
|
40
|
+
# * totally ignore when <tt>action</tt> is <tt>:delete</tt>. It has no purpose in this use case scenario.
|
41
|
+
#
|
42
|
+
# Examples:
|
43
|
+
# The example below sends the message text 'Are you coming over at 6pm for the BBQ tonight?' to user with screen name 'myfriendslogin'...
|
44
|
+
# @twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', 'myfriendslogin')
|
45
|
+
# The example below sends the same message text as above to user with unique integer ID of 1234567890...
|
46
|
+
# the example below sends the same message text as above to user represented by <tt>user</tt> object instance of <tt>Twitter::User</tt>...
|
47
|
+
# @twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', user)
|
48
|
+
# message = @twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', 1234567890)
|
49
|
+
# the example below delete's the message send directly above to user with unique ID 1234567890...
|
50
|
+
# @twitter.message(:delete, message)
|
51
|
+
# Or the following can also be done...
|
52
|
+
# @twitter.message(:delete, message.id)
|
53
|
+
#
|
54
|
+
# In both scenarios (<tt>action</tt> is <tt>:post</tt> or
|
55
|
+
# <tt>:delete</tt>) a blessed <tt>Twitter::Message</tt> object is
|
56
|
+
# returned that represents the newly posted or newly deleted message.
|
57
|
+
#
|
58
|
+
# An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
|
59
|
+
# is given. Valid actions are:
|
60
|
+
# * +:post+
|
61
|
+
# * +:delete+
|
62
|
+
#
|
63
|
+
# An <tt>ArgumentError</tt> is also raised when no user argument is
|
64
|
+
# supplied when <tt>action</tt> is +:post+.
|
65
|
+
def message(action, value, user = nil)
|
66
|
+
raise ArgumentError, "Invalid messaging action: #{action}" unless [:post, :delete].member?(action)
|
67
|
+
raise ArgumentError, "User argument must be supplied for :post case" if action.eql?(:post) and user.nil?
|
68
|
+
uri = @@MESSAGING_URIS[action]
|
69
|
+
user = user.to_i if user and user.is_a?(Twitter::User)
|
70
|
+
case action
|
71
|
+
when :post
|
72
|
+
response = http_connect({:text => value, :user => user, :source => @@config.source}.to_http_str) {|conn| create_http_post_request(uri) }
|
73
|
+
when :delete
|
74
|
+
response = http_connect {|conn| create_http_delete_request(uri, :id => value.to_i) }
|
75
|
+
end
|
76
|
+
message = Twitter::Message.unmarshal(response.body)
|
77
|
+
bless_model(message)
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
@@PROFILE_URIS = {
|
3
|
+
:info => '/account/update_profile',
|
4
|
+
:colors => '/account/update_profile_colors',
|
5
|
+
:device => '/account/update_delivery_device',
|
6
|
+
}
|
7
|
+
|
8
|
+
# Provides access to the Twitter Profile API.
|
9
|
+
#
|
10
|
+
# You can update profile information. You can update the types of profile
|
11
|
+
# information:
|
12
|
+
# * :info (name, email, url, location, description)
|
13
|
+
# * :colors (background_color, text_color, link_color, sidebar_fill_color,
|
14
|
+
# sidebar_border_color)
|
15
|
+
# * :device (set device to either "sms", "im" or "none")
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
# user = client.profile(:info, :location => "University Library")
|
19
|
+
# puts user.inspect
|
20
|
+
def profile(action, attributes)
|
21
|
+
connection = create_http_connection
|
22
|
+
connection.start do |connection|
|
23
|
+
response = http_connect(attributes.to_http_str) do |conn|
|
24
|
+
create_http_post_request(@@PROFILE_URIS[action])
|
25
|
+
end
|
26
|
+
bless_models(Twitter::User.unmarshal(response.body))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
|
3
|
+
@@SEARCH_URIS = {
|
4
|
+
:basic => "/search.json",
|
5
|
+
}
|
6
|
+
|
7
|
+
# Provides access to Twitter's Search API.
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
# # For keyword search
|
11
|
+
# iterator = @twitter.search(:q => "coworking")
|
12
|
+
# while (tweet = iterator.next)
|
13
|
+
# puts tweet.text
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
|
17
|
+
# is given. Valid actions are:
|
18
|
+
# * +:received+
|
19
|
+
# * +:sent+
|
20
|
+
def search(options = {})
|
21
|
+
# raise ArgumentError, "Invalid messaging action: #{action}"
|
22
|
+
uri = @@SEARCH_URIS[:basic]
|
23
|
+
response = http_connect(nil, false, :search) {|conn| create_http_get_request(uri, options) }
|
24
|
+
json = JSON.parse(response.body)
|
25
|
+
bless_models(Twitter::Status.unmarshal(JSON.dump(json["results"])))
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
@@STATUS_URIS = {
|
3
|
+
:get => '/statuses/show.json',
|
4
|
+
:post => '/statuses/update.json',
|
5
|
+
:delete => '/statuses/destroy.json',
|
6
|
+
}
|
7
|
+
|
8
|
+
# Provides access to individual statuses via Twitter's Status APIs
|
9
|
+
#
|
10
|
+
# <tt>action</tt> can be of the following values:
|
11
|
+
# * <tt>:get</tt> to retrieve status content. Assumes <tt>value</tt> given responds to :to_i message in meaningful way to yield intended status id.
|
12
|
+
# * <tt>:post</tt> to publish a new status
|
13
|
+
# * <tt>:delete</tt> to remove an existing status. Assumes <tt>value</tt> given responds to :to_i message in meaningful way to yield intended status id.
|
14
|
+
#
|
15
|
+
# <tt>value</tt> should be set to:
|
16
|
+
# * the status identifier for <tt>:get</tt> case
|
17
|
+
# * the status text message for <tt>:post</tt> case
|
18
|
+
# * none necessary for <tt>:delete</tt> case
|
19
|
+
#
|
20
|
+
# Examples:
|
21
|
+
# twitter.status(:get, 107786772)
|
22
|
+
# twitter.status(:post, "New Ruby open source project Twitter4R version 0.2.0 released.")
|
23
|
+
# twitter.status(:delete, 107790712)
|
24
|
+
#
|
25
|
+
# An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
|
26
|
+
# is given. Valid actions are:
|
27
|
+
# * +:get+
|
28
|
+
# * +:post+
|
29
|
+
# * +:delete+
|
30
|
+
def status(action, value = nil)
|
31
|
+
return self.timeline_for(action, value || {}) if :replies == action
|
32
|
+
raise ArgumentError, "Invalid status action: #{action}" unless @@STATUS_URIS.keys.member?(action)
|
33
|
+
return nil unless value
|
34
|
+
uri = @@STATUS_URIS[action]
|
35
|
+
response = nil
|
36
|
+
case action
|
37
|
+
when :get
|
38
|
+
response = http_connect {|conn| create_http_get_request(uri, :id => value.to_i) }
|
39
|
+
when :post
|
40
|
+
response = http_connect({:status => value, :source => @@config.source}.to_http_str) {|conn| create_http_post_request(uri) }
|
41
|
+
when :delete
|
42
|
+
response = http_connect {|conn| create_http_delete_request(uri, :id => value.to_i) }
|
43
|
+
end
|
44
|
+
bless_model(Twitter::Status.unmarshal(response.body))
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
@@TIMELINE_URIS = {
|
3
|
+
:public => '/statuses/public_timeline.json',
|
4
|
+
:friends => '/statuses/friends_timeline.json',
|
5
|
+
:friend => '/statuses/friends_timeline.json',
|
6
|
+
:user => '/statuses/user_timeline.json',
|
7
|
+
:me => '/statuses/user_timeline.json',
|
8
|
+
:replies => '/statuses/replies.json',
|
9
|
+
}
|
10
|
+
|
11
|
+
# Provides access to Twitter's Timeline APIs
|
12
|
+
#
|
13
|
+
# Returns timeline for given <tt>type</tt>.
|
14
|
+
#
|
15
|
+
# <tt>type</tt> can take the following values:
|
16
|
+
# * <tt>public</tt>
|
17
|
+
# * <tt>friends</tt> or <tt>friend</tt>
|
18
|
+
# * <tt>user</tt> or <tt>me</tt>
|
19
|
+
#
|
20
|
+
# <tt>:id</tt> is on key applicable to be defined in </tt>options</tt>:
|
21
|
+
# * the id or screen name (aka login) for :friends
|
22
|
+
# * the id or screen name (aka login) for :user
|
23
|
+
# * meaningless for the :me case, since <tt>twitter.timeline_for(:user, 'mylogin')</tt> and <tt>twitter.timeline_for(:me)</tt> are the same assuming 'mylogin' is the authenticated user's screen name (aka login).
|
24
|
+
#
|
25
|
+
# Examples:
|
26
|
+
# # returns the public statuses since status with id of 6543210
|
27
|
+
# twitter.timeline_for(:public, id => 6543210)
|
28
|
+
# # returns the statuses for friend with user id 43210
|
29
|
+
# twitter.timeline_for(:friend, :id => 43210)
|
30
|
+
# # returns the statuses for friend with screen name (aka login) of 'otherlogin'
|
31
|
+
# twitter.timeline_for(:friend, :id => 'otherlogin')
|
32
|
+
# # returns the statuses for user with screen name (aka login) of 'otherlogin'
|
33
|
+
# twitter.timeline_for(:user, :id => 'otherlogin')
|
34
|
+
#
|
35
|
+
# <tt>options</tt> can also include the following keys:
|
36
|
+
# * <tt>:id</tt> is the user ID, screen name of Twitter::User representation of a <tt>Twitter</tt> user.
|
37
|
+
# * <tt>:since</tt> is a Time object specifying the date-time from which to return results for. Applicable for the :friend, :friends, :user and :me cases.
|
38
|
+
# * <tt>:count</tt> specifies the number of statuses to retrieve. Only applicable for the :user case.
|
39
|
+
# * <tt>since_id</tt> is the status id of the public timeline from which to retrieve statuses for <tt>:public</tt>. Only applicable for the :public case.
|
40
|
+
#
|
41
|
+
# You can also pass this method a block, which will iterate through the results
|
42
|
+
# of the requested timeline and apply the block logic for each status returned.
|
43
|
+
#
|
44
|
+
# Example:
|
45
|
+
# twitter.timeline_for(:public) do |status|
|
46
|
+
# puts status.user.screen_name, status.text
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# twitter.timeline_for(:friend, :id => 'myfriend', :since => 30.minutes.ago) do |status|
|
50
|
+
# puts status.user.screen_name, status.text
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# timeline = twitter.timeline_for(:me) do |status|
|
54
|
+
# puts status.text
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# An <tt>ArgumentError</tt> will be raised if an invalid <tt>type</tt>
|
58
|
+
# is given. Valid types are:
|
59
|
+
# * +:public+
|
60
|
+
# * +:friends+
|
61
|
+
# * +:friend+
|
62
|
+
# * +:user+
|
63
|
+
# * +:me+
|
64
|
+
def timeline_for(type, options = {}, &block)
|
65
|
+
raise ArgumentError, "Invalid timeline type: #{type}" unless @@TIMELINE_URIS.keys.member?(type)
|
66
|
+
uri = @@TIMELINE_URIS[type]
|
67
|
+
response = http_connect {|conn| create_http_get_request(uri, options) }
|
68
|
+
timeline = Twitter::Status.unmarshal(response.body)
|
69
|
+
timeline.each {|status| bless_model(status); yield status if block_given? }
|
70
|
+
timeline
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
@@USER_URIS = {
|
3
|
+
:info => '/users/show',
|
4
|
+
:friends => '/statuses/friends.json',
|
5
|
+
:followers => '/statuses/followers.json',
|
6
|
+
}
|
7
|
+
|
8
|
+
# Provides access to Twitter's User APIs
|
9
|
+
#
|
10
|
+
# Returns user instance for the <tt>id</tt> given. The <tt>id</tt>
|
11
|
+
# can either refer to the numeric user ID or the user's screen name.
|
12
|
+
#
|
13
|
+
# For example,
|
14
|
+
# @twitter.user(234943) #=> Twitter::User object instance for user with numeric id of 234943
|
15
|
+
# @twitter.user('mylogin') #=> Twitter::User object instance for user with screen name 'mylogin'
|
16
|
+
#
|
17
|
+
# Where <tt>options</tt> is a +Hash+ of options that can include:
|
18
|
+
# * <tt>:page</tt> - optional. Retrieves the next set of friends. There are 100 friends per page. Default: 1.
|
19
|
+
# * <tt>:lite</tt> - optional. Prevents the inline inclusion of current status. Default: false.
|
20
|
+
# * <tt>:since</tt> - optional. Only relevant for <tt>:friends</tt> action. Narrows the results to just those friends added after the date given as value of this option. Must be HTTP-formatted date.
|
21
|
+
#
|
22
|
+
# An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
|
23
|
+
# is given. Valid actions are:
|
24
|
+
# * +:info+
|
25
|
+
# * +:friends+
|
26
|
+
#
|
27
|
+
# +Note:+ You should not use this method to attempt to retrieve the
|
28
|
+
# authenticated user's followers. Please use any of the following
|
29
|
+
# ways of accessing this list:
|
30
|
+
# followers = client.my(:followers)
|
31
|
+
# OR
|
32
|
+
# followers = client.my(:info).followers
|
33
|
+
def user(id, action = :info, options = {})
|
34
|
+
raise ArgumentError, "Invalid user action: #{action}" unless @@USER_URIS.keys.member?(action)
|
35
|
+
id = id.to_i if id.is_a?(Twitter::User)
|
36
|
+
params = options.merge(:id => id)
|
37
|
+
response = http_connect {|conn| create_http_get_request(@@USER_URIS[action], params) }
|
38
|
+
bless_models(Twitter::User.unmarshal(response.body))
|
39
|
+
end
|
40
|
+
|
41
|
+
# Syntactic sugar for queries relating to authenticated user in Twitter's User API
|
42
|
+
#
|
43
|
+
# Where <tt>action</tt> is one of the following:
|
44
|
+
# * <tt>:info</tt> - Returns user instance for the authenticated user.
|
45
|
+
# * <tt>:friends</tt> - Returns Array of users that are authenticated user's friends
|
46
|
+
# * <tt>:followers</tt> - Returns Array of users that are authenticated user's followers
|
47
|
+
#
|
48
|
+
# Where <tt>options</tt> is a +Hash+ of options that can include:
|
49
|
+
# * <tt>:page</tt> - optional. Retrieves the next set of friends. There are 100 friends per page. Default: 1.
|
50
|
+
# * <tt>:lite</tt> - optional. Prevents the inline inclusion of current status. Default: false.
|
51
|
+
# * <tt>:since</tt> - optional. Only relevant for <tt>:friends</tt> action. Narrows the results to just those friends added after the date given as value of this option. Must be HTTP-formatted date.
|
52
|
+
#
|
53
|
+
# An <tt>ArgumentError</tt> will be raised if an invalid <tt>action</tt>
|
54
|
+
# is given. Valid actions are:
|
55
|
+
# * +:info+
|
56
|
+
# * +:friends+
|
57
|
+
# * +:followers+
|
58
|
+
def my(action, options = {})
|
59
|
+
raise ArgumentError, "Invalid user action: #{action}" unless @@USER_URIS.keys.member?(action)
|
60
|
+
params = options.merge(:id => @login)
|
61
|
+
response = http_connect {|conn| create_http_get_request(@@USER_URIS[action], params) }
|
62
|
+
users = Twitter::User.unmarshal(response.body)
|
63
|
+
bless_models(users)
|
64
|
+
end
|
65
|
+
end
|