fleakr 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/README.rdoc +88 -57
  2. data/Rakefile +28 -9
  3. data/lib/fleakr.rb +25 -31
  4. data/lib/fleakr/api/option.rb +34 -34
  5. data/lib/fleakr/api/parameter_list.rb +23 -23
  6. data/lib/fleakr/objects.rb +4 -1
  7. data/lib/fleakr/objects/metadata.rb +35 -0
  8. data/lib/fleakr/objects/metadata_collection.rb +36 -0
  9. data/lib/fleakr/objects/photo.rb +15 -11
  10. data/lib/fleakr/objects/set.rb +17 -13
  11. data/lib/fleakr/objects/url.rb +83 -0
  12. data/lib/fleakr/objects/user.rb +8 -0
  13. data/lib/fleakr/support.rb +3 -1
  14. data/lib/fleakr/support/attribute.rb +2 -2
  15. data/lib/fleakr/support/object.rb +44 -29
  16. data/lib/fleakr/support/url_expander.rb +37 -0
  17. data/lib/fleakr/support/utility.rb +66 -0
  18. data/lib/fleakr/version.rb +5 -5
  19. data/test/fixtures/photos.getExif.xml +362 -0
  20. data/test/test_helper.rb +44 -47
  21. data/test/unit/fleakr/api/authentication_request_test.rb +12 -12
  22. data/test/unit/fleakr/api/file_parameter_test.rb +15 -15
  23. data/test/unit/fleakr/api/method_request_test.rb +1 -1
  24. data/test/unit/fleakr/api/option_test.rb +44 -44
  25. data/test/unit/fleakr/api/parameter_list_test.rb +40 -40
  26. data/test/unit/fleakr/api/response_test.rb +10 -10
  27. data/test/unit/fleakr/api/upload_request_test.rb +28 -28
  28. data/test/unit/fleakr/api/value_parameter_test.rb +10 -10
  29. data/test/unit/fleakr/core_ext/false_class_test.rb +5 -5
  30. data/test/unit/fleakr/core_ext/hash_test.rb +9 -9
  31. data/test/unit/fleakr/core_ext/true_class_test.rb +5 -5
  32. data/test/unit/fleakr/objects/authentication_token_test.rb +23 -23
  33. data/test/unit/fleakr/objects/collection_test.rb +23 -23
  34. data/test/unit/fleakr/objects/comment_test.rb +15 -15
  35. data/test/unit/fleakr/objects/contact_test.rb +11 -11
  36. data/test/unit/fleakr/objects/error_test.rb +8 -8
  37. data/test/unit/fleakr/objects/group_test.rb +13 -13
  38. data/test/unit/fleakr/objects/image_test.rb +4 -4
  39. data/test/unit/fleakr/objects/metadata_collection_test.rb +55 -0
  40. data/test/unit/fleakr/objects/metadata_test.rb +27 -0
  41. data/test/unit/fleakr/objects/photo_context_test.rb +23 -23
  42. data/test/unit/fleakr/objects/photo_test.rb +71 -64
  43. data/test/unit/fleakr/objects/search_test.rb +22 -22
  44. data/test/unit/fleakr/objects/set_test.rb +62 -40
  45. data/test/unit/fleakr/objects/tag_test.rb +32 -31
  46. data/test/unit/fleakr/objects/url_test.rb +185 -0
  47. data/test/unit/fleakr/objects/user_test.rb +43 -16
  48. data/test/unit/fleakr/support/attribute_test.rb +15 -15
  49. data/test/unit/fleakr/support/object_test.rb +77 -33
  50. data/test/unit/fleakr/support/request_test.rb +12 -12
  51. data/test/unit/fleakr/support/url_expander_test.rb +44 -0
  52. data/test/unit/fleakr/support/utility_test.rb +70 -0
  53. data/test/unit/fleakr_test.rb +48 -41
  54. metadata +43 -23
@@ -1,46 +1,60 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  class EmptyObject
4
4
  include Fleakr::Support::Object
5
5
  end
6
6
 
7
7
  class FlickrObject
