fleakr 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -39,7 +39,7 @@ module Fleakr
39
39
  @options = options
40
40
 
41
41
  @parameters = ParameterList.new(upload_options)
42
- @parameters << FileParameter.new('photo', filename)
42
+ parameters.add_upload_option(:photo, filename)
43
43
  end
44
44
 
45
45
  # A list of upload options for this upload request (see Fleakr::Api::Option)
@@ -5,15 +5,15 @@ module Fleakr
5
5
  #
6
6
  # A simple name / value parameter for use in API calls
7
7
  #
8
- class ValueParameter < Parameter
8
+ class ValueParameter
9
9
 
10
- attr_reader :value
10
+ attr_reader :name, :value
11
11
 
12
12
  # Create a new parameter with the specified name / value pair.
13
13
  #
14
14
  def initialize(name, value, include_in_signature = true)
15
+ @name = name
15
16
  @value = value
16
- super(name, include_in_signature)
17
17
  end
18
18
 
19
19
  # Generate the query string representation of this parameter.
@@ -54,6 +54,10 @@ module Fleakr
54
54
  self.new(response.body)
55
55
  end
56
56
 
57
+ def user
58
+ User.find_by_id(user_id)
59
+ end
60
+
57
61
  end
58
62
 
59
63
  end
@@ -24,8 +24,13 @@ module Fleakr
24
24
  flickr_attribute :id, :title, :description
25
25
  flickr_attribute :primary_photo_id, :from => '@primary'
26
26
  flickr_attribute :count, :from => '@photos'
27
+ flickr_attribute :user_id, :from => '@owner'
27
28
 
28
29
  find_all :by_user_id, :call => 'photosets.getList', :path => 'photosets/photoset'
30
+
31
+ find_one :by_id, :using => :photoset_id, :call => 'photosets.getInfo', :path => 'photoset'
32
+
33
+ lazily_load :user_id, :with => :load_info
29
34
 
30
35
  # Save all photos in this set to the specified directory for the specified size. Allowed
31
36
  # Sizes include <tt>:square</tt>, <tt>:small</tt>, <tt>:thumbnail</tt>, <tt>:medium</tt>,
@@ -46,10 +51,29 @@ module Fleakr
46
51
  sprintf("%0#{self.count.length}d_", (index + 1))
47
52
  end
48
53
 
54
+ # Primary photo for this set. See Fleakr::Objects::Photo for more details.
55
+ #
49
56
  def primary_photo
50
57
  @primary_photo ||= Photo.find_by_id(primary_photo_id)
51
58
  end
52
59
 
60
+ # The URL for this set.
61
+ #
62
+ def url
63
+ "http://www.flickr.com/photos/#{user_id}/sets/#{id}/"
64
+ end
65
+
66
+ # The user who created this set.
67
+ #
68
+ def user
69
+ User.find_by_id(user_id)
70
+ end
71
+
72
+ def load_info # :nodoc:
73
+ response = Fleakr::Api::MethodRequest.with_response!('photosets.getInfo', :photoset_id => self.id)
74
+ self.populate_from(response.body)
75
+ end
76
+
53
77
  end
54
78
  end
55
79
  end
@@ -1,2 +1,3 @@
1
1
  require 'fleakr/support/attribute'
2
- require 'fleakr/support/object'
2
+ require 'fleakr/support/object'
3
+ require 'fleakr/support/request'
@@ -26,7 +26,7 @@ module Fleakr
26
26
 
27
27
  class_eval <<-CODE
28
28
  def #{attribute}
29
- @#{attribute} ||= #{target}.send("find_all_by_#{finder_attribute}".to_sym, self.id)
29
+ @#{attribute} ||= #{target}.send("find_all_by_#{finder_attribute}".to_sym, self.id, self.authentication_options)
30
30
  end
31
31
  CODE
32
32
  end
