twitter4r 0.2.5 → 0.3.0
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/lib/twitter/client/auth.rb +27 -0
- data/lib/twitter/client/base.rb +1 -0
- data/lib/twitter/client/favorites.rb +53 -0
- data/lib/twitter/client/messaging.rb +0 -1
- data/lib/twitter/client.rb +2 -0
- data/lib/twitter/version.rb +2 -2
- data/spec/twitter/client/auth_spec.rb +34 -0
- data/spec/twitter/client/favorites_spec.rb +183 -0
- metadata +6 -2
@@ -0,0 +1,27 @@
|
|
1
|
+
class Twitter::Client
|
2
|
+
@@AUTHENTICATION_URIS = {
|
3
|
+
:verify => '/account/verify_credentials',
|
4
|
+
}
|
5
|
+
|
6
|
+
# Provides access to the Twitter verify credentials API.
|
7
|
+
#
|
8
|
+
# You can verify Twitter user credentials with minimal overhead using this method.
|
9
|
+
#
|
10
|
+
# Example:
|
11
|
+
# client.authenticate?("osxisforlightweights", "l30p@rd_s^cks!")
|
12
|
+
def authenticate?(login, password)
|
13
|
+
verify_credentials(login, password)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def verify_credentials(username, passwd)
|
18
|
+
connection = create_http_connection
|
19
|
+
connection.start do |connection|
|
20
|
+
request = create_http_get_request("#{@@AUTHENTICATION_URIS[:verify]}.json")
|
21
|
+
request.basic_auth(username, passwd)
|
22
|
+
response = connection.request(request)
|
23
|
+
response.is_a?(Net::HTTPSuccess) ? true : false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/lib/twitter/client/base.rb
CHANGED
@@ -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
|
@@ -18,7 +18,6 @@ class Twitter::Client
|
|
18
18
|
# * +:received+
|
19
19
|
# * +:sent+
|
20
20
|
def messages(action, options = nil)
|
21
|
-
# method scoped method
|
22
21
|
def uri_suffix(opts); opts && opts[:page] ? "?page=#{opts[:page]}" : ""; end
|
23
22
|
raise ArgumentError, "Invalid messaging action: #{action}" unless [:sent, :received].member?(action)
|
24
23
|
uri = @@MESSAGING_URIS[action] + uri_suffix(options)
|
data/lib/twitter/client.rb
CHANGED
data/lib/twitter/version.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
describe Twitter::Client, "#authenticate?" do
|
4
|
+
before(:each) do
|
5
|
+
@uri = '/account/verify_credentials.json'
|
6
|
+
@request = mas_net_http_get(:basic_auth => nil)
|
7
|
+
@twitter = client_context
|
8
|
+
@default_header = @twitter.send(:http_header)
|
9
|
+
@response = mas_net_http_response(:success)
|
10
|
+
@error_response = mas_net_http_response(404, "Resource Not Found")
|
11
|
+
@connection = mas_net_http(@response)
|
12
|
+
Net::HTTP.stub!(:new).and_return(@connection)
|
13
|
+
@login = "applestillsucks"
|
14
|
+
@password = "linuxstillrocks"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "creates expected HTTP GET request" do
|
18
|
+
@twitter.should_receive(:create_http_get_request).with(@uri).and_return(@request)
|
19
|
+
@twitter.authenticate?(@login, @password)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return true if HTTP response is 20X" do
|
23
|
+
@twitter.authenticate?(@login, @password).should be(true)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return false if HTTP response is not 20X" do
|
27
|
+
Net::HTTP.stub!(:new).and_return(mas_net_http(@error_response))
|
28
|
+
@twitter.authenticate?(@login, @password).should be(false)
|
29
|
+
end
|
30
|
+
|
31
|
+
after(:each) do
|
32
|
+
nilize(@uri, @request, @twitter, @default_header, @response, @error_response, @connection, @login, @password)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
describe Twitter::Client, "#favorites" do
|
4
|
+
before(:each) do
|
5
|
+
@uri = '/favorites.json'
|
6
|
+
@request = mas_net_http_get(:basic_auth => nil)
|
7
|
+
@twitter = client_context
|
8
|
+
@default_header = @twitter.send(:http_header)
|
9
|
+
@response = mas_net_http_response(:success)
|
10
|
+
@connection = mas_net_http(@response)
|
11
|
+
@options = { :page => 4 }
|
12
|
+
Net::HTTP.stub!(:new).and_return(@connection)
|
13
|
+
@favorites = []
|
14
|
+
Twitter::Status.stub!(:unmarshal).and_return(@favorites)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should create expected HTTP GET request when not giving options" do
|
18
|
+
@twitter.should_receive(:create_http_get_request).with(@uri).and_return(@request)
|
19
|
+
@twitter.favorites
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should create expected HTTP GET request when giving :page options" do
|
23
|
+
@twitter.should_receive(:create_http_get_request).with("#{@uri}?#{@options.to_http_str}").and_return(@request)
|
24
|
+
@twitter.favorites(@options)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should raise Twitter::RESTError when 401 HTTP response received without giving options" do
|
28
|
+
@connection = mas_net_http(mas_net_http_response(:not_authorized))
|
29
|
+
lambda {
|
30
|
+
@twitter.favorites
|
31
|
+
}.should raise_error(Twitter::RESTError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should raise Twitter::RESTError when 401 HTTP response received when giving page options" do
|
35
|
+
@connection = mas_net_http(mas_net_http_response(:not_authorized))
|
36
|
+
lambda {
|
37
|
+
@twitter.favorites(@options)
|
38
|
+
}.should raise_error(Twitter::RESTError)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should raise Twitter::RESTError when 403 HTTP response received without giving options" do
|
42
|
+
@connection = mas_net_http(mas_net_http_response(:forbidden))
|
43
|
+
lambda {
|
44
|
+
@twitter.favorites
|
45
|
+
}.should raise_error(Twitter::RESTError)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should raise Twitter::RESTError when 403 HTTP response received when giving page options" do
|
49
|
+
@connection = mas_net_http(mas_net_http_response(:forbidden))
|
50
|
+
lambda {
|
51
|
+
@twitter.favorites(@options)
|
52
|
+
}.should raise_error(Twitter::RESTError)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should raise Twitter::RESTError when 500 HTTP response received without giving options" do
|
56
|
+
@connection = mas_net_http(mas_net_http_response(:server_error))
|
57
|
+
lambda {
|
58
|
+
@twitter.favorites
|
59
|
+
}.should raise_error(Twitter::RESTError)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should raise Twitter::RESTError when 500 HTTP response received when giving page options" do
|
63
|
+
@connection = mas_net_http(mas_net_http_response(:server_error))
|
64
|
+
lambda {
|
65
|
+
@twitter.favorites(@options)
|
66
|
+
}.should raise_error(Twitter::RESTError)
|
67
|
+
end
|
68
|
+
|
69
|
+
after(:each) do
|
70
|
+
nilize(@uri, @request, @twitter, @default_header, @response, @error_response, @connection)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
module FavoriteSpecMixin
|
75
|
+
def init
|
76
|
+
@base_uri = '/favourings'
|
77
|
+
@request = mas_net_http_get(:basic_auth => nil)
|
78
|
+
@twitter = client_context
|
79
|
+
@default_header = @twitter.send(:http_header)
|
80
|
+
@response = mas_net_http_response(:success)
|
81
|
+
@connection = mas_net_http(@response)
|
82
|
+
Net::HTTP.stub!(:new).and_return(@connection)
|
83
|
+
@id = 234923423
|
84
|
+
@status = mas_twitter_status(:id => @id, :to_i => @id)
|
85
|
+
Twitter::Status.stub!(:unmarshal).and_return(@status)
|
86
|
+
end
|
87
|
+
|
88
|
+
def create_uri(method, id)
|
89
|
+
"#{@base_uri}/#{method.to_s}/#{id.to_i.to_s}.json"
|
90
|
+
end
|
91
|
+
|
92
|
+
def connection=(connection)
|
93
|
+
@connection = connection
|
94
|
+
end
|
95
|
+
|
96
|
+
def finalize
|
97
|
+
nilize(@uri, @request, @twitter, @default_header, @response, @error_response, @connection)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "Twitter::Client#favorite error handling", :shared => true do
|
102
|
+
it "should raise a Twitter::RESTError exception when a 401 HTTP response is received" do
|
103
|
+
connection = mas_net_http(mas_net_http_response(:not_authorized))
|
104
|
+
lambda {
|
105
|
+
execute_method
|
106
|
+
}.should raise_error(Twitter::RESTError)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should raise a Twitter::RESTError exception when a 403 HTTP response is received" do
|
110
|
+
connection = mas_net_http(mas_net_http_response(:forbidden))
|
111
|
+
lambda {
|
112
|
+
execute_method
|
113
|
+
}.should raise_error(Twitter::RESTError)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should raise a Twitter::RESTError exception when a 404 HTTP response is received" do
|
117
|
+
connection = mas_net_http(mas_net_http_response(:file_not_found))
|
118
|
+
lambda {
|
119
|
+
execute_method
|
120
|
+
}.should raise_error(Twitter::RESTError)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should raise a Twitter::RESTError exception when a 500 HTTP response is received" do
|
124
|
+
connection = mas_net_http(mas_net_http_response(:server_error))
|
125
|
+
lambda {
|
126
|
+
execute_method
|
127
|
+
}.should raise_error(Twitter::RESTError)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe Twitter::Client, "#favorite(:add, status)" do
|
132
|
+
include FavoriteSpecMixin
|
133
|
+
it_should_behave_like "Twitter::Client#favorite error handling"
|
134
|
+
|
135
|
+
before(:each) do
|
136
|
+
init
|
137
|
+
end
|
138
|
+
|
139
|
+
def execute_method
|
140
|
+
@twitter.favorite(:add, @id)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should create expected POST request for :add action supplying integer id of status" do
|
144
|
+
@twitter.should_receive(:create_http_post_request).with(create_uri(:create, @id)).and_return(@request)
|
145
|
+
@twitter.favorite(:add, @id)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should create expected POST request for :add action supplying status object" do
|
149
|
+
@twitter.should_receive(:create_http_post_request).with(create_uri(:create, @id)).and_return(@request)
|
150
|
+
@twitter.favorite(:add, @status)
|
151
|
+
end
|
152
|
+
|
153
|
+
after(:each) do
|
154
|
+
finalize
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe Twitter::Client, "#favorite(:remove, status)" do
|
159
|
+
include FavoriteSpecMixin
|
160
|
+
it_should_behave_like "Twitter::Client#favorite error handling"
|
161
|
+
|
162
|
+
before(:each) do
|
163
|
+
init
|
164
|
+
end
|
165
|
+
|
166
|
+
def execute_method
|
167
|
+
@twitter.favorite(:remove, @id)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should create expected DELETE request for :remove action supplying integer id of status" do
|
171
|
+
@twitter.should_receive(:create_http_delete_request).with(create_uri(:destroy, @id)).and_return(@request)
|
172
|
+
@twitter.favorite(:remove, @id)
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should create expected DELETE request for :remove action supplying status object" do
|
176
|
+
@twitter.should_receive(:create_http_delete_request).with(create_uri(:destroy, @id)).and_return(@request)
|
177
|
+
@twitter.favorite(:remove, @status)
|
178
|
+
end
|
179
|
+
|
180
|
+
after(:each) do
|
181
|
+
finalize
|
182
|
+
end
|
183
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: twitter4r
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.3.0
|
7
|
+
date: 2007-11-05 00:00:00 -06:00
|
8
8
|
summary: A clean Twitter client API in pure Ruby. Will include Twitter add-ons also in Ruby.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -35,8 +35,10 @@ files:
|
|
35
35
|
- lib/twitter/client/timeline.rb
|
36
36
|
- lib/twitter/client/messaging.rb
|
37
37
|
- lib/twitter/client/base.rb
|
38
|
+
- lib/twitter/client/favorites.rb
|
38
39
|
- lib/twitter/client/status.rb
|
39
40
|
- lib/twitter/client/friendship.rb
|
41
|
+
- lib/twitter/client/auth.rb
|
40
42
|
- lib/twitter/client/user.rb
|
41
43
|
- lib/twitter/rails.rb
|
42
44
|
- lib/twitter/client.rb
|
@@ -52,9 +54,11 @@ files:
|
|
52
54
|
- spec/twitter/model_spec.rb
|
53
55
|
- spec/twitter/client/status_spec.rb
|
54
56
|
- spec/twitter/client/timeline_spec.rb
|
57
|
+
- spec/twitter/client/auth_spec.rb
|
55
58
|
- spec/twitter/client/user_spec.rb
|
56
59
|
- spec/twitter/client/messaging_spec.rb
|
57
60
|
- spec/twitter/client/base_spec.rb
|
61
|
+
- spec/twitter/client/favorites_spec.rb
|
58
62
|
- spec/twitter/client/friendship_spec.rb
|
59
63
|
- spec/twitter/version_spec.rb
|
60
64
|
- spec/twitter/rails_spec.rb
|