8
-
8
+
9
9
  include Fleakr::Support::Object
10
-
10
+
11
11
  flickr_attribute :name
12
12
  flickr_attribute :description, :from => 'desc'
13
13
  flickr_attribute :id, :from => '@nsid'
14
14
  flickr_attribute :photoset_id, :from => 'photoset@id'
15
15
  flickr_attribute :tag, :category
16
-
16
+
17
17
  find_one :by_id, :call => 'people.getInfo'
18
-
18
+
19
+ has_many :associated_objects
20
+
21
+ end
22
+
23
+ module Fleakr
24
+ module Objects
25
+ class AssociatedObject
26
+
27
+ include Fleakr::Support::Object
28
+
29
+ find_all :by_flickr_object_id, :call => 'object.bogus'
30
+
31
+ end
32
+ end
19
33
  end
20
34
 
21
35
  module Fleakr
22
36
  class ObjectTest < Test::Unit::TestCase
23
-
37
+
24
38
  context "A class method provided by the Flickr::Object module" do
25
-
39
+
26
40
  should "have an empty list of attributes if none are supplied" do
27
41
  EmptyObject.attributes.should == []
28
42
  end
29
-
43
+
30
44
  should "know the names of all its attributes" do
31
45
  FlickrObject.attributes.map {|a| a.name.to_s }.should == %w(name description id photoset_id tag category)
32
46
  end
33
-
47
+
34
48
  should "be able to find by ID" do
35
49
  id = 1
36
50
  flickr_object = stub()
37
-
51
+
38
52
  response = mock_request_cycle :for => 'people.getInfo', :with => {:id => id}
39
53
  FlickrObject.expects(:new).with(response.body, :id => id).returns(flickr_object)
40
-
54
+
41
55
  FlickrObject.find_by_id(id).should == flickr_object
42
56
  end
43
-
57
+
44
58
  should "be able to pass parameters to the :find_by_id method" do
45
59
  id = 1
46
60
  params = {:authenticate? => true}
@@ -49,25 +63,55 @@ module Fleakr
49
63
 
50
64
  response = mock_request_cycle :for => 'people.getInfo', :with => full_params
51
65
  FlickrObject.expects(:new).with(response.body, full_params).returns(flickr_object)
52
-
66
+
53
67
  FlickrObject.find_by_id(id, params).should == flickr_object
54
68
  end
55
-
69
+
56
70
  end
57
-
71
+
58
72
  context "An instance method provided by the Flickr::Object module" do
59
-
73
+
60
74
  should "have default reader methods" do
61
75
  [:name, :description, :id, :photoset_id, :tag, :category].each do |method_name|
62
76
  FlickrObject.new.respond_to?(method_name).should == true
63
77
  end
64
78
  end
65
-
79
+
66
80
  should "be able to capture the :auth_token from options sent along to the new object" do
67
81
  object = EmptyObject.new(nil, {:foo => 'bar', :auth_token => 'toke'})
68
82
  object.authentication_options.should == {:auth_token => 'toke'}
69
83
  end
70
-
84
+
85
+ should "be able to pass parameters to the association" do
86
+ object = FlickrObject.new
87
+ object.stubs(:authentication_options).with().returns({'key' => 'value'})
88
+ object.stubs(:id).returns('1')
89
+
90
+ Fleakr::Objects::AssociatedObject.expects(:find_all_by_flickr_object_id).with('1', {'key' => 'value', :per_page => '100'}).returns('collection')
91
+
92
+ object.associated_objects(:per_page => '100').should == 'collection'
93
+ end
94
+
95
+ should "cache the results of the association" do
96
+ object = FlickrObject.new
97
+ object.stubs(:id).returns('1')
98
+
99
+ Fleakr::Objects::AssociatedObject.expects(:find_all_by_flickr_object_id).with('1', {}).once.returns('collection')
100
+
101
+ 2.times { object.associated_objects }
102
+ end
103
+
104
+ should "know not to cache the results of the association when there are different parameters" do
105
+ object = FlickrObject.new
106
+ object.stubs(:id).returns('1')
107
+
108
+ Fleakr::Objects::AssociatedObject.stubs(:find_all_by_flickr_object_id).with('1', {}).returns('collection_1')
109
+ Fleakr::Objects::AssociatedObject.stubs(:find_all_by_flickr_object_id).with('1', {:per_page => '100'}).returns('collection_2')
110
+
111
+ object.associated_objects.should == 'collection_1'
112
+ object.associated_objects(:per_page => '100').should == 'collection_2'
113
+ end
114
+
71
115
  context "when populating data from an XML document" do
