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
data/lib/fleakr/photo.rb DELETED
@@ -1,26 +0,0 @@
1
- module Fleakr
2
- class Photo
3
-
4
- include Fleakr::Object
5
-
6
- flickr_attribute :title, :attribute => 'title'
7
- flickr_attribute :id, :attribute => 'id'
8
- flickr_attribute :farm_id, :attribute => 'farm'
9
- flickr_attribute :server_id, :attribute => 'server'
10
- flickr_attribute :secret, :attribute => 'secret'
11
-
12
- find_all :by_photoset_id, :call => 'photosets.getPhotos', :path => 'photoset/photo'
13
- find_all :by_user_id, :call => 'people.getPublicPhotos', :path => 'photos/photo'
14
-
15
- def base_url
16
- "http://farm#{self.farm_id}.static.flickr.com/#{self.server_id}/#{self.id}_#{self.secret}"
17
- end
18
-
19
- [:square, :thumbnail, :small, :medium, :large].each do |size|
20
- define_method(size) do
21
- Image.new(self.base_url, size)
22
- end
23
- end
24
-
25
- end
26
- end
@@ -1,45 +0,0 @@
1
- module Fleakr
2
- class Request
3
-
4
- class ApiError < StandardError; end
5
-
6
- def self.api_key=(key)
7
- @api_key = key
8
- end
9
-
10
- def self.api_key
11
- @api_key
12
- end
13
-
14
- def self.with_response!(method, additional_parameters = {})
15
- request = Request.new(method, additional_parameters)
16
- response = request.send
17
-
18
- raise(ApiError, "Code: #{response.error.code} - #{response.error.message}") if response.error?
19
-
20
- response
21
- end
22
-
23
- def endpoint_uri
24
- uri = URI.parse('http://api.flickr.com/services/rest/')
25
- uri.query = self.query_parameters
26
- uri
27
- end
28
-
29
- def query_parameters
30
- @parameters.map {|key,value| "#{key}=#{CGI.escape(value)}" }.join('&')
31
- end
32
-
33
- def initialize(method, additional_parameters = {})
34
- method = method.sub(/^(flickr\.)?/, 'flickr.')
35
-
36
- default_parameters = {:api_key => self.class.api_key, :method => method}
37
- @parameters = default_parameters.merge(additional_parameters)
38
- end
39
-
40
- def send
41
- Response.new(Net::HTTP.get(self.endpoint_uri))
42
- end
43
-
44
- end
45
- end
@@ -1,21 +0,0 @@
1
- module Fleakr
2
- class Response
3
-
4
- def initialize(response_xml)
5
- @response_xml = response_xml
6
- end
7
-
8
- def body
9
- @body ||= Hpricot.XML(@response_xml)
10
- end
11
-
12
- def error?
13
- (self.body/'rsp').attr('stat') != 'ok'
14
- end
15
-
16
- def error
17
- Error.new(self.body) if self.error?
18
- end
19
-
20
- end
21
- end
data/lib/fleakr/search.rb DELETED
@@ -1,31 +0,0 @@
1
- module Fleakr
2
- class Search
3
-
4
- def initialize(text = nil, search_options = {})
5
- @text = text
6
- @search_options = search_options
7
- end
8
-
9
- def tag_list
10
- Array(@search_options[:tags]).join(',')
11
- end
12
-
13
- def parameters
14
- parameters = {}
15
- parameters.merge!(:text => @text) if @text
16
- parameters.merge!(:tags => self.tag_list) if self.tag_list.length > 0
17
- parameters
18
- end
19
-
20
- def results
21
- if @results.nil?
22
- response = Request.with_response!('photos.search', self.parameters)
23
- @results = (response.body/'rsp/photos/photo').map do |flickr_photo|
24
- Photo.new(flickr_photo)
25
- end
26
- end
27
- @results
28
- end
29
-
30
- end
31
- end
data/lib/fleakr/set.rb DELETED
@@ -1,22 +0,0 @@
1
- module Fleakr
2
- class Set
3
-
4
- include Fleakr::Object
5
-
6
- has_many :photos, :using => :photoset_id
7
-
8
- flickr_attribute :id, :attribute => 'id'
9
- flickr_attribute :title
10
- flickr_attribute :description
11
-
12
- find_all :by_user_id, :call => 'photosets.getList', :path => 'photosets/photoset'
13
-
14
- def save_to(path, size)
15
- target = "#{path}/#{self.title}"
16
- FileUtils.mkdir(target) unless File.exist?(target)
17
-
18
- self.photos.each {|p| p.send(size).save_to(target) }
19
- end
20
-
21
- end
22
- end
data/lib/fleakr/user.rb DELETED
@@ -1,33 +0,0 @@
1
- module Fleakr
2
- class User
3
-
4
- include Fleakr::Object
5
-
6
- flickr_attribute :id, :xpath => 'rsp/user', :attribute => 'nsid'
7
- flickr_attribute :username, :xpath => 'rsp/user/username'
8
- flickr_attribute :photos_url, :xpath => 'rsp/person/photosurl'
9
- flickr_attribute :profile_url, :xpath => 'rsp/person/profileurl'
10
- flickr_attribute :photos_count, :xpath => 'rsp/person/photos/count'
11
- flickr_attribute :icon_server, :xpath => 'rsp/person', :attribute => 'iconserver'
12
- flickr_attribute :icon_farm, :xpath => 'rsp/person', :attribute => 'iconfarm'
13
-
14
- has_many :sets, :groups, :photos
15
-
16
- find_one :by_username, :call => 'people.findByUsername'
17
- find_one :by_email, :using => :find_email, :call => 'people.findByEmail'
18
-
19
- def load_info
20
- response = Request.with_response!('people.getInfo', :user_id => self.id)
21
- self.populate_from(response.body)
22
- end
23
-
24
- def icon_url
25
- if self.icon_server.to_i > 0
26
- "http://farm#{self.icon_farm}.static.flickr.com/#{self.icon_server}/buddyicons/#{self.id}.jpg"
27
- else
28
- 'http://www.flickr.com/images/buddyicon.jpg'
29
- end
30
- end
31
-
32
- end
33
- end
@@ -1,82 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- module Fleakr
4
- class ImageTest < Test::Unit::TestCase
5
-
6
- def self.should_know_the_suffix_for(options)
7
- it "should know the suffix for the :#{options.keys.first} size" do
8
- Image.suffix_for(options.keys.first).should == options.values.first
9
- end
10
- end
11
-
12
-
13
- describe "The Image class" do
14
-
15
- should_know_the_suffix_for :square => '_s.jpg'
16
- should_know_the_suffix_for :thumbnail => '_t.jpg'
17
- should_know_the_suffix_for :small => '_m.jpg'
18
- should_know_the_suffix_for :medium => '.jpg'
19
- should_know_the_suffix_for :large => '_b.jpg'
20
-
21
- end
22
-
23
- describe "An instance of the Image class" do
24
-
25
- before do
26
- @base_filename = 'filename'
27
- @base_url = "http://foo.bar/#{@base_filename}"
28
- @size = :thumbnail
29
-
30
- @image = Image.new(@base_url, @size)
31
- end
32
-
33
- it "should know its full URL" do
34
- Image.stubs(:suffix_for).with(@size).returns('.jpg')
35
- @image.url.should == "#{@base_url}.jpg"
36
- end
37
-
38
- it "should know its filename" do
39
- Image.stubs(:suffix_for).with(@size).returns('.jpg')
40
- @image.filename.should == "#{@base_filename}.jpg"
41
- end
42
-
43
- context "when saving the file" do
44
-
45
- before do
46
- @tmp_dir = create_temp_directory
47
-
48
- @url = 'http://host.com/image.jpg'
49
- @image_filename = 'image.jpg'
50
-
51
- @image = Image.new('http://host/image', :thumbnail)
52
- @image.stubs(:url).with().returns(@url)
53
- @image.stubs(:filename).with().returns(@image_filename)
54
- end
55
-
56
- after do
57
- FileUtils.rm_rf(@tmp_dir)
58
- end
59
-
60
- should "be able to save to a directory with the original filename" do
61
- Net::HTTP.expects(:get).with(URI.parse(@url)).returns('image_data')
62
-
63
- @image.save_to(@tmp_dir)
64
- File.read("#{@tmp_dir}/image.jpg").should == 'image_data'
65
- end
66
-
67
- should "be able to save to a specified file" do
68
- Net::HTTP.expects(:get).with(URI.parse(@url)).returns('image_data')
69
- existing_file = "#{@tmp_dir}/existing_file.jpg"
70
-
71
- FileUtils.touch(existing_file)
72
-
73
- @image.save_to(existing_file)
74
- File.read(existing_file).should == 'image_data'
75
- end
76
-
77
- end
78
-
79
- end
80
-
81
- end
82
- end
@@ -1,64 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- module Fleakr
4
- class PhotoTest < Test::Unit::TestCase
5
-
6
- describe "The Photo class" do
7
-
8
- should_find_all :photos, :by => :user_id, :call => 'people.getPublicPhotos', :path => 'rsp/photos/photo'
9
- should_find_all :photos, :by => :photoset_id, :call => 'photosets.getPhotos', :path => 'rsp/photoset/photo'
10
-
11
- end
12
-
13
- describe "An instance of the Photo class" do
14
- context "when populating from an XML document" do
15
- before do
16
- @object = Photo.new(Hpricot.XML(read_fixture('people.getPublicPhotos')).at('rsp/photos/photo'))
17
- end
18
-
19
- should_have_a_value_for :id => '2924549350'
20
- should_have_a_value_for :title => 'Photo #1'
21
- should_have_a_value_for :farm_id => '4'
22
- should_have_a_value_for :server_id => '3250'
23
- should_have_a_value_for :secret => 'cbc1804258'
24
-
25
- end
26
- end
27
-
28
- context "in general" do
29
-
30
- before do
31
- @photo = Photo.new
32
-
33
- @photo.stubs(:id).with().returns('1')
34
- @photo.stubs(:farm_id).with().returns('2')
35
- @photo.stubs(:server_id).with().returns('3')
36
- @photo.stubs(:secret).with().returns('secret')
37
- end
38
-
39
- it "should know the base URL to retrieve images" do
40
- @photo.base_url.should == "http://farm2.static.flickr.com/3/1_secret"
41
- end
42
-
43
- end
44
-
45
- context "with a base URL" do
46
-
47
- before do
48
- @photo = Photo.new
49
- @photo.stubs(:base_url).with().returns('url')
50
- end
51
-
52
- [:square, :thumbnail, :small, :medium, :large].each do |size|
53
- it "should have a :#{size} image" do
54
- image = stub()
55
- Image.expects(:new).with('url', size).returns(image)
56
-
57
- @photo.send(size).should == image
58
- end
59
- end
60
-
61
- end
62
- end
63
-
64
- end
@@ -1,99 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- module Fleakr
4
- class RequestTest < Test::Unit::TestCase
5
-
6
- describe "A Request instance" do
7
-
8
- it "should be able to set an API key" do
9
- key = 'f00b4r'
10
- Request.api_key = key
11
- key.should == Request.api_key
12
- end
13
-
14
- context "with an API key" do
15
-
16
- before do
17
- @api_key = 'f00b4r'
18
- Request.stubs(:api_key).with().returns(@api_key)
19
- end
20
-
21
- it "should know the full query parameters" do
22
- request = Request.new('flickr.people.findByUsername', :username => 'foobar')
23
-
24
- expected = [
25
- "api_key=#{@api_key}",
26
- "method=flickr.people.findByUsername",
27
- "username=foobar"
28
- ]
29
-
30
- request.query_parameters.split('&').sort.should == expected
31
- end
32
-
33
- it "should escape the keys and values in the parameter list" do
34
- request = Request.new('flickr.people.findByUsername', :username => 'the decapitator')
35
- request.query_parameters.split('&').include?("username=#{CGI.escape('the decapitator')}").should be(true)
36
- end
37
-
38
- it "should translate a shorthand API call" do
39
- request = Request.new('people.findByUsername')
40
- request.query_parameters.split('&').include?('method=flickr.people.findByUsername').should be(true)
41
- end
42
-
43
- it "should know the endpoint with full parameters" do
44
- query_parameters = 'foo=bar'
45
-
46
- request = Request.new('people.getInfo')
47
- request.stubs(:query_parameters).with().returns(query_parameters)
48
-
49
- uri_mock = mock() {|m| m.expects(:query=).with(query_parameters)}
50
-
51
- URI.expects(:parse).with("http://api.flickr.com/services/rest/").returns(uri_mock)
52
-
53
- request.endpoint_uri.should == uri_mock
54
- end
55
-
56
- it "should be able to make a request" do
57
- endpoint_uri = stub()
58
-
59
- request = Request.new('flickr.people.findByUsername')
60
-
61
- request.stubs(:endpoint_uri).with().returns(endpoint_uri)
62
- Net::HTTP.expects(:get).with(endpoint_uri).returns('<xml>')
63
-
64
- request.send
65
- end
66
-
67
- it "should create a response from the request" do
68
- response_xml = '<xml>'
69
- response_stub = stub()
70
-
71
- request = Request.new('flickr.people.findByUsername')
72
-
73
- Net::HTTP.stubs(:get).returns(response_xml)
74
- Response.expects(:new).with(response_xml).returns(response_stub)
75
-
76
- request.send.should == response_stub
77
- end
78
-
79
- it "should be able to make a full request and response cycle" do
80
- response = stub(:error? => false)
81
- Response.expects(:new).with(kind_of(String)).returns(response)
82
-
83
- Request.with_response!('flickr.people.findByUsername', :username => 'foobar').should == response
84
- end
85
-
86
- it "should raise an exception when the full request / response cycle has errors" do
87
- response = stub(:error? => true, :error => stub(:code => '1', :message => 'User not found'))
88
- Response.stubs(:new).with(kind_of(String)).returns(response)
89
-
90
- lambda do
91
- Request.with_response!('flickr.people.findByUsername', :username => 'foobar')
92
- end.should raise_error(Fleakr::Request::ApiError)
93
- end
94
-
95
- end
96
-
97
- end
98
- end
99
- end