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,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  module Fleakr::Api
4
4
  class ResponseTest < Test::Unit::TestCase
@@ -10,35 +10,35 @@ module Fleakr::Api
10
10
  hpricot_stub = stub()
11
11
 
12
12
  Hpricot.expects(:XML).with(response_xml).returns(hpricot_stub)
13
-
13
+
14
14
  response = Response.new(response_xml)
15
15
  response.body.should == hpricot_stub
16
16
  end
17
-
17
+
18
18
  should "memoize the Hpricot document" do
19
19
  response = Response.new('<xml>')
20
-
20
+
21
21
  Hpricot.expects(:XML).with(kind_of(String)).once.returns(stub())
22
-
22
+
23
23
  2.times { response.body }
24
24
  end
25
-
25
+
26
26
  should "know if there are errors in the response" do
27
27
  response_xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<rsp stat=\"fail\">\n\t<err code=\"1\" msg=\"User not found\" />\n</rsp>\n"
28
28
  response = Response.new(response_xml)
29
-
29
+
30
30
  response.error?.should be(true)
31
31
  end
32
-
32
+
33
33
  should "not have an error if there are no errors in the XML" do
34
34
  response = Response.new(read_fixture('people.findByUsername'))
35
35
  response.error.should be(nil)
36
36
  end
37
-
37
+
38
38
  should "have an error if there is an error in the response" do
39
39
  response_xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<rsp stat=\"fail\">\n\t<err code=\"1\" msg=\"User not found\" />\n</rsp>\n"
40
40
  response = Response.new(response_xml)
41
-
41
+
42
42
  response.error.code.should == '1'
43
43
  response.error.message.should == 'User not found'
44
44
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  module Fleakr::Api
4
4
  class UploadRequestTest < Test::Unit::TestCase
@@ -7,32 +7,32 @@ module Fleakr::Api
7
7
 
8
8
  should "have a collection of upload_options" do
9
9
  request = UploadRequest.new('foo', :create, {:title => 'foo', :tags => %w(a b)})
10
-
10
+
11
11
  option_1, option_2 = [stub(:to_hash => {:one => 'value_1'}), stub(:to_hash => {:two => 'value_2'})]
12
-
12
+
13
13
  Option.expects(:for).with(:title, 'foo').returns(option_1)
14
14
  Option.expects(:for).with(:tags, %w(a b)).returns(option_2)
15
-
15
+
16
16
  request.upload_options.should == {:one => 'value_1', :two => 'value_2'}
17
17
  end
18
-
18
+
19
19
  should "create a file parameter on initialization" do
20
20
  filename = '/path/to/image.jpg'
21
-
21
+
22
22
  parameter_list = mock() {|p| p.expects(:add_upload_option).with(:photo, filename) }
23
23
  UploadRequest.any_instance.stubs(:parameters).with().returns(parameter_list)
24
-
24
+
25
25
  UploadRequest.new(filename)
26
26
  end
27
-
27
+
28
28
  should "allow setting options on initialization" do
29
29
  option = stub()
30
30
  option.stubs(:to_hash).with().returns({:title => 'foo'})
31
-
31
+
32
32
  Option.expects(:for).with(:title, 'foo').returns(option)
33
-
33
+
34
34
  ParameterList.expects(:new).with({:title => 'foo'}).returns(stub(:add_upload_option))
35
-
35
+
36
36
  UploadRequest.new('filename', :create, {:title => 'foo'})
37
37
  end
38
38
 
@@ -44,7 +44,7 @@ module Fleakr::Api
44
44
  request = UploadRequest.new('file')
45
45
  request.type.should == :create
46
46
  end
47
-
47
+
48
48
  should "allow setting the type to :update" do
49
49
  request = UploadRequest.new('file', :update)
50
50
  request.type.should == :update
@@ -54,16 +54,16 @@ module Fleakr::Api
54
54
  request = UploadRequest.new('filename')
55
55
  request.__send__(:endpoint_uri).should == URI.parse('http://api.flickr.com/services/upload/')
56
56
  end
57
-
57
+
58
58
  should "know the endpoint_uri for an :update request" do
59
59
  request = UploadRequest.new('filename', :update)
60
60
  request.__send__(:endpoint_uri).should == URI.parse('http://api.flickr.com/services/replace/')
61
61
  end
62
-
62
+
63
63
  should "only parse the endpoint URI once" do
64
64
  request = UploadRequest.new('filename')
65
65
  URI.expects(:parse).with(kind_of(String)).once.returns('uri')
66
-
66
+
67
67
  2.times { request.__send__(:endpoint_uri) }
68
68
  end
69
69
 