72
116
  setup do
73
117
  xml = <<-XML
@@ -83,59 +127,59 @@ module Fleakr
83
127
  @object = FlickrObject.new
84
128
  @object.populate_from(@document)
85
129
  end
86
-
130
+
87
131
  should "have the correct value for :name" do
88
132
  @object.name.should == 'Fleakr'
89
133
  end
90
-
134
+
91
135
  should "have the correct value for :description" do
92
136
  @object.description.should == 'Awesome'
93
137
  end
94
-
138
+
95
139
  should "have the correct value for :photoset_id" do
96
140
  @object.photoset_id.should == '1'
97
141
  end
98
-
142
+
99
143
  should "have the correct value for :id" do
100
144
  document = Hpricot.XML('<object nsid="1" />').at('object')
101
145
  @object.populate_from(document)
102
-
146
+
103
147
  @object.id.should == '1'
104
148
  end
105
-
149
+
106
150
  should "have the correct value for :tag" do
107
151
  @object.tag.should == 'Tag'
108
152
  end
109
-
153
+
110
154
  should "have the correct value for :category" do
111
155
  @object.category.should == 'Category'
112
156
  end
113
-
157
+
114
158
  should "maintain a reference to the original document" do
115
159
  @object.document.should == @document
116
160
  end
117
161
  end
118
-
162
+
119
163
  should "populate its data from an XML document when initializing" do
120
164
  document = stub()
121
165
  FlickrObject.any_instance.expects(:populate_from).with(document)
122
-
166
+
123
167
  FlickrObject.new(document)
124
168
  end
125
-
169
+
126
170
  should "not attempt to populate itself from an XML document if one is not available" do
127
171
  FlickrObject.any_instance.expects(:populate_from).never
128
172
  FlickrObject.new
129
173
  end
130
-
174
+
131
175
  should "not overwrite existing attributes when pulling in a partial new XML document" do
132
176
  object = FlickrObject.new(Hpricot.XML('<name>Fleakr</name>'))
133
177
  object.populate_from(Hpricot.XML('<desc>Awesome</desc>'))
134
-
178
+
135
179
  object.name.should == 'Fleakr'
136
180
  end
137
-
181
+
138
182
  end
139
-
183
+
140
184
  end
141
185
  end
@@ -1,42 +1,42 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  class BasicRequest
4
4
  include Fleakr::Support::Request
5
-
5
+
6
6
  def endpoint_url
7
7
  'http://example.com'
8
8
  end
9
-
9
+
10
10
  end
11
11
 
12
12
  module Fleakr
13
13
  class RequestTest < Test::Unit::TestCase
14
-
14
+
15
15
  context "An instance of the BasicRequest class with the Request mix-in" do
16
-
16
+
17
17
  should "have a collection of parameters" do
18
18
  params = {:perms => 'read'}
19
19
  Fleakr::Api::ParameterList.expects(:new).with(params, true).returns('params')
20
-
20
+
21
21
  request = BasicRequest.new(params)
22
22
  request.parameters.should == 'params'
23
23
  end
24
-
24
+
25
25
  should "know not to authenticate the request if asked not to" do
26
26
  Fleakr::Api::ParameterList.expects(:new).with({:perms => 'read'}, false).returns('params')
27
-
27
+
28
28
  request = BasicRequest.new(:perms => 'read', :authenticate? => false)
29
29
  request.parameters.should == 'params'
30
30
  end
31
-
31
+
32
32
  should "know the endpoint with full URL parameters" do
33
33
  request = BasicRequest.new
