reagent-fleakr 0.2.1 → 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.
Files changed (49) hide show
  1. data/{README.markdown → README.rdoc} +53 -26
  2. data/Rakefile +3 -3
  3. data/lib/fleakr/api/request.rb +58 -0
  4. data/lib/fleakr/api/response.rb +35 -0
  5. data/lib/fleakr/objects/contact.rb +31 -0
  6. data/lib/fleakr/objects/error.rb +22 -0
  7. data/lib/fleakr/objects/group.rb +26 -0
  8. data/lib/fleakr/objects/image.rb +51 -0
  9. data/lib/fleakr/objects/photo.rb +55 -0
  10. data/lib/fleakr/objects/search.rb +33 -0
  11. data/lib/fleakr/objects/set.rb +45 -0
  12. data/lib/fleakr/objects/user.rb +106 -0
  13. data/lib/fleakr/support/attribute.rb +28 -0
  14. data/lib/fleakr/support/object.rb +88 -0
  15. data/lib/fleakr/version.rb +3 -3
  16. data/lib/fleakr.rb +66 -11
  17. data/test/fixtures/contacts.getPublicList.xml +7 -0
  18. data/test/fixtures/groups.pools.getPhotos.xml +7 -0
  19. data/test/fixtures/people.getInfo.xml +1 -1
  20. data/test/fixtures/photos.getSizes.xml +10 -0
  21. data/test/test_helper.rb +21 -6
  22. data/test/unit/fleakr/api/request_test.rb +93 -0
  23. data/test/{fleakr → unit/fleakr/api}/response_test.rb +2 -2
  24. data/test/unit/fleakr/objects/contact_test.rb +58 -0
  25. data/test/{fleakr → unit/fleakr/objects}/error_test.rb +2 -2
  26. data/test/{fleakr → unit/fleakr/objects}/group_test.rb +6 -2
  27. data/test/unit/fleakr/objects/image_test.rb +76 -0
  28. data/test/unit/fleakr/objects/photo_test.rb +101 -0
  29. data/test/{fleakr → unit/fleakr/objects}/search_test.rb +21 -16
  30. data/test/{fleakr → unit/fleakr/objects}/set_test.rb +17 -12
  31. data/test/{fleakr → unit/fleakr/objects}/user_test.rb +44 -3
  32. data/test/{fleakr → unit/fleakr/support}/attribute_test.rb +2 -2
  33. data/test/{fleakr → unit/fleakr/support}/object_test.rb +3 -3
  34. data/test/unit/fleakr_test.rb +44 -0
  35. metadata +43 -29
  36. data/lib/fleakr/attribute.rb +0 -26
  37. data/lib/fleakr/error.rb +0 -10
  38. data/lib/fleakr/group.rb +0 -12
  39. data/lib/fleakr/image.rb +0 -35
  40. data/lib/fleakr/object.rb +0 -75
  41. data/lib/fleakr/photo.rb +0 -26
  42. data/lib/fleakr/request.rb +0 -45
  43. data/lib/fleakr/response.rb +0 -21
  44. data/lib/fleakr/search.rb +0 -31
  45. data/lib/fleakr/set.rb +0 -22
  46. data/lib/fleakr/user.rb +0 -33
  47. data/test/fleakr/image_test.rb +0 -82
  48. data/test/fleakr/photo_test.rb +0 -64
  49. data/test/fleakr/request_test.rb +0 -99
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Api
4
+ class RequestTest < Test::Unit::TestCase
5
+
6
+ describe "A Request instance" do
7
+
8
+ context "with an API key" do
9
+
10
+ before do
11
+ @api_key = 'f00b4r'
12
+ Fleakr.stubs(:api_key).with().returns(@api_key)
13
+ end
14
+
15
+ it "should know the full query parameters" do
16
+ request = Request.new('flickr.people.findByUsername', :username => 'foobar')
17
+
18
+ expected = [
19
+ "api_key=#{@api_key}",
20
+ "method=flickr.people.findByUsername",
21
+ "username=foobar"
22
+ ]
23
+
24
+ request.query_parameters.split('&').sort.should == expected
25
+ end
26
+
27
+ it "should escape the keys and values in the parameter list" do
28
+ request = Request.new('flickr.people.findByUsername', :username => 'the decapitator')
29
+ request.query_parameters.split('&').include?("username=#{CGI.escape('the decapitator')}").should be(true)
30
+ end
31
+
32
+ it "should translate a shorthand API call" do
33
+ request = Request.new('people.findByUsername')
34
+ request.query_parameters.split('&').include?('method=flickr.people.findByUsername').should be(true)
35
+ end
36
+
37
+ it "should know the endpoint with full parameters" do
38
+ query_parameters = 'foo=bar'
39
+
40
+ request = Request.new('people.getInfo')
41
+ request.stubs(:query_parameters).with().returns(query_parameters)
42
+
43
+ uri_mock = mock() {|m| m.expects(:query=).with(query_parameters)}
44
+
45
+ URI.expects(:parse).with("http://api.flickr.com/services/rest/").returns(uri_mock)
46
+
47
+ request.endpoint_uri.should == uri_mock
48
+ end
49
+
50
+ it "should be able to make a request" do
51
+ endpoint_uri = stub()
52
+
53
+ request = Request.new('flickr.people.findByUsername')
54
+
55
+ request.stubs(:endpoint_uri).with().returns(endpoint_uri)
56
+ Net::HTTP.expects(:get).with(endpoint_uri).returns('<xml>')
57
+
58
+ request.send
59
+ end
60
+
61
+ it "should create a response from the request" do
62
+ response_xml = '<xml>'
63
+ response_stub = stub()
64
+
65
+ request = Request.new('flickr.people.findByUsername')
66
+
67
+ Net::HTTP.stubs(:get).returns(response_xml)
68
+ Response.expects(:new).with(response_xml).returns(response_stub)
69
+
70
+ request.send.should == response_stub
71
+ end
72
+
73
+ it "should be able to make a full request and response cycle" do
74
+ response = stub(:error? => false)
75
+ Response.expects(:new).with(kind_of(String)).returns(response)
76
+
77
+ Request.with_response!('flickr.people.findByUsername', :username => 'foobar').should == response
78
+ end
79
+
80
+ it "should raise an exception when the full request / response cycle has errors" do
81
+ response = stub(:error? => true, :error => stub(:code => '1', :message => 'User not found'))
82
+ Response.stubs(:new).with(kind_of(String)).returns(response)
83
+
84
+ lambda do
85
+ Request.with_response!('flickr.people.findByUsername', :username => 'foobar')
86
+ end.should raise_error(Fleakr::Api::Request::ApiError)
87
+ end
88
+
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -1,6 +1,6 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
2
 