@@ -83,14 +83,14 @@ module Fleakr::Api
83
83
 
84
84
  should "be able to send a POST request to the API service" do
85
85
  request = UploadRequest.new('filename')
86
-
86
+
87
87
  uri = stub()
88
88
  uri.stubs(:host).with().returns('host')
89
89
  uri.stubs(:path).with().returns('path')
90
90
  uri.stubs(:port).with().returns(80)
91
-
91
+
92
92
  request.stubs(:endpoint_uri).with().returns(uri)
93
-
93
+
94
94
  request.stubs(:headers).with().returns('header' => 'value')
95
95
  request.parameters.stubs(:to_form).with().returns('form')
96
96
 
@@ -98,39 +98,39 @@ module Fleakr::Api
98
98
  http.expects(:post).with('path', 'form', {'header' => 'value'})
99
99
 
100
100
  Net::HTTP.expects(:start).with('host', 80).yields(http).returns(stub(:body))
101
-
101
+
102
102
  request.send
103
103
  end
104
-
104
+
105
105
  should "get back a response from a POST operation" do
106
106
  response = stub()
107
-
107
+
108
108
  Net::HTTP.expects(:start).with(kind_of(String), kind_of(Fixnum)).returns(stub(:body => '<xml>'))
109
109
  Response.expects(:new).with('<xml>').returns(response)
110
-
110
+
111
111
  request = UploadRequest.new('filename')
112
112
  request.send.should == response
113
113
  end
114
-
114
+
115
115
  should "be able to make a full request and response cycle" do
116
116
  filename = 'filename'
117
117
  response = stub(:error? => false)
118
-
118
+
119
119
  UploadRequest.expects(:new).with(filename, :create, {}).returns(stub(:send => response))
120
120
  UploadRequest.with_response!(filename).should == response
121
121
  end
122
-
122
+
123
123
  should "raise an exception when the full request / response cycle has errors" do
124
124
  filename = 'filename'
125
125
  response = stub(:error? => true, :error => stub(:code => '1', :message => 'User not found'))
126
-
126
+
127
127
  UploadRequest.expects(:new).with(filename, :create, {}).returns(stub(:send => response))
128
-
128
+
129
129
  lambda do
130
130
  UploadRequest.with_response!(filename)
131
131
  end.should raise_error(Fleakr::ApiError)
132
132
  end
133
-
133
+
134
134
  end
135
135
 
136
136
  end
@@ -1,41 +1,41 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  module Fleakr::Api
4
4
  class ValueParameterTest < Test::Unit::TestCase
5
5
 
6
6
  context "An instance of the ValueParameter class" do
7
-
7
+
8
8
  should "have a value" do
9
9
  parameter = ValueParameter.new('foo', 'bar')
10
10
  parameter.value.should == 'bar'
11
11
  end
12
-
12
+
13
13
  should "know how to generate the query representation of itself" do
14
14
  parameter = ValueParameter.new('foo', 'bar')
15
15
  parameter.to_query.should == 'foo=bar'
16
16
  end
17
-
17
+
18
18
  should "escape the value when generating the query representation" do
19
19
  parameter = ValueParameter.new('foo', 'Mr. Crystal?')
20
20
  parameter.to_query.should == 'foo=Mr.+Crystal%3F'
21
21
  end
22
-
22
+
23
23
  should "use an empty string when generating a query if the parameter value is nil" do
24
24
  parameter = ValueParameter.new('foo', nil)
25
25
  parameter.to_query.should == 'foo='
26
26
  end
27
-
27
+
28
28
  should "know how to generate the form representation of itself" do
29
29
  parameter = ValueParameter.new('foo', 'bar')
30
- expected =
30
+ expected =
31
31
  'Content-Disposition: form-data; name="foo"' + "\r\n" +
32
32
  "\r\n" +
33
33
  "bar\r\n"
34
-
34
+
35
35
  parameter.to_form.should == expected
36
36
  end
37
-
37
+
38
38
  end
39
-
39
+
40
40
  end
41
41
  end
@@ -1,13 +1,13 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  class FalseClassTest < Test::Unit::TestCase
4
-
4
+
5
5
  context "An instance of the FalseClass class" do
6
-
6
+
7
7
  should "have 0 as its integer value" do
8
8
  false.to_i.should == 0
9
9
  end
10
-
10
+
11
11
  end
12
-
12
+
13
13
  end
@@ -1,32 +1,32 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  class HashTest < Test::Unit::TestCase
4
-
4
+
5
5
  context "An instance of Hash" do
6
6
  context "when extracting key/value pairs" do
7
-
7
+
8
8
  setup do