34
34
  request.parameters.stubs(:to_query).with().returns('query')
35
-
35
+
36
36
  request.endpoint_uri.to_s.should == 'http://example.com?query'
37
37
  end
38
-
38
+
39
39
  end
40
-
40
+
41
41
  end
42
42
  end
@@ -0,0 +1,44 @@
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
+
3
+ module Fleakr
4
+ module Support
5
+
6
+ class UrlExpanderTest < Test::Unit::TestCase
7
+
8
+ context "The UrlExpander class" do
9
+
10
+ should "be able to expand a URL" do
11
+ expander = stub() {|e| e.stubs(:expanded_path).with().returns('/expanded/path') }
12
+ UrlExpander.stubs(:new).with('url').returns(expander)
13
+
14
+ UrlExpander.expand('url').should == '/expanded/path'
15
+ end
16
+
17
+ end
18
+
19
+ context "An instance of the UrlExpander class" do
20
+
21
+ should "know the path that needs to be expanded" do
22
+ expander = UrlExpander.new('http://flic.kr/p/7a9yQV')
23
+ expander.path_to_expand.should == '/photo.gne?short=7a9yQV'
24
+ end
25
+
26
+ should "know the expanded path" do
27
+ response = stub()
28
+ client = stub()
29
+
30
+ response.stubs(:[]).with('location').returns('/expanded/path')
31
+ client.stubs(:head).with('/photo.gne?short=7a9yQV').returns(response)
32
+
33
+ Net::HTTP.stubs(:start).with('www.flickr.com', 80).yields(client)
34
+
35
+ expander = UrlExpander.new('http://flic.kr/p/7a9yQV')
36
+ expander.expanded_path.should == '/expanded/path'
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,70 @@
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
+
3
+ module Fleakr::Support
4
+ class UtilityTest < Test::Unit::TestCase
5
+
6
+ context "The Utility class" do
7
+
8
+ should "know the module and class name for a simple string" do
9
+ Utility.class_name_for('Module', 'foo').should == 'Module::Foo'
10
+ end
11
+
12
+ should "know the module and class name for a symbol" do
13
+ Utility.class_name_for('Module', :foo).should == 'Module::Foo'
14
+ end
15
+
16
+ should "know the module and class name for a string that contains an underscore" do
17
+ Utility.class_name_for('Module', 'foo_bar').should == 'Module::FooBar'
18
+ end
19
+
20
+ should "know the module and class name for a pluralized string" do
21
+ Utility.class_name_for('Module', 'foos').should == 'Module::Foo'
22
+ end
23
+
24
+ should "know the ID attribute for a simple class name" do
25
+ Utility.id_attribute_for('Foo').should == 'foo_id'
26
+ end
27
+
28
+ should "know the ID attribute for a namespaced class name" do
29
+ Utility.id_attribute_for('Namespace::Foo').should == 'foo_id'
30
+ end
31
+
32
+ should "know the ID attribute for a camelcased class name" do
33
+ Utility.id_attribute_for('FooBar').should == 'foo_bar_id'
34
+ end
35
+
36
+ should "know that nil is blank" do
37
+ Utility.blank?(nil).should be(true)
38
+ end
39
+
40
+ should "know that an empty string is blank" do
41
+ Utility.blank?('').should be(true)
42
+ end
43
+
44
+ should "know that a string with just spaces is blank" do
45
+ Utility.blank?(' ').should be(true)
46
+ end
47
+
48
+ should "return an array and an empty hash when extracting options from an array" do
49
+ array = ['a']
50
+ Utility.extract_options(array).should == [['a'], {}]
51
+ end
52
+
53
+ should "return an array and hash parts when extracting options from an array" do
54
+ array = ['a', {:key => 'value'}]
55
+ Utility.extract_options(array).should == [['a'], {:key => 'value'}]
56
+ end
57
+
58
+ should "not modify the original array when extracting options from the array" do
59
+ original_array = ['a', {:key => 'value'}]
60
+ array = original_array.dup
61
+
62
+ Utility.extract_options(array)
63
+
64
+ array.should == original_array
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+ end
@@ -1,153 +1,160 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  class FleakrTest < Test::Unit::TestCase
4
-
4
+
5
5
  context "The Fleakr module" do