3
- module Fleakr
3
+ module Fleakr::Api
4
4
  class ResponseTest < Test::Unit::TestCase
5
5
 
6
6
  describe "An instance of Response" do
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Objects
4
+ class ContactTest < Test::Unit::TestCase
5
+
6
+ describe "The Contact class" do
7
+
8
+ should "return a list of users for a specified user's contacts" do
9
+ user_1, user_2 = [stub(), stub()]
10
+ contact_1, contact_2 = [stub(:to_user => user_1), stub(:to_user => user_2)]
11
+
12
+ response = mock_request_cycle :for => 'contacts.getPublicList', :with => {:user_id => '1'}
13
+
14
+ contact_1_doc, contact_2_doc = (response.body/'rsp/contacts/contact').map
15
+
16
+ Contact.stubs(:new).with(contact_1_doc).returns(contact_1)
17
+ Contact.stubs(:new).with(contact_2_doc).returns(contact_2)
18
+
19
+ Contact.find_all_by_user_id('1').should == [user_1, user_2]
20
+ end
21
+
22
+ end
23
+
24
+ describe "An instance of the Contact class" do
25
+ context "when populating from an XML document" do
26
+ before do
27
+ @object = Contact.new(Hpricot.XML(read_fixture('contacts.getPublicList')).at('contacts/contact'))
28
+ end
29
+
30
+ should_have_a_value_for :id => '9302864@N42'
31
+ should_have_a_value_for :username => 'blinky'
32
+ should_have_a_value_for :icon_server => '2263'
33
+ should_have_a_value_for :icon_farm => '3'
34
+
35
+ end
36
+
37
+ context "in general" do
38
+
39
+ should "be able to convert to a user" do
40
+ contact = Contact.new
41
+ user = mock()
42
+
43
+ User.stubs(:new).returns(user)
44
+
45
+ [:id, :username, :icon_server, :icon_farm].each do |method|
46
+ contact.stubs(method).with().returns(method.to_s)
47
+ user.expects("#{method}=".to_sym).with(method.to_s)
48
+ end
49
+
50
+ contact.to_user.should == user
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
@@ -1,6 +1,6 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
2
 
3
- module Fleakr
3
+ module Fleakr::Objects
4
4
  class ErrorTest < Test::Unit::TestCase
5
5
 
6
6
  describe "An instance of the Error class" do
@@ -1,8 +1,12 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
2
 
3
- module Fleakr
3
+ module Fleakr::Objects
4
4
  class GroupTest < Test::Unit::TestCase
5
5
 
6
+ should_have_many :photos
7
+
8
+ should_search_by :group_id
9
+
6
10
  describe "The Group class" do
7
11
 
8
12
  should_find_all :groups, :by => :user_id, :call => 'people.getPublicGroups', :path => 'rsp/groups/group'