@@ -41,7 +41,7 @@ module Fleakr
41
41
  options.merge!(:#{attribute} => value)
42
42
 
43
43
  response = Fleakr::Api::MethodRequest.with_response!('#{options[:call]}', options)
44
- (response.body/'rsp/#{options[:path]}').map {|e| #{target_class}.new(e) }
44
+ (response.body/'rsp/#{options[:path]}').map {|e| #{target_class}.new(e, options) }
45
45
  end
46
46
  CODE
47
47
  end
@@ -54,7 +54,7 @@ module Fleakr
54
54
  options.merge!(:#{attribute} => value)
55
55
 
56
56
  response = Fleakr::Api::MethodRequest.with_response!('#{options[:call]}', options)
57
- #{self.name}.new(response.body)
57
+ #{self.name}.new(response.body, options)
58
58
  end
59
59
  CODE
60
60
  end
@@ -64,7 +64,10 @@ module Fleakr
64
64
 
65
65
  class_eval <<-CODE
66
66
  def search(*parameters)
67
- parameters << {:#{key} => self.id}
67
+ options = {:#{key} => self.id}
68
+ options.merge!(self.authentication_options)
69
+
70
+ parameters << options
68
71
  Fleakr::Objects::Search.new(*parameters).results
69
72
  end
70
73
  CODE
@@ -88,10 +91,11 @@ module Fleakr
88
91
 
89
92
  module InstanceMethods
90
93
 
91
- attr_reader :document
94
+ attr_reader :document, :authentication_options
92
95
 
93
- def initialize(document = nil)
96
+ def initialize(document = nil, options = {})
94
97
  self.populate_from(document) unless document.nil?
98
+ @authentication_options = options.extract!(:auth_token)
95
99
  end
96
100
 
97
101
  def populate_from(document)
@@ -0,0 +1,23 @@
1
+ module Fleakr
2
+ module Support # :nodoc:all
3
+ module Request
4
+
5
+ attr_reader :parameters
6
+
7
+ def initialize(additional_parameters = {})
8
+ authenticate = additional_parameters.delete(:authenticate?)
9
+ authenticate = authenticate.nil? ? true : authenticate
10
+
11
+ @parameters = Fleakr::Api::ParameterList.new(additional_parameters, authenticate)
12
+ end
13
+
14
+ def endpoint_uri
15
+ uri = URI.parse(endpoint_url)
16
+ uri.query = self.parameters.to_query
17
+ uri
18
+ end
19
+
20
+
21
+ end
22
+ end
23
+ end
@@ -2,8 +2,8 @@ module Fleakr
2
2
  module Version # :nodoc:
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 5
6
- TINY = 2
5
+ MINOR = 6
6
+ TINY = 0
7
7
 
8
8
  def self.to_s
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <rsp stat="ok">
3
+ <photoset id="72157622660138146" owner="43955217@N05" primary="4042405768" secret="05ef8f3baf" server="3525" farm="4" photos="2">
4
+ <title>My First Set!</title>
5
+ <description>This is a description</description>
6
+ </photoset>
7
+ </rsp>
@@ -59,7 +59,7 @@ class Test::Unit::TestCase
59
59
  object = this_klass.new
60
60
  object.stubs(:id).with().returns('1')
61
61
 
62
- target_klass.expects("find_all_by_#{finder_attribute}".to_sym).with('1').returns(results)
62
+ target_klass.expects("find_all_by_#{finder_attribute}".to_sym).with('1', {}).returns(results)
63
63
  object.send(attribute).should == results
64
64
  end
65
65
 
@@ -87,7 +87,7 @@ class Test::Unit::TestCase
87
87
  stub = stub()
88
88
  response = mock_request_cycle :for => options[:call], :with => params
89
89
 
90
- klass.expects(:new).with(response.body).returns(stub)
90
+ klass.expects(:new).with(response.body, params).returns(stub)
91
91
  klass.send("find_by_#{options[:by]}".to_sym, condition_value).should == stub
92
92
  end
93
93
  end
@@ -111,7 +111,7 @@ class Test::Unit::TestCase
111
111
  stub = stub()
112
112
  stubs << stub
113
113
 
114
- klass.expects(:new).with(element).returns(stub)
114
+ klass.expects(:new).with(element, finder_options).returns(stub)
115
115
  end
116
116
 
117
117
  klass.send("find_all_by_#{options[:by]}".to_sym, condition_value).should == stubs
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Api
4
+ class AuthenticationRequestTest < Test::Unit::TestCase
5
+
6
+ context "An instance of AuthenticationRequest" do
7
+
8
+ should "know the endpoint URL" do
9
+ request = AuthenticationRequest.new
10
+ request.endpoint_url.should == 'http://flickr.com/services/auth/'
11
+ end
12
+
13
+ should "be able to make a request" do
14
+ endpoint_uri = stub()
15
+
16
+ request = AuthenticationRequest.new
17
+ request.stubs(:endpoint_uri).with().returns(endpoint_uri)
18
+
19
+ Net::HTTP.expects(:get_response).with(endpoint_uri).returns('response')
20
+
21
+ request.response.should == 'response'
22
+ end
23
+
24
+ should "know the authorization_url for the authentication request" do
25
+ response_headers = {'Location' => 'http://example.com/auth'}
26
+ response = stub() {|r| r.stubs(:header).with().returns(response_headers) }
27
+
28
+ request = AuthenticationRequest.new
29
+ request.stubs(:response).with().returns(response)
30
+
31
+ request.authorization_url.should == 'http://example.com/auth'
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -14,11 +14,6 @@ module Fleakr::Api
14
14
  FileUtils.rm_rf(@temp_dir)
15
15
  end
16
16
 
17
- should "know not to include itself in the parameter signature" do
18
- parameter = FileParameter.new('photo', @filename)
19
- parameter.include_in_signature?.should be(false)
20
- end
21
-
22
17
  {'jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif'}.each do |ext, mime_type|
23
18
  should "know the correct MIME type for an extension of #{ext}" do
24
19
  parameter = FileParameter.new('photo', "#{@temp_dir}/image.#{ext}")
@@ -3,92 +3,74 @@ require File.dirname(__FILE__) + '/../../../test_helper'
3
3
  module Fleakr::Api
4
4
  class MethodRequestTest < Test::Unit::TestCase
5
5
 
6
- context "An instance of MethodRequest" do
7
-
8
- context "with API credentials" do
9
-
10
- setup do
11
- @api_key = 'f00b4r'
12
- Fleakr.stubs(:api_key).with().returns(@api_key)
13
- Fleakr.stubs(:shared_secret).with().returns('sekrit')
14
- end
15
-
16
- should "know the full query parameters" do
17
- request = MethodRequest.new('flickr.people.findByUsername', :username => 'foobar')
18
-
19
- request.parameters[:api_key].value.should == @api_key
20
- request.parameters[:method].value.should == 'flickr.people.findByUsername'
21
- request.parameters[:username].value.should == 'foobar'
22
- end
23
-
24
- should "translate a shorthand API call" do
25
- request = MethodRequest.new('people.findByUsername')
26
- request.parameters[:method].value.should == 'flickr.people.findByUsername'
27
- end
28
-
29
- should "know the endpoint with full parameters" do
30
- query_parameters = 'foo=bar'
31
-
32
- request = MethodRequest.new('people.getInfo')
33
- request.parameters.stubs(:to_query).returns(query_parameters)
34
-
35
- uri_mock = mock()
36
- uri_mock.expects(:query=).with(query_parameters)
37
- URI.expects(:parse).with("http://api.flickr.com/services/rest/").returns(uri_mock)
38
-
39
- request.__send__(:endpoint_uri).should == uri_mock
40
- end
41
-
42
- should "be able to make a request" do
43
- endpoint_uri = stub()
44
-
45
- request = MethodRequest.new('people.findByUsername')
46
- request.stubs(:endpoint_uri).with().returns(endpoint_uri)
47
-
48
- Net::HTTP.expects(:get).with(endpoint_uri).returns('<xml>')
49
-
50
- request.send
51
- end
52
-
53
- should "create a response from the request" do
54
- response_xml = '<xml>'
55
- response_stub = stub()
56
-
57
- Net::HTTP.stubs(:get).returns(response_xml)
58
- Response.expects(:new).with(response_xml).returns(response_stub)
59
-
60
- request = MethodRequest.new('people.findByUsername')
61
- request.stubs(:endpoint_uri)
62
-
63
- request.send.should == response_stub
64
- end
65
-
66
- should "be able to make a full request and response cycle" do
67
- method = 'flickr.people.findByUsername'
68
- params = {:username => 'foobar'}
69
-
70
- response = stub(:error? => false)
71
-
72
- MethodRequest.expects(:new).with(method, params).returns(stub(:send => response))
73
-
74
- MethodRequest.with_response!(method, params).should == response
75
- end
76
-
77
- should "raise an exception when the full request / response cycle has errors" do
78
- method = 'flickr.people.findByUsername'
79
- params = {:username => 'foobar'}
80
-
81
- response = stub(:error? => true, :error => stub(:code => '1', :message => 'User not found'))
82
-
83
- MethodRequest.expects(:new).with(method, params).returns(stub(:send => response))
84
-
85
- lambda do
86
- MethodRequest.with_response!('flickr.people.findByUsername', :username => 'foobar')
87
- end.should raise_error(Fleakr::ApiError)
88
- end
89
-
6
+ context "An instance of the MethodRequest class" do
7
+
8
+ should "know the endpoint URL" do
9
+ request = MethodRequest.new('people.findByUsername')
10
+ request.endpoint_url.should == 'http://api.flickr.com/services/rest/'
11
+ end
12
+
13
+ should "add the method name to the list of parameters" do
14
+ parameter_list = mock() {|p| p.expects(:add_option).with(:method, 'flickr.people.findByUsername') }
15
+ MethodRequest.any_instance.stubs(:parameters).with().returns(parameter_list)
16
+
17
+ MethodRequest.new('flickr.people.findByUsername')
18
+ end
19
+
20
+ should "translate a shorthand API call" do
21
+ request = MethodRequest.new('people.findByUsername')
22
+ request.method.should == 'flickr.people.findByUsername'
23
+ end
24
+
25
+ should "be able to make a request" do
26
+ endpoint_uri = stub()
27
+
28
+ request = MethodRequest.new('people.findByUsername')
29
+ request.stubs(:endpoint_uri).with().returns(endpoint_uri)
30
+
31
+ Net::HTTP.expects(:get).with(endpoint_uri).returns('<xml>')
32
+
33
+ request.send
34
+ end
35
+
36
+ should "create a response from the request" do
37
+ response_xml = '<xml>'
38
+ response_stub = stub()
39
+
40
+ Net::HTTP.stubs(:get).returns(response_xml)
41
+ Response.expects(:new).with(response_xml).returns(response_stub)
42
+
43
+ request = MethodRequest.new('people.findByUsername')
44
+ request.stubs(:endpoint_uri)
45
+
46
+ request.send.should == response_stub
47
+ end
48
+
49
+ should "be able to make a full request and response cycle" do
50
+ method = 'flickr.people.findByUsername'
51
+ params = {:username => 'foobar'}
52
+
53
+ response = stub(:error? => false)
54
+
55
+ MethodRequest.expects(:new).with(method, params).returns(stub(:send => response))
56
+
57
+ MethodRequest.with_response!(method, params).should == response
90
58
  end
59
+
60
+ should "raise an exception when the full request / response cycle has errors" do
61
+ method = 'flickr.people.findByUsername'
62
+ params = {:username => 'foobar'}
63
+
64
+ response = stub(:error? => true, :error => stub(:code => '1', :message => 'User not found'))
65
+
66
+ MethodRequest.expects(:new).with(method, params).returns(stub(:send => response))
67
+
68
+ lambda do
69
+ MethodRequest.with_response!('flickr.people.findByUsername', :username => 'foobar')
70
+ end.should raise_error(Fleakr::ApiError)
71
+ end
72
+
91
73
  end
92
-
74
+
93
75
  end
94
76
  end
@@ -3,173 +3,192 @@ require File.dirname(__FILE__) + '/../../../test_helper'
3
3
  module Fleakr::Api
4
4
  class ParameterListTest < Test::Unit::TestCase
5
5
 
6
- context "An instance of the ParameterList class" do
7
-
6
+ context "An instance of the ParameterList class with an available API key" do
8
7
  setup do
9
- @api_key = 'key'
10
- @secret = 'foobar'
11
-
12
- Fleakr.stubs(:api_key).with().returns(@api_key)
8
+ @api_key_value = 'api_key_value'
9
+ Fleakr.stubs(:api_key).with().returns(@api_key_value)
10
+
13
11
  @parameter_list = ParameterList.new
14
12
  end
15
-
16
- should "contain the :api_key by default" do
17
- @parameter_list[:api_key].name.should == 'api_key'
18
- @parameter_list[:api_key].value.should == @api_key
19
- @parameter_list[:api_key].include_in_signature?.should be(true)
13
+
14
+ should "send the authentication token by default if it is available" do
15
+ @parameter_list.stubs(:authentication_token).with().returns('toke')
16
+ @parameter_list.send_authentication_token?.should be(true)
20
17
  end
21
18
 
22
- should "be able to create an initial list of parameters" do
23
- parameter_list = ParameterList.new(:one => 'two')
24
- parameter_list[:one].value.should == 'two'
19
+ should "know not to send the authentication token" do
20
+ pl = ParameterList.new({}, false)
21
+ pl.stubs(:authentication_token).with().returns('toke')
22
+
23
+ pl.send_authentication_token?.should be(false)
25
24
  end
26
25
 
27
- should "be able to add parameters to its list" do
28
- parameter = ValueParameter.new('foo', 'bar')
29
-
30
- @parameter_list << parameter
31
- @parameter_list['foo'].should == parameter
26
+ should "know not to send the authentication token if it's not available from the global value" do
27
+ @parameter_list.stubs(:authentication_token).with().returns(nil)
28
+ @parameter_list.send_authentication_token?.should be(false)
32
29
  end
33
30
 
34
- should "allow access to parameters by symbol" do
35
- parameter = ValueParameter.new('foo', 'bar')
36
- @parameter_list << parameter
37
-
38
- @parameter_list[:foo].should == parameter
31
+ should "have a default list of options" do
32
+ @parameter_list.default_options.should == {:api_key => @api_key_value}
39
33
  end
40
34
 
41
- should "overwrite existing values when a duplicate is added" do
42
- length = @parameter_list.instance_variable_get(:@list).length
43
- 2.times {@parameter_list << ValueParameter.new('foo', 'bar') }
35
+ should "use the authentication_token in the options if we're to authenticate" do
36
+ @parameter_list.stubs(:send_authentication_token?).with().returns(true)
37
+ @parameter_list.stubs(:authentication_token).with().returns('toke')
38
+ @parameter_list.stubs(:default_options).with().returns({})
44
39
 
45
- @parameter_list.instance_variable_get(:@list).length.should == length + 1
40
+ @parameter_list.options.should == {:auth_token => 'toke'}
46
41
  end
47
42
 
48
- should "be able to calculate the signature of the parameters" do
49
- Fleakr.stubs(:shared_secret).with().returns(@secret)
50
-
51
- @parameter_list << ValueParameter.new('foo', 'bar')
52
- @parameter_list.signature.should == Digest::MD5.hexdigest("#{@secret}api_key#{@api_key}foobar")
43
+ should "add additional options from the constructor" do
44
+ pl = ParameterList.new(:foo => 'bar')
45
+ pl.options.should == {:foo => 'bar', :api_key => @api_key_value}
53
46
  end
54
47
 
55
- should "use the correct order when signing a list of multiple parameters" do
56
- Fleakr.stubs(:shared_secret).with().returns(@secret)
57
-
58
- @parameter_list << ValueParameter.new('z', 'a')
59
- @parameter_list << ValueParameter.new('a', 'z')
60
-
61
- @parameter_list.signature.should == Digest::MD5.hexdigest("#{@secret}azapi_key#{@api_key}za")
48
+ should "know that it doesn't need to sign the request by default" do
49
+ @parameter_list.sign?.should be(false)
62
50
  end
63
51
 
64
- should "ignore the parameters that aren't included in the signature" do
65
- Fleakr.stubs(:shared_secret).with().returns(@secret)
66
-
67
- @parameter_list << ValueParameter.new('foo', 'bar')
68
- @parameter_list << ValueParameter.new('yes', 'no', false)
69
-
70
- @parameter_list.signature.should == Digest::MD5.hexdigest("#{@secret}api_key#{@api_key}foobar")
52
+ should "know that it needs to sign the request when a shared secret is available" do
53
+ Fleakr.expects(:shared_secret).with().returns('secrit')
54
+ @parameter_list.sign?.should be(true)
71
55
  end
72
56
 
73
- should "be able to generate a boundary for post data" do
74
- rand = '0.123'
57
+ should "be able to add an option to the list" do
58
+ @parameter_list.stubs(:default_options).with().returns({})
75
59
 
76
- @parameter_list.stubs(:rand).with().returns(stub(:to_s => rand))
77
- @parameter_list.boundary.should == Digest::MD5.hexdigest(rand)
60
+ @parameter_list.add_option(:foo, 'bar')
61
+ @parameter_list.options.should == {:foo => 'bar'}
78
62
  end
79
63
 
80
- should "know that it doesn't need to sign the request by default" do
81
- @parameter_list.sign?.should be(false)
64
+ should "have an empty list of upload options by default" do
65
+ @parameter_list.upload_options.should == {}
82
66
  end
83
67
 
84
- should "know that it needs to sign the request when a shared secret is available" do
85
- Fleakr.expects(:shared_secret).with().returns(@secret)
86
- @parameter_list.sign?.should be(true)
68
+ should "be able to add an upload option to the list" do
69
+ @parameter_list.add_upload_option(:file, '/path/to/foo')
70
+ @parameter_list.upload_options.should == {:file => '/path/to/foo'}
87
71
  end
88
72
 
89
- should "know that it doesn't need to authenticate the request by default" do
90
- @parameter_list.authenticate?.should be(false)
73
+ should "be able to generate the list of parameters without a signature" do
74
+ @parameter_list.stubs(:options).with().returns({:foo => 'bar', :api_sig => '1234'})
75
+ @parameter_list.options_without_signature.should == {:foo => 'bar'}
91
76
  end
92
77
 
93
- should "know to authenticate the request when a token is available" do
94
- Fleakr.stubs(:token).with().returns(stub(:value => 'toke'))
95
- parameter_list = ParameterList.new
78
+ should "be able to calculate the signature of the parameters" do
79
+ Fleakr.stubs(:shared_secret).with().returns('sekrit')
80
+
81
+ options = {:api_key => @api_key_value, :foo => 'bar', :blip => 'zeeez'}
82
+ @parameter_list.stubs(:options_without_signature).with().returns(options)
96
83
 
97
- parameter_list.authenticate?.should be(true)
84
+ @parameter_list.signature.should == Digest::MD5.hexdigest("sekritapi_key#{@api_key_value}blipzeeezfoobar")
98
85
  end
99
86
 
100
- should "not authenticate the request if it's been specifically told not to" do
101
- Fleakr.expects(:token).with().never
87
+ should "be able to generate a list of options with a signature" do
88
+ parameters = [ValueParameter.new('foo', 'bar')]
89
+
90
+ @parameter_list.stubs(:options_without_signature).with().returns(:foo => 'bar')
91
+ @parameter_list.stubs(:signature).with().returns('sig')
102
92
 
103
- parameter_list = ParameterList.new(:authenticate? => false)
104
- parameter_list.authenticate?.should be(false)
93
+ @parameter_list.options_with_signature.should == {:foo => 'bar', :api_sig => 'sig'}
105
94
  end
106
95
 
96
+ should "know the auth token when provided to the constructor" do
97
+ pl = ParameterList.new(:auth_token => 'toke')
98
+ pl.authentication_token.should == 'toke'
99
+ end
100
+
101
+ should "know the auth token when available as a global value" do
102
+ Fleakr.stubs(:auth_token).with().returns('toke')
103
+
104
+ pl = ParameterList.new
105
+ pl.authentication_token.should == 'toke'
106
+ end
107
107
 
108
- should "know to authenticate the request when asked" do
109
- Fleakr.expects(:token).with().returns(stub(:value => 'toke'))
108
+ should "know that there is no authentication token if it is not available as a param or global value" do
109
+ Fleakr.stubs(:auth_token).with().returns(nil)
110
110
 
111
- parameter_list = ParameterList.new(:authenticate? => true)
112
- parameter_list.authenticate?.should be(true)
111
+ pl = ParameterList.new
112
+ pl.authentication_token.should be_nil
113
+ end
114
+
115
+ should "be able to generate a boundary for post data" do
116
+ rand = '0.123'
117
+
118
+ @parameter_list.stubs(:rand).with().returns(stub(:to_s => rand))
119
+ @parameter_list.boundary.should == Digest::MD5.hexdigest(rand)
120
+ end
121
+
122
+ should "be able to generate a list of parameters without a signature" do
123
+ parameter = stub()
124
+
125
+ @parameter_list.stubs(:sign?).with().returns(false)
126
+ @parameter_list.expects(:options_without_signature).with().returns(:foo => 'bar')
127
+
128
+ ValueParameter.expects(:new).with(:foo, 'bar').returns(parameter)
129
+
130
+ @parameter_list.list.should == [parameter]
113
131
  end
114
132
 
115
- should "contain the :auth_token parameter in the list if the request is to be authenticated" do
116
- Fleakr.expects(:token).with().returns(stub(:value => 'toke'))
133
+ should "be able to generate a list of parameters with a signature" do
134
+ parameter = stub()
117
135
 
118
- parameter_list = ParameterList.new(:authenticate? => true)
119
- auth_param = parameter_list[:auth_token]
136
+ @parameter_list.stubs(:sign?).with().returns(true)
137
+ @parameter_list.expects(:options_with_signature).with().returns(:foo => 'bar')
138
+
139
+ ValueParameter.expects(:new).with(:foo, 'bar').returns(parameter)
120
140
 
121
- auth_param.name.should == 'auth_token'
122
- auth_param.value.should == 'toke'
123
- auth_param.include_in_signature?.should be(true)
141
+ @parameter_list.list.should == [parameter]
124
142
  end
125
143
 
126
- should "include the signature in the list of parameters if the request is to be signed" do
127
- parameter_list = ParameterList.new
144
+ should "be able to generate a list of parameters with upload parameters if available" do
145
+ value_parameter = stub()
146
+ file_parameter = stub()
128
147
 
129
- parameter_list.stubs(:sign?).with().returns(true)
130
- parameter_list.stubs(:signature).with().returns('sig')
148
+ @parameter_list.stubs(:sign?).with().returns(true)
149
+ @parameter_list.expects(:options_with_signature).with().returns(:foo => 'bar')
150
+ @parameter_list.expects(:upload_options).with().returns(:file => 'path')
131
151
 
132
- signature_param = parameter_list[:api_sig]
152
+ ValueParameter.expects(:new).with(:foo, 'bar').returns(value_parameter)
153
+ FileParameter.expects(:new).with(:file, 'path').returns(file_parameter)
133
154
 
134
- signature_param.name.should == 'api_sig'
135
- signature_param.value.should == 'sig'
136
- signature_param.include_in_signature?.should be(false)
155
+ @parameter_list.list.should == [value_parameter, file_parameter]
137
156
  end
138
-
157
+
139
158
  context "with associated parameters" do
140
-
159
+
141
160
  setup do
142
161
  @p1 = ValueParameter.new('a', 'b')
143
162
  @p2 = ValueParameter.new('c', 'd')
144
-
163
+
145
164
  @p1.stubs(:to_query).with().returns('q1')
146
165
  @p1.stubs(:to_form).with().returns('f1')
147
-
166
+
148
167
  @p2.stubs(:to_query).with().returns('q2')
149
168
  @p2.stubs(:to_form).with().returns('f2')
150
-
151
- @parameter_list.stubs(:list).with().returns('a' => @p1, 'c' => @p2)
169
+
170
+ @parameter_list.stubs(:list).with().returns([@p1, @p2])
152
171
  end
153
-
172
+
154
173
  should "be able to generate a query representation of itself" do
155
174
  @parameter_list.to_query.should == 'q1&q2'
156
175
  end
157
-
176
+
158
177
  should "be able to represent a form representation of itself" do
159
178
  @parameter_list.stubs(:boundary).returns('bound')
160
-
179
+
161
180
  expected =
162
181
  "--bound\r\n" +
163
182
  "f1" +
164
183
  "--bound\r\n" +
165
184
  "f2" +
166
185
  "--bound--"
167
-
186
+
168
187
  @parameter_list.to_form.should == expected
169
188
  end
170
-
189
+
171
190
  end
172
-
191
+
173
192
  end
174
193
 
175
194
  end