9
9
  @hash = {:one => 'two', :three => 'four'}
10
10
  end
11
-
11
+
12
12
  should "return a hash with the matching key/value pairs" do
13
13
  @hash.extract!(:one).should == {:one => 'two'}
14
14
  end
15
-
15
+
16
16
  should "return an empty hash if the key isn't found" do
17
17
  @hash.extract!(:foo).should == {}
18
18
  end
19
-
19
+
20
20
  should "alter the original hash when a value is extracted" do
21
21
  @hash.extract!(:one)
22
22
  @hash.should == {:three => 'four'}
23
23
  end
24
-
24
+
25
25
  should "be able to extract multiple keys" do
26
26
  @hash.extract!(:one, :three, :missing).should == {:one => 'two', :three => 'four'}
27
27
  end
28
-
28
+
29
29
  end
30
30
  end
31
-
31
+
32
32
  end
@@ -1,13 +1,13 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  class TrueClassTest < Test::Unit::TestCase
4
-
4
+
5
5
  context "An instance of the TrueClass class" do
6
-
6
+
7
7
  should "have 1 as its integer value" do
8
8
  true.to_i.should == 1
9
9
  end
10
-
10
+
11
11
  end
12
-
12
+
13
13
  end
@@ -1,70 +1,70 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  module Fleakr::Objects
4
4
  class AuthenticationTokenTest < Test::Unit::TestCase
5
-
5
+
6
6
  context "The AuthenticationToken class" do
7
-
7
+
8
8
  should "be able to create an instance from a mini_token" do
9
9
  token = '123-123-123'
10
10
  auth_token = stub()
11
-
11
+
12
12
  response = mock_request_cycle :for => 'auth.getFullToken', :with => {:mini_token => token, :authenticate? => false}
13
-
13
+
14
14
  AuthenticationToken.expects(:new).with(response.body).returns(auth_token)
15
-
15
+
16
16
  AuthenticationToken.from_mini_token(token).should == auth_token
17
17
  end
18
-
18
+
19
19
  should "be able to create an instance from an auth_token" do
20
20
  token = 'abc123'
21
21
  auth_token = stub()
22
22
 
23
23
  response = mock_request_cycle :for => 'auth.checkToken', :with => {:auth_token => token, :authenticate? => false}
24
-
24
+
25
25
  AuthenticationToken.expects(:new).with(response.body).returns(auth_token)
26
26
  AuthenticationToken.from_auth_token(token).should == auth_token
27
27
  end
28
-
29
-
28
+
29
+
30
30
  should "be able to create an instance from a frob" do
31
31
  frob = '12345678901234567-abcde89012fg3456-7890123'
32
32
  auth_token = stub()
33
-
33
+
34
34
  response = mock_request_cycle :for => 'auth.getToken', :with => {:frob => frob, :authenticate? => false}
35
-
35
+
36
36
  AuthenticationToken.expects(:new).with(response.body).returns(auth_token)
37
37
  AuthenticationToken.from_frob(frob).should == auth_token
38
38
  end
39
-
39
+
40
40
  end
41
-
41
+
42
42
  context "An instance of the AuthenticationToken class" do
43
-
43
+
44
44
  should "have an associated user" do
45
45
  token = AuthenticationToken.new
46
46
  token.stubs(:user_id).with().returns('1')
47
-
47
+
48
48
  User.expects(:find_by_id).with('1').returns('user')
49
-
49
+
50
50
  token.user.should == 'user'
51
51
  end
52
-
52
+
53
53
  context "when populating from an XML document" do
54
-
54
+
55
55
  setup do
56
56
  @object = AuthenticationToken.new(Hpricot.XML(read_fixture('auth.getFullToken')))
57
57
  end
58
-
58
+
59
59
  should_have_a_value_for :value => 'abc-123'
60
60
  should_have_a_value_for :permissions => 'delete'
61
61
  should_have_a_value_for :user_id => '31066442@N69'
62
62
  should_have_a_value_for :full_name => 'Sir Froot Pants'
63
63
  should_have_a_value_for :user_name => 'frootpantz'
64
-
64
+
65
65
  end
66
-
66
+
67
67
  end
68
-
68
+
69
69
  end
70
70
  end
@@ -1,23 +1,23 @@
1
- require File.dirname(__FILE__) + '/../../../test_helper'
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
2
 
3
3
  module Fleakr::Objects
4
4
  class CollectionTest < Test::Unit::TestCase
5
5
 
6
6
  context "The Collection class" do