@@ -0,0 +1,76 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Objects
4
+ class ImageTest < Test::Unit::TestCase
5
+
6
+ describe "The Image class" do
7
+
8
+ should_find_all :images, :by => :photo_id, :call => 'photos.getSizes', :path => 'sizes/size'
9
+
10
+ end
11
+
12
+ describe "An instance of the Image class" do
13
+
14
+ context "when populating the object from an XML document" do
15
+
16
+ before do
17
+ @object = Image.new(Hpricot.XML(read_fixture('photos.getSizes')).at('sizes/size'))
18
+ end
19
+
20
+ should_have_a_value_for :size => 'Square'
21
+ should_have_a_value_for :width => '75'
22
+ should_have_a_value_for :height => '75'
23
+ should_have_a_value_for :url => 'http://farm4.static.flickr.com/3093/2409912100_71e14ed08a_s.jpg'
24
+ should_have_a_value_for :page => 'http://www.flickr.com/photos/the_decapitator/2409912100/sizes/sq/'
25
+
26
+ end
27
+
28
+ context "in general" do
29
+
30
+ it "should know its filename" do
31
+ image = Image.new
32
+ image.stubs(:url).with().returns('http://flickr.com/photos/foobar.jpg')
33
+
34
+ image.filename.should == 'foobar.jpg'
35
+ end
36
+
37
+ context "when saving the file" do
38
+
39
+ before do
40
+ @tmp_dir = create_temp_directory
41
+
42
+ @url = 'http://host.com/image.jpg'
43
+ @image_filename = 'image.jpg'
44
+
45
+ @image = Image.new
46
+ @image.stubs(:url).with().returns(@url)
47
+ @image.stubs(:filename).with().returns(@image_filename)
48
+ end
49
+
50
+ after do
51
+ FileUtils.rm_rf(@tmp_dir)
52
+ end
53
+
54
+ should "be able to save to a directory with the original filename" do
55
+ Net::HTTP.expects(:get).with(URI.parse(@url)).returns('image_data')
56
+
57
+ @image.save_to(@tmp_dir)
58
+ File.read("#{@tmp_dir}/image.jpg").should == 'image_data'
59
+ end
60
+
61
+ should "be able to save to a specified file" do
62
+ Net::HTTP.expects(:get).with(URI.parse(@url)).returns('image_data')
63
+ existing_file = "#{@tmp_dir}/existing_file.jpg"
64
+
65
+ FileUtils.touch(existing_file)
66
+
67
+ @image.save_to(existing_file)
68
+ File.read(existing_file).should == 'image_data'
69
+ end
70
+
71
+ end
72
+ end
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Objects
4
+ class PhotoTest < Test::Unit::TestCase
5
+
6
+ should_have_many :images
7
+
8
+ describe "The Photo class" do
9
+
10
+ should_find_all :photos, :by => :user_id, :call => 'people.getPublicPhotos', :path => 'rsp/photos/photo'
11
+ should_find_all :photos, :by => :photoset_id, :call => 'photosets.getPhotos', :path => 'rsp/photoset/photo'
12
+ should_find_all :photos, :by => :group_id, :call => 'groups.pools.getPhotos', :path => 'rsp/photos/photo'
13
+
14
+ end
15
+
16
+ describe "An instance of the Photo class" do
17
+ context "when populating from an XML document" do
18
+ before do
19
+ @object = Photo.new(Hpricot.XML(read_fixture('people.getPublicPhotos')).at('rsp/photos/photo'))
20
+ end
21
+
22
+ should_have_a_value_for :id => '2924549350'
23
+ should_have_a_value_for :title => 'Photo #1'
24
+ should_have_a_value_for :farm_id => '4'
25
+ should_have_a_value_for :server_id => '3250'
26
+ should_have_a_value_for :secret => 'cbc1804258'
27
+
28
+ end
29
+
30
+ context "in general" do
31
+
32
+ it "should have a collection of images by size" do
33
+ photo = Photo.new
34
+
35
+ small_image, large_image = [stub(:size => 'Small'), stub(:size => 'Large')]
36
+
37
+ photo.stubs(:images).returns([small_image, large_image])
38
+
39
+ expected = {
40
+ :square => nil,
41
+ :thumbnail => nil,
42
+ :small => small_image,
43
+ :medium => nil,
44
+ :large => large_image,
45
+ :original => nil
46
+ }
47
+
48
+ photo.send(:images_by_size).should == expected
49
+ end
50
+
51
+ [:square, :thumbnail, :small, :medium, :large, :original].each do |method|
52
+ it "should have a reader for the :#{method} image" do
53
+ photo = Photo.new
54
+ image = stub()
55
+
56
+ photo.stubs(:images_by_size).returns(method => image)
57
+ photo.send(method).should == image
58
+ end
59
+ end
60
+
61
+
62
+ end
63
+ end
64
+
65
+ # context "in general" do
66
+ #
67
+ # before do
68
+ # @photo = Photo.new
69
+ #
70
+ # @photo.stubs(:id).with().returns('1')
71
+ # @photo.stubs(:farm_id).with().returns('2')
72
+ # @photo.stubs(:server_id).with().returns('3')
73
+ # @photo.stubs(:secret).with().returns('secret')
74
+ # end
75
+ #
76
+ # it "should know the base URL to retrieve images" do
77
+ # @photo.send(:base_url).should == "http://farm2.static.flickr.com/3/1_secret"
78
+ # end
79
+ #
80
+ # end
81
+
82
+ # context "with a base URL" do
83
+ #
84
+ # before do
85
+ # @photo = Photo.new
86
+ # @photo.stubs(:base_url).with().returns('url')
87
+ # end
88
+ #
89
+ # [:square, :thumbnail, :small, :medium, :large].each do |size|
90
+ # it "should have a :#{size} image" do
91
+ # image = stub()
92
+ # Image.expects(:new).with('url', size).returns(image)
93
+ #
94
+ # @photo.send(size).should == image
95
+ # end
96
+ # end
97
+ #
98
+ # end
99
+ end
100
+
101
+ end
@@ -1,38 +1,43 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
2
 
