theoooo-fleakr 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/README.rdoc +350 -0
  2. data/Rakefile +41 -0
  3. data/lib/fleakr.rb +163 -0
  4. data/lib/fleakr/api.rb +8 -0
  5. data/lib/fleakr/api/file_parameter.rb +47 -0
  6. data/lib/fleakr/api/method_request.rb +66 -0
  7. data/lib/fleakr/api/option.rb +175 -0
  8. data/lib/fleakr/api/parameter.rb +35 -0
  9. data/lib/fleakr/api/parameter_list.rb +97 -0
  10. data/lib/fleakr/api/response.rb +35 -0
  11. data/lib/fleakr/api/upload_request.rb +75 -0
  12. data/lib/fleakr/api/value_parameter.rb +36 -0
  13. data/lib/fleakr/core_ext.rb +3 -0
  14. data/lib/fleakr/core_ext/false_class.rb +7 -0
  15. data/lib/fleakr/core_ext/hash.rb +22 -0
  16. data/lib/fleakr/core_ext/true_class.rb +7 -0
  17. data/lib/fleakr/objects.rb +12 -0
  18. data/lib/fleakr/objects/authentication_token.rb +60 -0
  19. data/lib/fleakr/objects/comment.rb +49 -0
  20. data/lib/fleakr/objects/contact.rb +31 -0
  21. data/lib/fleakr/objects/error.rb +22 -0
  22. data/lib/fleakr/objects/group.rb +36 -0
  23. data/lib/fleakr/objects/image.rb +50 -0
  24. data/lib/fleakr/objects/photo.rb +147 -0
  25. data/lib/fleakr/objects/photo_context.rb +49 -0
  26. data/lib/fleakr/objects/search.rb +30 -0
  27. data/lib/fleakr/objects/set.rb +50 -0
  28. data/lib/fleakr/objects/tag.rb +56 -0
  29. data/lib/fleakr/objects/user.rb +95 -0
  30. data/lib/fleakr/support.rb +2 -0
  31. data/lib/fleakr/support/attribute.rb +46 -0
  32. data/lib/fleakr/support/object.rb +110 -0
  33. data/lib/fleakr/version.rb +13 -0
  34. data/test/fixtures/auth.checkToken.xml +8 -0
  35. data/test/fixtures/auth.getFullToken.xml +8 -0
  36. data/test/fixtures/auth.getToken.xml +8 -0
  37. data/test/fixtures/contacts.getPublicList.xml +7 -0
  38. data/test/fixtures/groups.pools.getPhotos.xml +7 -0
  39. data/test/fixtures/people.findByEmail.xml +6 -0
  40. data/test/fixtures/people.findByUsername.xml +6 -0
  41. data/test/fixtures/people.getInfo.xml +18 -0
  42. data/test/fixtures/people.getPublicGroups.xml +7 -0
  43. data/test/fixtures/people.getPublicPhotos.xml +7 -0
  44. data/test/fixtures/photos.comments.getList.xml +7 -0
  45. data/test/fixtures/photos.getContext.xml +6 -0
  46. data/test/fixtures/photos.getInfo.xml +20 -0
  47. data/test/fixtures/photos.getSizes.xml +10 -0
  48. data/test/fixtures/photos.search.xml +7 -0
  49. data/test/fixtures/photosets.comments.getList.xml +7 -0
  50. data/test/fixtures/photosets.getList.xml +13 -0
  51. data/test/fixtures/photosets.getPhotos.xml +7 -0
  52. data/test/fixtures/tags.getListPhoto.xml +9 -0
  53. data/test/fixtures/tags.getListUser.xml +10 -0
  54. data/test/fixtures/tags.getRelated.xml +9 -0
  55. data/test/test_helper.rb +140 -0
  56. data/test/unit/fleakr/api/file_parameter_test.rb +63 -0
  57. data/test/unit/fleakr/api/method_request_test.rb +93 -0
  58. data/test/unit/fleakr/api/option_test.rb +179 -0
  59. data/test/unit/fleakr/api/parameter_list_test.rb +176 -0
  60. data/test/unit/fleakr/api/parameter_test.rb +34 -0
  61. data/test/unit/fleakr/api/response_test.rb +49 -0
  62. data/test/unit/fleakr/api/upload_request_test.rb +145 -0
  63. data/test/unit/fleakr/api/value_parameter_test.rb +41 -0
  64. data/test/unit/fleakr/core_ext/false_class_test.rb +13 -0
  65. data/test/unit/fleakr/core_ext/hash_test.rb +32 -0
  66. data/test/unit/fleakr/core_ext/true_class_test.rb +13 -0
  67. data/test/unit/fleakr/objects/authentication_token_test.rb +61 -0
  68. data/test/unit/fleakr/objects/comment_test.rb +66 -0
  69. data/test/unit/fleakr/objects/contact_test.rb +58 -0
  70. data/test/unit/fleakr/objects/error_test.rb +21 -0
  71. data/test/unit/fleakr/objects/group_test.rb +46 -0
  72. data/test/unit/fleakr/objects/image_test.rb +81 -0
  73. data/test/unit/fleakr/objects/photo_context_test.rb +80 -0
  74. data/test/unit/fleakr/objects/photo_test.rb +243 -0
  75. data/test/unit/fleakr/objects/search_test.rb +74 -0
  76. data/test/unit/fleakr/objects/set_test.rb +82 -0
  77. data/test/unit/fleakr/objects/tag_test.rb +98 -0
  78. data/test/unit/fleakr/objects/user_test.rb +91 -0
  79. data/test/unit/fleakr/support/attribute_test.rb +126 -0
  80. data/test/unit/fleakr/support/object_test.rb +129 -0
  81. data/test/unit/fleakr_test.rb +171 -0
  82. metadata +175 -0
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Objects
4
+ class SearchTest < Test::Unit::TestCase
5
+
6
+ describe "An instance of the Search class" do
7
+
8
+ it "should be able to generate a list of tags from a single-valued parameter" do
9
+ search = Search.new(:tags => 'foo')
10
+ search.send(:tag_list).should == 'foo'
11
+ end
12
+
13
+ it "should be able to generate a list of tags from multi-valued parameters" do
14
+ search = Search.new(:tags => %w(foo bar))
15
+ search.send(:tag_list).should == 'foo,bar'
16
+ end
17
+
18
+ it "should be able to create parameters for the search" do
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'}
26
+ end
27
+
28
+ it "should not have any :tags parameters if none are supplied" do
29
+ search = Search.new({})
30
+ search.send(:parameters).should == {}
31
+ end
32
+
33
+ it "should convert the search term into the appropriate parameter" do
34
+ search = Search.new(:text => 'foo')
35
+ search.send(:parameters).should == {:text => 'foo'}
36
+ end
37
+
38
+ it "should be able to search photos based on text" do
39
+ response = mock_request_cycle :for => 'photos.search', :with => {:text => 'foo'}
40
+ search = Search.new(:text => 'foo')
41
+ search.results
42
+ end
43
+
44
+ it "should be able to search photos based on tags" do
45
+ response = mock_request_cycle :for => 'photos.search', :with => {:tags => 'one,two'}
46
+
47
+ photo_1, photo_2 = [stub(), stub()]
48
+ photo_1_doc, photo_2_doc = (response.body/'rsp/photos/photo').map {|doc| doc }
49
+
50
+ Photo.expects(:new).with(photo_1_doc).returns(photo_1)
51
+ Photo.expects(:new).with(photo_2_doc).returns(photo_2)
52
+
53
+ search = Search.new(:tags => %w(one two))
54
+ search.results.should == [photo_1, photo_2]
55
+ end
56
+
57
+ should "memoize the search results" do
58
+ response = stub(:body => Hpricot.XML(read_fixture('photos.search')))
59
+ Fleakr::Api::MethodRequest.expects(:with_response!).with(kind_of(String), kind_of(Hash)).once.returns(response)
60
+
61
+ (response.body/'rsp/photos/photo').each do |doc|
62
+ Photo.expects(:new).with(doc).once
63
+ end
64
+
65
+ search = Search.new(:tags => %w(foo))
66
+
67
+ 2.times { search.results }
68
+ end
69
+
70
+
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,82 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Objects
4
+ class SetTest < Test::Unit::TestCase
5
+
6
+ should_have_many :photos, :comments
7
+
8
+ describe "The Set class" do
9
+
10
+ should_find_all :sets, :by => :user_id, :call => 'photosets.getList', :path => 'rsp/photosets/photoset'
11
+
12
+ end
13
+
14
+ describe "An instance of the Set class" do
15
+
16
+ context "when populating from an XML document" do
17
+ before do
18
+ @object = Set.new(Hpricot.XML(read_fixture('photosets.getList')).at('rsp/photosets/photoset'))
19
+ end
20
+
21
+ should_have_a_value_for :id => '72157609490909659'
22
+ should_have_a_value_for :title => 'Second Set'
23
+ should_have_a_value_for :description => 'This is the second set.'
24
+ should_have_a_value_for :count => '138'
25
+
26
+ end
27
+
28
+ context "when saving the set" do
29
+ before do
30
+ @tmp_dir = create_temp_directory
31
+ @set_title = 'set'
32
+
33
+ @set = Set.new
34
+ @set.stubs(:title).with().returns(@set_title)
35
+ end
36
+
37
+ after { FileUtils.rm_rf(@tmp_dir) }
38
+
39
+ it "should know the prefix string based on the number of photos in the set" do
40
+ set = Set.new
41
+ set.stubs(:count).with().returns('5')
42
+ set.file_prefix(0).should == '1_'
43
+
44
+ set.stubs(:count).with().returns('99')
45
+ set.file_prefix(0).should == '01_'
46
+ end
47
+
48
+ it "should save all files of the specified size to the specified directory" do
49
+ image = mock()
50
+ image.expects(:save_to).with("#{@tmp_dir}/set", '1_')
51
+
52
+ photo = stub(:small => image)
53
+
54
+ @set.stubs(:photos).with().returns([photo])
55
+ @set.stubs(:file_prefix).with(0).returns('1_')
56
+
57
+ FileUtils.expects(:mkdir).with("#{@tmp_dir}/set")
58
+
59
+ @set.save_to(@tmp_dir, :small)
60
+ end
61
+
62
+ it "should not create the directory if it already exists" do
63
+ FileUtils.mkdir("#{@tmp_dir}/set")
64
+
65
+ @set.stubs(:photos).with().returns([])
66
+
67
+ FileUtils.expects(:mkdir).with("#{@tmp_dir}/set").never
68
+
69
+ @set.save_to(@tmp_dir, :small)
70
+ end
71
+
72
+ it "should not raise errors when saving an image size that doesn't exist" do
73
+ @set.stubs(:photos).with().returns([stub(:large => nil)])
74
+ lambda { @set.save_to(@tmp_dir, :large) }.should_not raise_error
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,98 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Objects
4
+ class TagTest < Test::Unit::TestCase
5
+
6
+ describe "The Tag class" do
7
+
8
+ should_find_all :tags, :by => :photo_id, :call => 'tags.getListPhoto', :path => 'rsp/photo/tags/tag'
9
+ should_find_all :tags, :by => :user_id, :call => 'tags.getListUser', :path => 'rsp/who/tags/tag'
10
+ end
11
+
12
+ describe "An instance of the Tag class" do
13
+
14
+ before { @tag = Tag.new }
15
+
16
+ context "when populating from the tags_getListPhoto XML data" do
17
+ before do
18
+ @object = Tag.new(Hpricot.XML(read_fixture('tags.getListPhoto')).at('rsp/photo/tags/tag'))
19
+ end
20
+
21
+ should_have_a_value_for :id => '1'
22
+ should_have_a_value_for :author_id => '15498419@N05'
23
+ should_have_a_value_for :value => 'stu72'
24
+ should_have_a_value_for :raw => 'stu 72'
25
+ should_have_a_value_for :machine_flag => '0'
26
+
27
+ end
28
+
29
+ it "should have an author" do
30
+ user = stub()
31
+
32
+ @tag.expects(:author_id).at_least_once.with().returns('1')
33
+
34
+
35
+ User.expects(:find_by_id).with('1').returns(user)
36
+
37
+ @tag.author.should == user
38
+ end
39
+
40
+ it "should memoize the author data" do
41
+ @tag.expects(:author_id).at_least_once.with().returns('1')
42
+
43
+ User.expects(:find_by_id).with('1').once.returns(stub())
44
+
45
+ 2.times { @tag.author }
46
+ end
47
+
48
+ it "should return nil for author if author_id is not present" do
49
+ @tag.expects(:author_id).with().returns(nil)
50
+
51
+ @tag.author.should be(nil)
52
+ end
53
+
54
+ it "should have related tags" do
55
+ @tag.expects(:value).with().returns('foo')
56
+
57
+ response = mock_request_cycle :for => 'tags.getRelated', :with => {:tag => 'foo'}
58
+
59
+ stubs = []
60
+ elements = (response.body/'rsp/tags/tag').map
61
+
62
+ elements.each do |element|
63
+ stub = stub()
64
+ stubs << stub
65
+
66
+ Tag.expects(:new).with(element).returns(stub)
67
+ end
68
+
69
+ @tag.related.should == stubs
70
+ end
71
+
72
+ it "should memoize the data for related tags" do
73
+ @tag.expects(:value).with().returns('foo')
74
+
75
+ mock_request_cycle :for => 'tags.getRelated', :with => {:tag => 'foo'}
76
+
77
+ 2.times { @tag.related }
78
+ end
79
+
80
+ it "should be able to generate a string representation of itself" do
81
+ @tag.expects(:value).with().returns('foo')
82
+ @tag.to_s.should == 'foo'
83
+ end
84
+
85
+ it "should know that it is not a machine tag" do
86
+ @tag.expects(:machine_flag).with().returns('0')
87
+ @tag.machine?.should be(false)
88
+ end
89
+
90
+ it "should know that it is a machine tag" do
91
+ @tag.expects(:machine_flag).with().returns('1')
92
+ @tag.machine?.should be(true)
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+ end
@@ -0,0 +1,91 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Objects
4
+ class UserTest < Test::Unit::TestCase
5
+
6
+ should_search_by :user_id
7
+
8
+ should_have_many :photos, :groups, :sets, :contacts, :tags
9
+
10
+ should_autoload_when_accessing :name, :photos_url, :profile_url, :photos_count, :location, :with => :load_info
11
+ should_autoload_when_accessing :icon_server, :icon_farm, :pro, :admin, :icon_url, :with => :load_info
12
+
13
+ describe "The User class" do
14
+
15
+ should_find_one :user, :by => :username, :call => 'people.findByUsername', :path => 'rsp/user'
16
+ should_find_one :user, :by => :email, :with => :find_email, :call => 'people.findByEmail', :path => 'rsp/user'
17
+ should_find_one :user, :by => :id, :with => :user_id, :call => 'people.getInfo', :path => 'rsp/person'
18
+
19
+ end
20
+
21
+ describe "An instance of User" do
22
+ context "when populating the object from an XML document" do
23
+ before do
24
+ @object = User.new(Hpricot.XML(read_fixture('people.findByUsername')))
25
+ @object.populate_from(Hpricot.XML(read_fixture('people.getInfo')))
26
+ end
27
+
28
+ should_have_a_value_for :id => '31066442@N69'
29
+ should_have_a_value_for :username => 'frootpantz'
30
+ should_have_a_value_for :name => 'Sir Froot Pantz'
31
+ should_have_a_value_for :location => 'The Moon'
32
+ should_have_a_value_for :photos_url => 'http://www.flickr.com/photos/frootpantz/'
33
+ should_have_a_value_for :profile_url => 'http://www.flickr.com/people/frootpantz/'
34
+ should_have_a_value_for :photos_count => '3907'
35
+ should_have_a_value_for :icon_server => '30'
36
+ should_have_a_value_for :icon_farm => '1'
37
+ should_have_a_value_for :pro => '1'
38
+ should_have_a_value_for :admin => '0'
39
+
40
+ end
41
+
42
+ context "in general" do
43
+
44
+ before { @user = User.new }
45
+
46
+ it "should be able to retrieve additional information about the current user" do
47
+ response = mock_request_cycle :for => 'people.getInfo', :with => {:user_id => @user.id}
48
+ @user.expects(:populate_from).with(response.body)
49
+
50
+ @user.load_info
51
+ end
52
+
53
+ it "should be able to generate an icon URL when the :icon_server value is greater than zero" do
54
+ @user.stubs(:icon_server).with().returns('1')
55
+ @user.stubs(:icon_farm).with().returns('2')
56
+ @user.stubs(:id).with().returns('45')
57
+
58
+ @user.icon_url.should == 'http://farm2.static.flickr.com/1/buddyicons/45.jpg'
59
+ end
60
+
61
+ it "should return the default icon URL when the :icon_server value is zero" do
62
+ @user.stubs(:icon_server).with().returns('0')
63
+ @user.icon_url.should == 'http://www.flickr.com/images/buddyicon.jpg'
64
+ end
65
+
66
+ it "should return the default icon URL when the :icon_server value is nil" do
67
+ @user.stubs(:icon_server).with().returns(nil)
68
+ @user.icon_url.should == 'http://www.flickr.com/images/buddyicon.jpg'
69
+ end
70
+
71
+ it "should return a boolean value for :pro?" do
72
+ @user.stubs(:pro).with().returns('0')
73
+ @user.pro?.should be(false)
74
+
75
+ @user.stubs(:pro).with().returns('1')
76
+ @user.pro?.should be(true)
77
+ end
78
+
79
+ it "should return a boolean value for :admin?" do
80
+ @user.stubs(:admin).with().returns('0')
81
+ @user.admin?.should be(false)
82
+
83
+ @user.stubs(:admin).with().returns('1')
84
+ @user.admin?.should be(true)
85
+ end
86
+
87
+ end
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,126 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Fleakr::Support
4
+ class AttributeTest < Test::Unit::TestCase
5
+ describe "An instance of the Attribute class" do
6
+
7
+ it "should know the name of the attribute" do
8
+ attr = Attribute.new('foo')
9
+ attr.name.should == :foo
10
+ end
11
+
12
+ it "should have a default source" do
13
+ attr = Attribute.new(:foo)
14
+ attr.sources.should == ['foo']
15
+ end
16
+
17
+ it "should be able to assign multiple sources" do
18
+ attr = Attribute.new(:foo, ['foo1', 'foo2'])
19
+ attr.sources.should == ['foo1', 'foo2']
20
+ end
21
+
22
+ it "should pull the location from the source" do
23
+ attr = Attribute.new('foo')
24
+ attr.location('foo').should == 'foo'
25
+ end
26
+
27
+ it "should return the location when splitting" do
28
+ attr = Attribute.new('foo')
29
+ attr.split('foo').should == ['foo', nil]
30
+ end
31
+
32
+ it "should return the name for the location when splitting if the location isn't specified" do
33
+ attr = Attribute.new('foo')
34
+ attr.split('@bar').should == ['foo', 'bar']
35
+ end
36
+
37
+ it "should allow the setting of the location information" do
38
+ attr = Attribute.new('foo', 'bar')
39
+ attr.sources.should == ['bar']
40
+ end
41
+
42
+ it "should allow the setting of the attribute value" do
43
+ attr = Attribute.new('foo')
44
+ attr.attribute('@bogon').should == 'bogon'
45
+ end
46
+
47
+ it "should use the location as the attribute" do
48
+ attr = Attribute.new('foo')
49
+ attr.attribute('foo').should == 'foo'
50
+ end
51
+
52
+ it "should use the attribute for the attribute if specified" do
53
+ attr = Attribute.new(:id, '@nsid')
54
+ attr.attribute('@nsid').should == 'nsid'
55
+ end
56
+
57
+ it "should be able to retrieve the node from the path" do
58
+ document = Hpricot.XML('<name>Bassdrive</name>')
59
+ expected = document.at('name')
60
+
61
+ attr = Attribute.new(:name)
62
+ attr.node_for(document, 'name').should == expected
63
+ end
64
+
65
+ it "should be able to retrieve the node that contains the specified attribute" do
66
+ document = Hpricot.XML('<user id="1337" />')
67
+ expected = document.at('user')
68
+
69
+ attr = Attribute.new(:id)
70
+ attr.node_for(document, '@id').should == expected
71
+ end
72
+
73
+ it "should be able to retrieve the node for the specified attribute" do
74
+ document = Hpricot.XML('<user nsid="1337" />')
75
+ expected = document.at('user')
76
+
77
+ attr = Attribute.new(:id, '@nsid')
78
+ attr.node_for(document, '@nsid').should == expected
79
+ end
80
+
81
+ it "should be able to pull simple values from an XML document" do
82
+ document = Hpricot.XML('<name>Bassdrive</name>')
83
+ attr = Attribute.new(:name)
84
+ attr.value_from(document).should == 'Bassdrive'
85
+ end
86
+
87
+ it "should be able to pull an attribute value from the current XML node" do
88
+ document = Hpricot.XML('<user id="1337" />')
89
+ attr = Attribute.new(:id)
90
+ attr.value_from(document).should == '1337'
91
+ end
92
+
93
+ it "should be able to pull a specific attribute value from the current XML node" do
94
+ document = Hpricot.XML('<user nsid="1337" />')
95
+ attr = Attribute.new(:id, '@nsid')
96
+ attr.value_from(document).should == '1337'
97
+ end
98
+
99
+ it "should be able to pull an attribute value for a node and attribute" do
100
+ document = Hpricot.XML('<station><genre slug="dnb">Drum & Bass</genre></station>')
101
+ attr = Attribute.new(:slug, 'station/genre@slug')
102
+ attr.value_from(document).should == 'dnb'
103
+ end
104
+
105
+ it "should be able to pull a value from a nested XML node" do
106
+ document = Hpricot.XML('<rsp><user>blip</user></rsp>')
107
+ attr = Attribute.new(:user)
108
+ attr.value_from(document).should == 'blip'
109
+ end
110
+
111
+ it "should return nil if it cannot find the specified node" do
112
+ document = Hpricot.XML('<user id="1" />')
113
+ attr = Attribute.new(:photoset, '@nsid')
114
+ attr.value_from(document).should be(nil)
115
+ end
116
+
117
+ it "should be able to try a series of nodes to find a value" do
118
+ document = Hpricot.XML('<photoid>123</photoid>')
119
+
120
+ attr = Attribute.new(:id, ['photo@nsid', 'photoid'])
121
+ attr.value_from(document).should == '123'
122
+ end
123
+
124
+ end
125
+ end
126
+ end