7
-
8
- should_find_one :collections, :by => :id, :call => 'collections.getInfo', :path => 'rsp/collection'
9
- should_find_all :collections, :by => :user_id, :call => 'collections.getTree', :path => 'rsp/collections/collection'
10
-
7
+
8
+ should_find_one :collections, :by => :id, :call => 'collections.getInfo', :path => 'rsp/collection', :class => Collection
9
+ should_find_all :collections, :by => :user_id, :call => 'collections.getTree', :path => 'rsp/collections/collection', :class => Collection
10
+
11
11
  end
12
12
 
13
13
  context "An instance of the Collection class" do
14
-
14
+
15
15
  context "when populating from a call to collections.getInfo" do
16
16
  setup do
17
17
  document = Hpricot.XML(read_fixture('collections.getInfo')).at('rsp/collection')
18
18
  @object = Collection.new(document)
19
19
  end
20
-
20
+
21
21
  should_have_a_value_for :id => '34762917-72157619347181335'
22
22
  should_have_a_value_for :title => 'Work'
23
23
  should_have_a_value_for :description => 'Sample Collection'
@@ -26,46 +26,46 @@ module Fleakr::Objects
26
26
  should_have_a_value_for :created => '1244460707'
27
27
 
28
28
  end
29
-
29
+
30
30
  context "when populating from a call to collections.getTree" do
31
31
  setup do
32
32
  document = Hpricot.XML(read_fixture('collections.getTree')).at('rsp/collections/collection')
33
33
  @object = Collection.new(document)
34
34
  end
35
-
35
+
36
36
  should_have_a_value_for :id => '123-456'
37
37
  should_have_a_value_for :title => 'Top-Level Collection 1'
38
38
  should_have_a_value_for :description => 'Description 1'
39
39
  should_have_a_value_for :large_icon_url => 'http://farm4.static.flickr.com/3573/cols/72157617429277370_1691956f71_l.jpg'
40
40
  should_have_a_value_for :small_icon_url => 'http://farm4.static.flickr.com/3573/cols/72157617429277370_1691956f71_s.jpg'
41
-
41
+
42
42
  end
43
-
43
+
44
44
  context "that contains collections" do
45
45
  setup do
46
46
  document = Hpricot.XML(read_fixture('collections.getTree')).at('rsp/collections/collection')
47
47
  @collection = Collection.new(document)
48
48
  end
49
-
49
+
50
50
  should "have a set of collections" do
51
51
  collection_titles = @collection.collections.map {|c| c.title }
52
52
  collection_titles.should == ['Second-Level Collection 1']
53
53
  end
54
-
54
+
55
55
  should "not have any associated sets" do
56
56
  @collection.sets.should == []
57
57
  end
58
58
  end
59
-
59
+
60
60
  context "that contains sets" do
61
61
  setup do
62
62
  xml = <<-XML
63
- <collection id="123-102" title="Top-Level Collection 2" description="Description 5" iconlarge="http://farm4.static.flickr.com/3573/cols/72157617429277370_1691956f71_l.jpg" iconsmall="http://farm4.static.flickr.com/3573/cols/72157617429277370_1691956f71_s.jpg">
64
- <set id="1238" title="Set 5" description="" />
65
- <set id="1239" title="Set 6" description="" />
63
+ <collection id="123-102" title="Top-Level Collection 2" description="Description 5" iconlarge="http://farm4.static.flickr.com/3573/cols/72157617429277370_1691956f71_l.jpg" iconsmall="http://farm4.static.flickr.com/3573/cols/72157617429277370_1691956f71_s.jpg">
64
+ <set id="1238" title="Set 5" description="" />
65
+ <set id="1239" title="Set 6" description="" />
66
66
  </collection>
67
67
  XML
68
-
68
+
69
69
  document = Hpricot.XML(xml).at('collection')
70
70
  @collection = Collection.new(document)
71
71
  end
@@ -73,25 +73,25 @@ module Fleakr::Objects
73
73
  should "not have any associated collections" do
74
74
  @collection.collections.should == []
75
75
  end
76
-
76
+
77
77
  should "have associated sets" do
78
78
  set_titles = @collection.sets.map {|s| s.title }
79
79
  set_titles.should == ['Set 5', 'Set 6']
80
80
  end
81
-
81
+
82
82
  end
83
-
83
+
84
84
  context "in general" do
85
85
  setup do
86
86
  @collection = Collection.new
87
87
  @time = Time.parse('2008-08-01 00:00:00')
88
88
  end
89
-
89
+
90
90
  should "have a value for :created_at" do
91
91
  @collection.expects(:created).with().returns("#{@time.to_i}")
92
92
  @collection.created_at.to_s.should == @time.to_s
93
93
  end
94
-
94
+
95
95
  end
96
96
  end
97
97