partigirb 0.2.7 → 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/README.markdown CHANGED
@@ -12,23 +12,35 @@ There is also a [complete documentation of the API](http://partigi.pbworks.com/)
12
12
 
13
13
  ## Install
14
14
 
15
+ gem install oauth
15
16
  gem install partigirb -s http://gemcutter.org
16
17
 
17
18
  ## Usage
18
19
 
19
20
  ### Creating a client
20
21
 
21
- #### Without Authentication
22
+ Since Partigi started to require OAuth in all its API requests they support two authorization modes: the common OAuth specification ([http://oauth.net/core/1.0a/](http://oauth.net/core/1.0a/)) for read-write access and OAuth with a slightly modification for read-only access, which just consists of skiping the oauth_token when signing the requests.
22
23
 
23
- client = Partigirb::Client.new
24
+ Regardless of the access method used you will need to register your application and get a `consumer_key` and a `consumer_secret`, used in both access methods. ([http://www.partigi.com/applications](http://www.partigi.com/applications))
24
25
 
25
- #### With Authentication
26
+ #### Read-only access
26
27
 
27
- client = Partigirb::Client.new(:auth => {:login => 'SOMEUSERLOGIN', :api_secret => 'SECRETGENERATEDFORTHEUSER'})
28
-
29
- #### With specific API version
30
-
31
- client = Partigirb::Client.new(:api_version => 2)
28
+ client = Partigirb::Client.new('consumer_key', 'consumer_secret')
29
+
30
+ #### Read-write access
31
+
32
+ client = Partigirb::Client.new('consumer_key', 'consumer_secret')
33
+ client.set_callback_url('http://myapplication.test/partigi')
34
+ request_token = client.request_token
35
+
36
+ # At this point we must redirect the user to Partigi so he can authorize our application to access
37
+ # his profile, we can get the authorization url with request_token.authorize_url
38
+
39
+ # Once we are back Partigi will give us the parameter oauth_verifier, which we will use
40
+ # to get the access token
41
+ client.authorize_from_request(request_token.token, request_token.secret, oauth_verifier)
42
+
43
+ # Now our client is fully authorized to call write methods for the user
32
44
 
33
45
  ### Request methods
34
46
 
@@ -100,6 +112,12 @@ There are two special cases to be aware of in regard to PartigiStruct:
100
112
 
101
113
  In case Partigi returns an error response, this is turned into a PartigiError object which message attribute is set to the error string returned in the XML response.
102
114
 
115
+ ### Verifying our credentials
116
+
117
+ A way to check if our client is correctly authorized for write access or to just get the current user's information is by using the verify_credentials method, which will return either the PartigiStruct object with user's information or a PartigiError object in case of authorization failure.
118
+
119
+ client.verify_credentials
120
+
103
121
  ## Requirements
104
122
 
105
123
  - json
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.7
1
+ 0.3.0
@@ -1,5 +1,8 @@
1
1
  require File.dirname(__FILE__) + '/../lib/partigirb'
2
2
 
3
+ CONSUMER_KEY = "your_consumer_key"
4
+ CONSUMER_SECRET = "your_consumer_secret"
5
+
3
6
  if ARGV.empty?
4
7
  puts "\nUsage: ruby #{__FILE__} user_id_or_login\n\n"
5
8
  exit
@@ -14,15 +17,15 @@ def show_reviews(client, reviews, title)
14
17
  puts
15
18
 
16
19
  reviews.each do |review|
17
- film = client.items.show? :id => review.ptItem_id, :type => 'film'
18
- puts "- #{film.title}"
20
+ item = client.items.show? :id => review.ptItem_id, :item_type => review.ptItem_type
21
+ puts "- #{item.title}"
19
22
  puts " Comment: #{review.content.text}"
20
23
  puts
21
24
  end
22
25
  puts
23
26
  end
24
27
 
25
- client = Partigirb::Client.new
28
+ client = Partigirb::Client.new(CONSUMER_KEY, CONSUMER_SECRET)
26
29
 
27
30
  reviews = client.reviews.index? :user_id => user_id, :per_page => 5, :status => 0, :order => 'desc'
28
31
  show_reviews(client, reviews, "Latest 5 films you want to watch")
@@ -1,5 +1,8 @@
1
1
  require File.dirname(__FILE__) + '/../lib/partigirb'
2
2
 
3
+ CONSUMER_KEY = "your_consumer_key"
4
+ CONSUMER_SECRET = "your_consumer_secret"
5
+
3
6
  if ARGV.empty?
4
7
  puts "\nUsage: #{__FILE__} user_id_or_login\n\n"
5
8
  exit
@@ -7,7 +10,7 @@ end
7
10
 
8
11
  user_id = ARGV.first
9
12
 
10
- client = Partigirb::Client.new
13
+ client = Partigirb::Client.new(CONSUMER_KEY, CONSUMER_SECRET)
11
14
 
12
15
  traitors = []
13
16
 
@@ -46,28 +46,22 @@ module Partigirb
46
46
  PARTIGI_API_HOST = "www.partigi.com"
47
47
  TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
48
48
 
49
- attr_accessor :default_format, :headers, :api_version, :transport, :request, :api_host, :auth, :handlers
50
-
51
- def initialize(options={})
52
- self.transport = Transport.new
53
- self.api_host = PARTIGI_API_HOST.clone
54
- self.api_version = options[:api_version] || Partigirb::CURRENT_API_VERSION
55
- self.headers = {"User-Agent"=>"Partigirb/#{Partigirb::VERSION}"}.merge!(options[:headers]||{})
56
- self.default_format = options[:default_format] || :atom
57
- self.handlers = {
49
+ attr_accessor :consumer_key, :consumer_secret, :default_format, :headers, :api_version, :transport, :request, :api_host, :auth, :handlers, :access_token
50
+
51
+ def initialize(consumer_key, consumer_secret, options={})
52
+ @consumer = ::OAuth::Consumer.new(consumer_key, consumer_secret, {:site => "http://#{PARTIGI_API_HOST}"})
53
+ @transport = Transport.new(@consumer)
54
+ @api_host = PARTIGI_API_HOST.clone
55
+ @api_version = options[:api_version] || Partigirb::CURRENT_API_VERSION
56
+ @headers = {"User-Agent"=>"Partigirb/#{Partigirb::VERSION}"}.merge!(options[:headers]||{})
57
+ @default_format = options[:default_format] || :atom
58
+ @handlers = {
58
59
  :json => Partigirb::Handlers::JSONHandler.new,
59
60
  :xml => Partigirb::Handlers::XMLHandler.new,
60
61
  :atom => Partigirb::Handlers::AtomHandler.new,
61
62
  :unknown => Partigirb::Handlers::StringHandler.new
62
63
  }
63
- self.handlers.merge!(options[:handlers]||{})
64
-
65
- # Authentication param should be a hash with keys:
66
- # login (required)
67
- # api_secret (required)
68
- # nonce (optional, would be automatically generated if missing)
69
- # timestamp (optional, current timestamp will be automatically used if missing)
70
- self.auth = options[:auth]
64
+ @handlers.merge!(options[:handlers]) if options[:handlers]
71
65
  end
72
66
 
73
67
  def method_missing(name,*args)
@@ -101,6 +95,43 @@ module Partigirb
101
95
  @request ||= Request.new(self,api_version)
102
96
  end
103
97
 
98
+ # OAuth related methods
99
+
100
+ # Note: If using oauth with a web app, be sure to provide :oauth_callback.
101
+ def request_token(options={})
102
+ @request_token ||= consumer.get_request_token(options)
103
+ end
104
+
105
+ def set_callback_url(url)
106
+ clear_request_token
107
+ request_token(:oauth_callback => url)
108
+ end
109
+
110
+ # For web apps use params[:oauth_verifier], for desktop apps,
111
+ # use the verifier is the pin that twitter gives users.
112
+ def authorize_from_request(request_token, request_secret, verifier_or_pin)
113
+ request_token = OAuth::RequestToken.new(consumer, request_token, request_secret)
114
+ @access_token = request_token.get_access_token(:oauth_verifier => verifier_or_pin)
115
+ end
116
+
117
+ def consumer
118
+ @consumer
119
+ end
120
+
121
+ def access_token
122
+ @access_token
123
+ end
124
+
125
+ def authorize_from_access(token, secret)
126
+ @access_token = OAuth::AccessToken.new(consumer, token, secret)
127
+ end
128
+
129
+ # Shortcut methods
130
+
131
+ def verify_credentials
132
+ account.verify_credentials?
133
+ end
134
+
104
135
  protected
105
136
 
106
137
  def call_with_format(format,params={})
@@ -113,11 +144,9 @@ module Partigirb
113
144
 
114
145
  def send_request(params)
115
146
  begin
116
- set_authentication_headers
117
-
118
- transport.request(
119
- request.method, request.url, :headers=>headers, :params=>params
120
- )
147
+ options = {:headers=>headers, :params=>params}
148
+ options.merge!(:access_token => @access_token) unless @access_token.nil?
149
+ transport.request(request.method, request.url, options)
121
150
  rescue => e
122
151
  puts e
123
152
  end
@@ -128,19 +157,15 @@ module Partigirb
128
157
 
129
158
  begin
130
159
  if res.code.to_i != 200
131
- handle_error_response(res, Partigirb::Handlers::XMLHandler.new)
160
+ handle_error_response(res)
132
161
  else
133
162
  fmt_handler.decode_response(res.body)
134
163
  end
135
164
  end
136
165
  end
137
166
 
138
- # TODO: Test for errors
139
- def handle_error_response(res, handler)
140
- # Response for errors is an XML document containing an error tag as a root,
141
- # having a text node with error name. As XMLHandler starts building the Struct
142
- # on root node the returned value from the handler will always be the error name text.
143
- raise PartigiError.new(handler.decode_response(res.body))
167
+ def handle_error_response(res)
168
+ raise PartigiError.new(res.body)
144
169
  end
145
170
 
146
171
  def format_invocation?(name)
@@ -151,40 +176,8 @@ module Partigirb
151
176
  handlers[format] || handlers[:unknown]
152
177
  end
153
178
 
154
- # Adds the proper WSSE headers if there are the right authentication parameters
155
- def set_authentication_headers
156
- unless self.auth.nil? || self.auth === Hash || self.auth.empty?
157
- auths = self.auth.stringify_keys
158
-
159
- if auths.has_key?('login') && auths.has_key?('api_secret')
160
- if !auths['timestamp'].nil?
161
- timestamp = case auths['timestamp']
162
- when Time
163
- auths['timestamp'].strftime(TIMESTAMP_FORMAT)
164
- when String
165
- auths['timestamp']
166
- end
167
- else
168
- timestamp = Time.now.strftime(TIMESTAMP_FORMAT) if timestamp.nil?
169
- end
170
-
171
- nonce = auths['nonce'] || generate_nonce
172
- password_digest = generate_password_digest(nonce, timestamp, auths['login'], auths['api_secret'])
173
- headers.merge!({
174
- 'Authorization' => "WSSE realm=\"#{PARTIGI_API_HOST}\", profile=\"UsernameToken\"",
175
- 'X-WSSE' => "UsernameToken Username=\"#{auths['login']}\", PasswordDigest=\"#{password_digest}\", Nonce=\"#{nonce}\", Created=\"#{timestamp}\""
176
- })
177
- end
178
- end
179
- end
180
-
181
- def generate_nonce
182
- o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
183
- Digest::MD5.hexdigest((0..10).map{o[rand(o.length)]}.join)
184
- end
185
-
186
- def generate_password_digest(nonce, timestamp, login, secret)
187
- Base64.encode64(Digest::SHA1.hexdigest("#{nonce}#{timestamp}#{login}#{secret}")).chomp
179
+ def clear_request_token
180
+ @request_token = nil
188
181
  end
189
182
  end
190
183
  end
@@ -13,8 +13,12 @@ module Partigirb
13
13
 
14
14
  class Transport
15
15
 
16
- attr_accessor :debug
17
-
16
+ attr_accessor :debug, :consumer
17
+
18
+ def initialize(consumer)
19
+ @consumer = consumer
20
+ end
21
+
18
22
  CRLF = "\r\n"
19
23
 
20
24
  def req_class(method)
@@ -44,23 +48,25 @@ module Partigirb
44
48
  end
45
49
 
46
50
  def execute_request(method,url,options={})
47
- conn = Net::HTTP.new(url.host, url.port)
48
- #conn.use_ssl = (url.scheme == 'https')
49
- conn.start do |http|
50
- req = req_class(method).new(url.request_uri)
51
-
52
- add_headers(req,options[:headers])
53
- if file_param?(options[:params])
54
- add_multipart_data(req,options[:params])
55
- else
56
- add_form_data(req,options[:params])
57
- end
58
-
59
- dump_request(req) if debug
60
- res = http.request(req)
61
- dump_response(res) if debug
62
- res
51
+ http = Net::HTTP.new(url.host, url.port)
52
+
53
+ req = req_class(method).new(url.request_uri)
54
+ add_headers(req,options[:headers])
55
+ if file_param?(options[:params])
56
+ add_multipart_data(req,options[:params])
57
+ else
58
+ add_form_data(req,options[:params])
63
59
  end
60
+
61
+ # Sign the request with OAuth
62
+ # when there is no options[:access_token] the request is signed without oauth_token
63
+ # so it will be a Partigi OAuth Readonly request
64
+ req.oauth!(http, @consumer, options[:access_token])
65
+
66
+ dump_request(req) if debug
67
+ res = http.request(req)
68
+ dump_response(res) if debug
69
+ res
64
70
  end
65
71
 
66
72
  def query_string(params)
data/lib/partigirb.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Partigirb
2
- VERSION='0.1.0'
2
+ VERSION='0.3.0'
3
3
  CURRENT_API_VERSION=1
4
4
  end
5
5
 
@@ -14,6 +14,7 @@ require 'digest'
14
14
  require 'rexml/document'
15
15
  require 'mime/types'
16
16
  require 'ostruct'
17
+ require 'oauth'
17
18
 
18
19
  require 'partigirb/core_ext'
19
20
 
@@ -23,5 +24,4 @@ require 'partigirb/handlers/json_handler'
23
24
  require 'partigirb/handlers/string_handler'
24
25
 
25
26
  require 'partigirb/transport'
26
- require 'partigirb/client'
27
-
27
+ require 'partigirb/client'
data/partigirb.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{partigirb}
8
- s.version = "0.2.7"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alvaro Bautista", "Fernando Blat"]
12
- s.date = %q{2009-10-28}
12
+ s.date = %q{2010-02-10}
13
13
  s.email = ["alvarobp@gmail.com", "ferblape@gmail.com"]
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -73,4 +73,3 @@ Gem::Specification.new do |s|
73
73
  else
74
74
  end
75
75
  end
76
-
data/test/client_test.rb CHANGED
@@ -88,71 +88,6 @@ class ClientTest < Test::Unit::TestCase
88
88
  assert_equal 'header', Net::HTTP.request['Fake']
89
89
  end
90
90
 
91
- should "add authentication headers when login and secret are provided" do
92
- @client = new_client(200, '', :auth => {:login => 'auser', :api_secret => 'his_api_secret'})
93
-
94
- @client.friendships.update! :id => 321
95
-
96
- assert_not_nil Net::HTTP.request['Authorization']
97
- assert_equal "WSSE realm=\"#{Partigirb::Client::PARTIGI_API_HOST}\", profile=\"UsernameToken\"", Net::HTTP.request['Authorization']
98
-
99
- assert_not_nil Net::HTTP.request['X-WSSE']
100
- assert_match /UsernameToken Username="auser", PasswordDigest="[^"]+", Nonce="[^"]+", Created="[^"]+"/, Net::HTTP.request['X-WSSE']
101
- end
102
-
103
- should "not add authentication headers if no auth params are provided" do
104
- @client.friendships.update! :id => 321
105
-
106
- assert_nil Net::HTTP.request['Authorization']
107
- assert_nil Net::HTTP.request['X-WSSE']
108
- end
109
-
110
- should "use given nonce for authentication" do
111
- @client = new_client(200, '', :auth => {:login => 'auser', :api_secret => 'his_api_secret', :nonce => '123456789101112'})
112
- @client.friendships.update! :id => 321
113
-
114
- assert_equal "WSSE realm=\"#{Partigirb::Client::PARTIGI_API_HOST}\", profile=\"UsernameToken\"", Net::HTTP.request['Authorization']
115
- assert_match /UsernameToken Username="auser", PasswordDigest="[^"]+", Nonce="123456789101112", Created="[^"]+"/, Net::HTTP.request['X-WSSE']
116
- end
117
-
118
- should "use given timestamp string for authentication" do
119
- @client = new_client(200, '', :auth => {:login => 'auser', :api_secret => 'his_api_secret', :timestamp => '2009-07-15T14:43:07Z'})
120
- @client.friendships.update! :id => 321
121
-
122
- assert_equal "WSSE realm=\"#{Partigirb::Client::PARTIGI_API_HOST}\", profile=\"UsernameToken\"", Net::HTTP.request['Authorization']
123
- assert_match /UsernameToken Username="auser", PasswordDigest="[^"]+", Nonce="[^"]+", Created="2009-07-15T14:43:07Z"/, Net::HTTP.request['X-WSSE']
124
- end
125
-
126
- should "use given Time object as timestamp for authentication" do
127
- timestamp = Time.now
128
- @client = new_client(200, '', :auth => {:login => 'auser', :api_secret => 'his_api_secret', :timestamp => timestamp})
129
- @client.friendships.update! :id => 321
130
-
131
- assert_equal "WSSE realm=\"#{Partigirb::Client::PARTIGI_API_HOST}\", profile=\"UsernameToken\"", Net::HTTP.request['Authorization']
132
- assert_match /UsernameToken Username="auser", PasswordDigest="[^"]+", Nonce="[^"]+", Created="#{timestamp.strftime(Partigirb::Client::TIMESTAMP_FORMAT)}"/, Net::HTTP.request['X-WSSE']
133
- end
134
-
135
- should "use the PasswordDigest from given parameters" do
136
- @client = new_client(200, '', :auth => {:login => 'auser', :api_secret => 'his_api_secret', :nonce => '123456789101112', :timestamp => '2009-07-15T14:43:07Z'})
137
- @client.friendships.update! :id => 321
138
-
139
- pdigest = Base64.encode64(Digest::SHA1.hexdigest("1234567891011122009-07-15T14:43:07Zauserhis_api_secret")).chomp
140
-
141
- assert_equal "WSSE realm=\"#{Partigirb::Client::PARTIGI_API_HOST}\", profile=\"UsernameToken\"", Net::HTTP.request['Authorization']
142
- assert_match /UsernameToken Username="auser", PasswordDigest="#{pdigest}", Nonce="123456789101112", Created="2009-07-15T14:43:07Z"/, Net::HTTP.request['X-WSSE']
143
- end
144
-
145
- context "generate_nonce method" do
146
- should "generate random strings" do
147
- @client.instance_eval do
148
- nonces = []
149
- 1.upto(25) do
150
- assert !nonces.include?(generate_nonce)
151
- end
152
- end
153
- end
154
- end
155
-
156
91
  should "process XML response by XML handler" do
157
92
  Partigirb::Handlers::XMLHandler.any_instance.expects(:decode_response).once
158
93
  Partigirb::Handlers::AtomHandler.any_instance.expects(:decode_response).never
@@ -175,14 +110,88 @@ class ClientTest < Test::Unit::TestCase
175
110
  end
176
111
 
177
112
  should "raise a PartigiError with response error text as the message when http response codes are other than 200" do
178
- client = new_client(400, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>Partigi::BadAPIRequestRequiredParams</error>\n")
179
-
180
- begin
181
- client.items.show.xml :id => 'madeup'
182
- rescue Exception => e
183
- assert e.is_a?(Partigirb::PartigiError)
184
- assert_equal 'Partigi::BadAPIRequestRequiredParams', e.message
185
- end
113
+ client = new_client(400, "Partigi::BadAPIRequestRequiredParams")
114
+
115
+ begin
116
+ client.items.show.xml :id => 'madeup'
117
+ rescue Exception => e
118
+ assert e.is_a?(Partigirb::PartigiError)
119
+ assert_equal 'Partigi::BadAPIRequestRequiredParams', e.message
120
+ end
121
+ end
122
+
123
+ should "initialize with OAuth consumer key and secret" do
124
+ client = Partigirb::Client.new('my_consumer_key', 'my_consumer_secret')
125
+
126
+ assert client.consumer.is_a?(OAuth::Consumer)
127
+ assert_equal 'my_consumer_key', client.consumer.key
128
+ assert_equal 'my_consumer_secret', client.consumer.secret
129
+ end
130
+
131
+ should "get a request token from the consumer" do
132
+ consumer = mock('oauth consumer')
133
+ request_token = mock('request token')
134
+ OAuth::Consumer.expects(:new).with('my_consumer_key', 'my_consumer_secret', {:site => 'http://www.partigi.com'}).returns(consumer)
135
+ client = Partigirb::Client.new('my_consumer_key', 'my_consumer_secret')
136
+
137
+ consumer.expects(:get_request_token).returns(request_token)
138
+
139
+ assert_equal request_token, client.request_token
140
+ end
141
+
142
+ should "clear request token and set the callback url" do
143
+ consumer = mock('oauth consumer')
144
+ request_token = mock('request token')
145
+ OAuth::Consumer.expects(:new).with('my_consumer_key', 'my_consumer_secret', {:site => 'http://www.partigi.com'}).returns(consumer)
146
+ client = Partigirb::Client.new('my_consumer_key', 'my_consumer_secret')
147
+
148
+ client.expects(:clear_request_token).once
149
+ consumer.expects(:get_request_token).with({:oauth_callback => 'http://testing.com/oauth_callback'}).returns(request_token)
150
+
151
+ client.set_callback_url('http://testing.com/oauth_callback')
152
+ end
153
+
154
+ should "create and set access token from request token, request secret and verifier" do
155
+ client = Partigirb::Client.new('my_consumer_key', 'my_consumer_secret')
156
+ consumer = OAuth::Consumer.new('my_consumer_key', 'my_consumer_secret', {:site => 'http://www.partigi.com'})
157
+ client.stubs(:consumer).returns(consumer)
158
+
159
+ access_token = mock('access token')
160
+ request_token = mock('request token')
161
+ request_token.expects(:get_access_token).with(:oauth_verifier => 'verify_me').returns(access_token)
162
+
163
+ OAuth::RequestToken.expects(:new).with(consumer, 'the_request_token', 'the_request_secret').returns(request_token)
164
+
165
+ client.authorize_from_request('the_request_token', 'the_request_secret', 'verify_me')
166
+ assert_equal access_token, client.access_token
167
+ end
168
+
169
+ should "create and set access token from access token and secret" do
170
+ client = Partigirb::Client.new('my_consumer_key', 'my_consumer_secret')
171
+ consumer = OAuth::Consumer.new('my_consumer_key', 'my_consumer_secret', {:site => 'http://www.partigi.com'})
172
+ client.stubs(:consumer).returns(consumer)
173
+
174
+ client.authorize_from_access('the_access_token', 'the_access_secret')
175
+ assert client.access_token.is_a?(OAuth::AccessToken)
176
+ assert_equal 'the_access_token', client.access_token.token
177
+ assert_equal 'the_access_secret', client.access_token.secret
178
+ end
179
+
180
+ should "pass access token on Transport requests when available" do
181
+ access_token = mock('access token')
182
+ OAuth::AccessToken.expects(:new).with(anything, 'the_access_token', 'the_access_secret').returns(access_token)
183
+ @client.transport.expects(:request).with(anything, anything, has_entries({:access_token => access_token})).returns(MockResponse.new(200,''))
184
+ @client.authorize_from_access('the_access_token', 'the_access_secret')
185
+ @client.user.show.xml
186
+ end
187
+
188
+ context "verify_credentials method" do
189
+ should "make a request to /account/verify_credentials" do
190
+ @client = new_client(200, '')
191
+ @client.verify_credentials
192
+
193
+ assert_equal '/api/v1/account/verify_credentials.atom', @client.transport.url.path
194
+ end
186
195
  end
187
196
 
188
197
  # Copied from Grackle
@@ -2,8 +2,9 @@
2
2
  class MockTransport < Partigirb::Transport
3
3
  attr_accessor :status, :body, :method, :url, :options
4
4
 
5
- def initialize(status,body,headers={})
5
+ def initialize(consumer,status,body,headers={})
6
6
  Net::HTTP.response = MockResponse.new(status,body,headers)
7
+ super(consumer)
7
8
  end
8
9
 
9
10
  def request(method, string_url, options)
data/test/test_helper.rb CHANGED
@@ -13,8 +13,8 @@ Dir.glob('test/mocks/*_mock.rb').each { |e| require e }
13
13
 
14
14
  class Test::Unit::TestCase
15
15
  def new_client(status=200, response_body='', client_opts={})
16
- client = Partigirb::Client.new(client_opts)
17
- client.transport = MockTransport.new(status,response_body)
16
+ client = Partigirb::Client.new('prb_consumer_key', 'prb_consumer_secret', client_opts)
17
+ client.transport = MockTransport.new(client.consumer,status,response_body)
18
18
  client
19
19
  end
20
20
 
@@ -1,8 +1,25 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
  require 'net/http'
3
3
 
4
- class RequestTest < Test::Unit::TestCase
5
- def test_nothing
6
- assert true
4
+ class TransportTest < Test::Unit::TestCase
5
+ should "sign request using OAuth without access token by default" do
6
+ consumer = ::OAuth::Consumer.new('consumer_key', 'this_is_secret')
7
+ transport = Partigirb::Transport.new(consumer)
8
+
9
+ Net::HTTP::Get.any_instance.expects(:oauth!).with(anything, consumer, nil).once
10
+ Net::HTTP.any_instance.expects(:request).once
11
+
12
+ transport.request :get, "http://test.host"
7
13
  end
8
- end
14
+
15
+ should "sign request using OAuth with the given Access Token" do
16
+ consumer = ::OAuth::Consumer.new('consumer_key', 'this_is_secret')
17
+ access_token = ::OAuth::AccessToken.new('access_token', 'access_secret')
18
+ transport = Partigirb::Transport.new(consumer)
19
+
20
+ Net::HTTP::Get.any_instance.expects(:oauth!).with(anything, consumer, access_token).once
21
+ Net::HTTP.any_instance.expects(:request).once
22
+
23
+ transport.request :get, "http://test.host", :access_token => access_token
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: partigirb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alvaro Bautista
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-28 00:00:00 +01:00
13
+ date: 2010-02-10 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16