6
-
6
+
7
7
  [:api_key, :shared_secret, :auth_token].each do |attribute|
8
8
  should "be able to set a value for :#{attribute}" do
9
9
  value = 'value'
10
-
10
+
11
11
  Fleakr.send("#{attribute}=".to_sym, value)
12
12
  Fleakr.send(attribute).should == value
13
13
  end
14
14
  end
15
-
15
+
16
16
  should "provide a means to find a user by his username" do
17
17
  user = stub()
18
18
  Fleakr::Objects::User.expects(:find_by_username).with('username', {}).returns(user)
19
19
  Fleakr.user('username').should == user
20
20
  end
21
-
21
+
22
22
  should "fall back to finding a user by email if finding by username fails" do
23
23
  user = stub()
24
24
  email = 'user@host.com'
25
-
25
+
26
26
  Fleakr::Objects::User.stubs(:find_by_username).with(email, {}).raises(Fleakr::ApiError)
27
27
  Fleakr::Objects::User.expects(:find_by_email).with(email, {}).returns(user)
28
-
28
+
29
29
  Fleakr.user(email).should == user
30
30
  end
31
-
31
+
32
32
  should "fall back to finding a user by URL if username and email both fail" do
33
33
  user = stub()
34
34
  url = 'http://flickr.com/photos/user'
35
-
35
+
36
36
  Fleakr::Objects::User.stubs(:find_by_username).with(url, {}).raises(Fleakr::ApiError)
37
37
  Fleakr::Objects::User.stubs(:find_by_email).with(url, {}).raises(Fleakr::ApiError)
38
38
  Fleakr::Objects::User.expects(:find_by_url).with(url, {}).returns(user)
39
-
39
+
40
40
  Fleakr.user(url).should == user
41
41
  end
42
42
 
43
43
  should "not return nil if a user cannot be found" do
44
44
  data = 'data'
45
-
45
+
46
46
  Fleakr::Objects::User.stubs(:find_by_username).with(data, {}).raises(Fleakr::ApiError)
47
47
  Fleakr::Objects::User.stubs(:find_by_email).with(data, {}).raises(Fleakr::ApiError)
48
48
  Fleakr::Objects::User.stubs(:find_by_url).with(data, {}).raises(Fleakr::ApiError)
49
-
49
+
50
50
  Fleakr.user(data).should be_nil
51
51
  end
52
-
52
+
53
53
  should "provide additional options to the finder call if supplied" do
54
54
  user = stub()
55
55
  Fleakr::Objects::User.expects(:find_by_username).with('username', :auth_token => 'toke').returns(user)
56
56
  Fleakr.user('username', :auth_token => 'toke').should == user
57
57
  end
58
-
58
+
59
59
  should "find all contacts for the authenticated user" do
60
60
  Fleakr::Objects::Contact.expects(:find_all).with({}).returns('contacts')
61
61
  Fleakr.contacts.should == 'contacts'
62
62
  end
63
-
63
+
64
64
  should "allow filtering when finding contacts for the authenticated user" do
65
65
  Fleakr::Objects::Contact.expects(:find_all).with(:filter => :friends).returns('contacts')
66
66
  Fleakr.contacts(:friends).should == 'contacts'
67
67
  end
68
-
68
+
69
69
  should "allow passing of additional parameters when finding contacts for the authenticated user" do
70
70
  Fleakr::Objects::Contact.expects(:find_all).with(:filter => :friends, :page => 1).returns('contacts')
71
71
  Fleakr.contacts(:friends, :page => 1).should == 'contacts'
72
72
  end
73
-
73
+
74
74
  should "be able to perform text searches" do
75
75
  photos = [stub()]
76
-
76
+
77
77
  Fleakr::Objects::Search.expects(:new).with(:text => 'foo').returns(stub(:results => photos))
78
78
  Fleakr.search('foo').should == photos
79
79
  end