3
- module Fleakr
3
+ module Fleakr::Objects
4
4
  class SearchTest < Test::Unit::TestCase
5
5
 
6
6
  describe "An instance of the Search class" do
7
7
 
8
8
  it "should be able to generate a list of tags from a single-valued parameter" do
9
- search = Search.new(nil, :tags => 'foo')
10
- search.tag_list.should == 'foo'
9
+ search = Search.new(:tags => 'foo')
10
+ search.send(:tag_list).should == 'foo'
11
11
  end
12
12
 
13
13
  it "should be able to generate a list of tags from multi-valued parameters" do
14
- search = Search.new(nil, :tags => %w(foo bar))
15
- search.tag_list.should == 'foo,bar'
14
+ search = Search.new(:tags => %w(foo bar))
15
+ search.send(:tag_list).should == 'foo,bar'
16
16
  end
17
17
 
18
18
  it "should be able to create parameters for the search" do
19
- search = Search.new(nil, :tags => %w(foo bar))
20
- search.parameters.should == {:tags => 'foo,bar'}
19
+ search = Search.new(:tags => %w(foo bar))
20
+ search.send(:parameters).should == {:tags => 'foo,bar'}
21
+ end
22
+
23
+ it "should preserve the original :tags parameter if it is a comma-separated string" do
24
+ search = Search.new(:tags => 'one,two')
25
+ search.send(:parameters).should == {:tags => 'one,two'}
21
26
  end
22
27
 
23
28
  it "should not have any :tags parameters if none are supplied" do
24
- search = Search.new
25
- search.parameters.should == {}
29
+ search = Search.new({})
30
+ search.send(:parameters).should == {}
26
31
  end
27
32
 
28
33
  it "should convert the search term into the appropriate parameter" do
29
- search = Search.new('foo')
30
- search.parameters.should == {:text => 'foo'}
34
+ search = Search.new(:text => 'foo')
35
+ search.send(:parameters).should == {:text => 'foo'}
31
36
  end
32
37
 
33
38
  it "should be able to search photos based on text" do
34
39
  response = mock_request_cycle :for => 'photos.search', :with => {:text => 'foo'}
35
- search = Search.new('foo')
40
+ search = Search.new(:text => 'foo')
36
41
  search.results
37
42
  end
38
43
 
@@ -45,19 +50,19 @@ module Fleakr
45
50
  Photo.expects(:new).with(photo_1_doc).returns(photo_1)
46
51
  Photo.expects(:new).with(photo_2_doc).returns(photo_2)
47
52
 
48
- search = Search.new(nil, :tags => %w(one two))
53
+ search = Search.new(:tags => %w(one two))
49
54
  search.results.should == [photo_1, photo_2]
50
55
  end
51
56
 
52
57
  should "memoize the search results" do
53
58
  response = stub(:body => Hpricot.XML(read_fixture('photos.search')))
54
- Request.expects(:with_response!).with(kind_of(String), kind_of(Hash)).once.returns(response)
59
+ Fleakr::Api::Request.expects(:with_response!).with(kind_of(String), kind_of(Hash)).once.returns(response)
55
60
 
56
61
  (response.body/'rsp/photos/photo').each do |doc|
57
62
  Photo.expects(:new).with(doc).once
58
63
  end
59
64
 
60
- search = Search.new(nil, :tags => %w(foo))
65
+ search = Search.new(:tags => %w(foo))
61
66
 
62
67
  2.times { search.results }
63
68
  end