80
-
80
+
81
81
  should "be able to perform searches based on tags" do
82
82
  Fleakr::Objects::Search.expects(:new).with(:tags => %w(one two)).returns(stub(:results => []))
83
83
  Fleakr.search(:tags => %w(one two))
84
84
  end
85
-
85
+
86
86
  # TODO: refactor uploading tests?
87
87
  should "be able to upload a collection of images" do
88
88
  glob = '*.jpg'
89
89
  filenames = %w(one.jpg two.jpg)
90
-
90
+
91
91
  Dir.expects(:[]).with(glob).returns(filenames)
92
-
92
+
93
93
  Fleakr::Objects::Photo.expects(:upload).with('one.jpg', {})
94
94
  Fleakr::Objects::Photo.expects(:upload).with('two.jpg', {})
95
-
95
+
96
96
  Fleakr.upload(glob)
97
97
  end
98
-
98
+
99
99
  should "return recently uploaded photos" do
100
100
  filename = '/path/to/image.jpg'
101
101
  new_image = stub()
102
-
102
+
103
103
  Dir.expects(:[]).with(filename).returns([filename])
104
104
  Fleakr::Objects::Photo.expects(:upload).with(filename, {}).returns(new_image)
105
-
105
+
106
106
  Fleakr.upload(filename).should == [new_image]
107
107
  end
108
-
108
+
109
109
  should "be able to pass options for the uploaded files" do
110
110
  filename = '/path/to/image.jpg'
111
111
  new_image = stub()
112
-
112
+
113
113
  Dir.expects(:[]).with(filename).returns([filename])
114
114
  Fleakr::Objects::Photo.expects(:upload).with(filename, :title => 'bop bip').returns(new_image)
115
-
115
+
116
116
  Fleakr.upload(filename, :title => 'bop bip').should == [new_image]
117
117
  end
118
-
118
+
119
119
  should "be able to generate the authorization_url with default permissions" do
120
120
  request = stub() {|r| r.stubs(:authorization_url).with().returns('auth_url') }
121
121
  Fleakr::Api::AuthenticationRequest.expects(:new).with(:perms => :read).returns(request)
122
-
122
+
123
123
  Fleakr.authorization_url.should == 'auth_url'
124
124
  end
125
-
125
+
126
126
  should "be able to specify different permissions when generating the authorization_url" do
127
127
  request = stub() {|r| r.stubs(:authorization_url).with().returns('auth_url') }
128
128
  Fleakr::Api::AuthenticationRequest.expects(:new).with(:perms => :delete).returns(request)
129
-
129
+
130
130
  Fleakr.authorization_url(:delete).should == 'auth_url'
131
131
  end
132
-
132
+
133
133
  should "be able to retrieve a token from the provided frob" do
134
134
  Fleakr::Objects::AuthenticationToken.expects(:from_frob).with('frob').returns('toke')
135
135
  Fleakr.token_from_frob('frob').should == 'toke'
136
136
  end
137
-
137
+
138
138
  should "be able to retrieve a token from the provided mini-token" do
139
139
  Fleakr::Objects::AuthenticationToken.expects(:from_mini_token).with('mini_token').returns('toke')
140
140
  Fleakr.token_from_mini_token('mini_token').should == 'toke'
141
141
  end
142
-
142
+
143
143
  should "be able to get the user for the provided auth token" do
144
144
  token = stub() {|t| t.stubs(:user).with().returns('user') }
145
-
145
+
146
146
  Fleakr::Objects::AuthenticationToken.expects(:from_auth_token).with('toke').returns(token)
147
-
147
+
148
148
  Fleakr.user_for_token('toke').should == 'user'
149
149
  end
150
-
150
+
151
+ should "be able to find the correct resource for a URL" do
152
+ url = stub() {|u| u.stubs(:resource).with().returns('resource') }
153
+ Fleakr::Objects::Url.expects(:new).with('url').returns(url)
154
+
155
+ Fleakr.resource_from_url('url').should == 'resource'
156
+ end
157
+
151
158
  end
152
-
159
+
153
160
  end