reagent-fleakr 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -30,13 +30,18 @@ Find a user by username:
30
30
  >> user = Fleakr::User.find_by_username('the decapitator')
31
31
  => #<Fleakr::User:0x692648 @username="the decapitator", @id="21775151@N06">
32
32
 
33
- And that user's associated sets:
33
+ Or by email:
34
+
35
+ >> user = Fleakr::User.find_by_email('user@host.com')
36
+ => #<Fleakr::User:0x11f484c @username="bckspcr", @id="84481630@N00">
37
+
38
+ Once you have a user, you can find his associated sets:
34
39
 
35
40
  >> user.sets
36
41
  => [#<Fleakr::Set:0x671358 @title="The Decapitator", @description="">,
37
42
  #<Fleakr::Set:0x66d898 @title="londonpaper hijack", ...
38
43
 
39
- Or that user's groups:
44
+ Or groups if you would like:
40
45
 
41
46
  >> user.groups
42
47
  => [#<Fleakr::Group:0x11f2330 ...,
@@ -46,7 +51,7 @@ Or that user's groups:
46
51
  >> user.groups.first.id
47
52
  => "14581414@N00"
48
53
 
49
- You can also grab photos for a particular set:
54
+ When accessing a set, you can also grab all the photos that are in that set:
50
55
 
51
56
  >> user.sets.first
52
57
  => #<Fleakr::Set:0x1195bbc @title="The Decapitator", @id="72157603480986566", @description="">
@@ -54,16 +59,38 @@ You can also grab photos for a particular set:
54
59
  => #<Fleakr::Photo:0x1140108 ... >
55
60
  >> user.sets.first.photos.first.title
56
61
  => "Untitled1"
62
+
63
+ If a photo interests you, save it down to a directory of your choosing:
57
64
 
58
- If you would prefer to just search photos, you can do that by tag for now:
59
-
60
- >> search = Fleakr::Search.new(:tags => %w(macro))
61
- => [#<Fleakr::Photo:0x118bc70 @title="Demure", @id="3076049945">,
62
- #<Fleakr::Photo:0x118b7d4 @title="Bark fly on Bay leaf", @id="3076052409">, ...
63
- >> search.results.first.title
64
- => "Demure"
65
- >> search.results.first.id
66
- => "3076049945"
65
+ >> user.sets.first.photos.first.small.save_to('/tmp')
66
+ => #<File:/tmp/2117922283_715587b2cb_m.jpg (closed)>
67
+
68
+ If you can't decide on a photo and would rather just save the whole set, specify the target directory
69
+ and the size of the images you're interested in:
70
+
71
+ >> user.sets.first.save_to('/tmp', :square)
72
+ => [#<Fleakr::Photo:0x1187a1c @secret="715587b2cb" ...
73
+ >> Dir["/tmp/#{user.sets.first.title}/*.jpg"].map
74
+ => ["/tmp/The Decapitator/2117919621_8b2d601bff_s.jpg",
75
+ "/tmp/The Decapitator/2117921045_5fb15eff90_s.jpg",
76
+ "/tmp/The Decapitator/2117922283_715587b2cb_s.jpg", ...
77
+
78
+ If you would prefer to just search photos, you can do that with search text:
79
+
80
+ >> search = Fleakr::Search.new('ponies!!')
81
+ >> search.results
82
+ => [#<Fleakr::Photo:0x11f4e64 @title="hiroshima atomic garden", @id="3078234390">,
83
+ #<Fleakr::Photo:0x11f4928 @title="PONYLOV", @id="3077360853">, ...
84
+ >> search.results.first.title
85
+ => "hiroshima atomic garden"
86
+
87
+ You can also search based on tags:
88
+
89
+ >> search = Fleakr::Search.new(nil, :tags => 'macro')
90
+ >> search.results.first.title
91
+ => "Demure"
92
+ >> search.results.first.id
93
+ => "3076049945"
67
94
 
68
95
  ## TODO
69
96
 
data/lib/fleakr.rb CHANGED
@@ -14,4 +14,5 @@ require 'fleakr/error'
14
14
  require 'fleakr/set'
15
15
  require 'fleakr/user'
16
16
  require 'fleakr/group'
17
- require 'fleakr/search'
17
+ require 'fleakr/search'
18
+ require 'fleakr/image'
data/lib/fleakr/group.rb CHANGED
@@ -8,9 +8,7 @@ module Fleakr
8
8
 
9
9
  def self.find_all_by_user_id(user_id)
10
10
  response = Request.with_response!('people.getPublicGroups', :user_id => user_id)
11
- (response.body/'rsp/groups/group').map do |flickr_group|
12
- Group.new(flickr_group)
13
- end
11
+ (response.body/'rsp/groups/group').map {|g| Group.new(g) }
14
12
  end
15
13
 
16
14
  end
@@ -0,0 +1,35 @@
1
+ module Fleakr
2
+ class Image
3
+
4
+ SUFFIXES = {
5
+ :square => '_s.jpg',
6
+ :thumbnail => '_t.jpg',
7
+ :small => '_m.jpg',
8
+ :medium => '.jpg',
9
+ :large => '_b.jpg',
10
+ }.freeze
11
+
12
+ def self.suffix_for(size)
13
+ self::SUFFIXES[size]
14
+ end
15
+
16
+ def initialize(base_url, size)
17
+ @base_url = base_url
18
+ @size = size
19
+ end
20
+
21
+ def url
22
+ "#{@base_url}#{Image.suffix_for(@size)}"
23
+ end
24
+
25
+ def filename
26
+ self.url.match(/([^\/]+)$/)[1]
27
+ end
28
+
29
+ def save_to(target)
30
+ destination = File.directory?(target) ? "#{target}/#{self.filename}" : "#{target}"
31
+ File.open(destination, 'w') {|f| f << Net::HTTP.get(URI.parse(self.url)) }
32
+ end
33
+
34
+ end
35
+ end
data/lib/fleakr/object.rb CHANGED
@@ -22,7 +22,8 @@ module Fleakr
22
22
 
23
23
  def populate_from(document)
24
24
  self.class.attributes.each do |attribute|
25
- instance_variable_set("@#{attribute.name}".to_sym, attribute.value_from(document))
25
+ value = attribute.value_from(document)
26
+ instance_variable_set("@#{attribute.name}".to_sym, value) unless value.nil?
26
27
  end
27
28
  end
28
29
 
data/lib/fleakr/photo.rb CHANGED
@@ -5,11 +5,27 @@ module Fleakr
5
5
 
6
6
  flickr_attribute :title, :attribute => 'title'
7
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'
8
11
 
9
12
  def self.find_all_by_photoset_id(photoset_id)
10
13
  response = Request.with_response!('photosets.getPhotos', :photoset_id => photoset_id)
11
- (response.body/'rsp/photoset/photo').map do |photo_body|
12
- Photo.new(photo_body)
14
+ (response.body/'rsp/photoset/photo').map {|p| Photo.new(p) }
15
+ end
16
+
17
+ def self.find_all_by_user_id(user_id)
18
+ response = Request.with_response!('people.getPublicPhotos', :user_id => user_id)
19
+ (response.body/'rsp/photos/photo').map {|p| Photo.new(p) }
20
+ end
21
+
22
+ def base_url
23
+ "http://farm#{self.farm_id}.static.flickr.com/#{self.server_id}/#{self.id}_#{self.secret}"
24
+ end
25
+
26
+ [:square, :thumbnail, :small, :medium, :large].each do |size|
27
+ define_method(size) do
28
+ Image.new(self.base_url, size)
13
29
  end
14
30
  end
15
31
 
data/lib/fleakr/search.rb CHANGED
@@ -3,13 +3,25 @@ module Fleakr
3
3
 
4
4
  attr_reader :tags
5
5
 
6
- def initialize(parameters)
7
- @tags = parameters[:tags]
6
+ def initialize(text = nil, search_options = {})
7
+ @text = text
8
+ @search_options = search_options
9
+ end
10
+
11
+ def tag_list
12
+ Array(@search_options[:tags]).join(',')
13
+ end
14
+
15
+ def parameters
16
+ parameters = {}
17
+ parameters.merge!(:text => @text) if @text
18
+ parameters.merge!(:tags => self.tag_list) if self.tag_list.length > 0
19
+ parameters
8
20
  end
9
21
 
10
22
  def results
11
23
  if @results.nil?
12
- response = Request.with_response!('photos.search', :tags => self.tags.join(','))
24
+ response = Request.with_response!('photos.search', self.parameters)
13
25
  @results = (response.body/'rsp/photos/photo').map do |flickr_photo|
14
26
  Photo.new(flickr_photo)
15
27
  end
data/lib/fleakr/set.rb CHANGED
@@ -9,15 +9,19 @@ module Fleakr
9
9
 
10
10
  def self.find_all_by_user_id(user_id)
11
11
  response = Request.with_response!('photosets.getList', :user_id => user_id)
12
-
13
- (response.body/'rsp/photosets/photoset').map do |flickr_set|
14
- Set.new(flickr_set)
15
- end
12
+ (response.body/'rsp/photosets/photoset').map {|s| Set.new(s) }
16
13
  end
17
14
 
18
15
  def photos
19
16
  @photos ||= Photo.find_all_by_photoset_id(self.id)
20
17
  end
21
18
 
19
+ def save_to(path, size)
20
+ target = "#{path}/#{self.title}"
21
+ FileUtils.mkdir(target) unless File.exist?(target)
22
+
23
+ self.photos.each {|p| p.send(size).save_to(target) }
24
+ end
25
+
22
26
  end
23
27
  end
data/lib/fleakr/user.rb CHANGED
@@ -1,28 +1,55 @@
1
1
  module Fleakr
2
2
  class User
3
-
3
+
4
4
  include Fleakr::Object
5
-
5
+
6
6
  flickr_attribute :id, :xpath => 'rsp/user', :attribute => 'nsid'
7
7
  flickr_attribute :username, :xpath => 'rsp/user/username'
8
-
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
+ def icon_farm_with_load
15
+ retrieve_additional_information
16
+ icon_farm_without_load
17
+ end
18
+
9
19
  def self.find_by_username(username)
10
20
  response = Request.with_response!('people.findByUsername', :username => username)
11
21
  User.new(response.body)
12
22
  end
13
-
23
+
14
24
  def self.find_by_email(email)
15
25
  response = Request.with_response!('people.findByEmail', :find_email => email)
16
26
  User.new(response.body)
17
27
  end
18
-
28
+
29
+ def load_info
30
+ response = Request.with_response!('people.getInfo', :user_id => self.id)
31
+ self.populate_from(response.body)
32
+ end
33
+
34
+ def icon_url
35
+ if self.icon_server.to_i > 0
36
+ "http://farm#{self.icon_farm}.static.flickr.com/#{self.icon_server}/buddyicons/#{self.id}.jpg"
37
+ else
38
+ 'http://www.flickr.com/images/buddyicon.jpg'
39
+ end
40
+ end
41
+
19
42
  def sets
20
43
  @set ||= Set.find_all_by_user_id(self.id)
21
44
  end
22
-
45
+
23
46
  def groups
24
47
  @groups ||= Group.find_all_by_user_id(self.id)
25
48
  end
26
49
 
50
+ def photos
51
+ @photos ||= Photo.find_all_by_user_id(self.id)
52
+ end
53
+
27
54
  end
28
55
  end
@@ -2,8 +2,8 @@ module Fleakr
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 1
6
- TINY = 3
5
+ MINOR = 2
6
+ TINY = 0
7
7
 
8
8
  def self.to_s
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <rsp stat="ok">
3
+ <person id="31066442@N69" nsid="31066442@N69" isadmin="0" ispro="1" iconserver="30" iconfarm="1">
4
+ <username>frootpantz</username>
5
+ <realname />
6
+ <mbox_sha1sum>e52ed1e5b91c763694995460e9796fc2adc02019</mbox_sha1sum>
7
+ <location />
8
+ <photosurl>http://www.flickr.com/photos/frootpantz/</photosurl>
9
+ <profileurl>http://www.flickr.com/people/frootpantz/</profileurl>
10
+ <mobileurl>http://m.flickr.com/photostream.gne?id=34225</mobileurl>
11
+ <photos>
12
+ <firstdatetaken>2006-10-11 08:19:57</firstdatetaken>
13
+ <firstdate>1160612247</firstdate>
14
+ <count>3907</count>
15
+ <views>9002</views>
16
+ </photos>
17
+ </person>
18
+ </rsp>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <rsp stat="ok">
3
+ <photos page="1" pages="1" perpage="100" total="76">
4
+ <photo id="2924549350" owner="21775151@N06" secret="cbc1804258" server="3250" farm="4" title="Photo #1" ispublic="1" isfriend="0" isfamily="0" />
5
+ <photo id="2923697303" owner="21775151@N06" secret="649aa95f29" server="3208" farm="4" title="Photo #2" ispublic="1" isfriend="0" isfamily="0" />
6
+ </photos>
7
+ </rsp>
@@ -5,37 +5,20 @@ module Fleakr
5
5
 
6
6
  describe "The Group class" do
7
7
 
8
- it "should be able to find all groups by user ID" do
9
- user_id = 'ABC123'
10
- group_1, group_2 = [stub(), stub()]
11
-
12
- response = mock_request_cycle :for => 'people.getPublicGroups', :with => {:user_id => user_id}
13
-
14
- group_1_doc, group_2_doc = (response.body/'rsp/groups/group').map {|doc| doc }
15
-
16
- Group.expects(:new).with(group_1_doc).returns(group_1)
17
- Group.expects(:new).with(group_2_doc).returns(group_2)
18
-
19
- Group.find_all_by_user_id(user_id).should == [group_1, group_2]
20
- end
21
-
8
+ should_find_all :groups, :by => :user_id, :call => 'people.getPublicGroups', :path => 'rsp/groups/group'
9
+
22
10
  end
23
11
 
24
12
  describe "An instance of the Group class" do
25
13
  context "when initializing from an Hpricot document" do
26
14
 
27
15
  before do
28
- doc = (Hpricot.XML(read_fixture('people.getPublicGroups'))/'rsp/groups/group').first
29
- @group = Group.new(doc)
30
- end
31
-
32
- it "should have a value for :id" do
33
- @group.id.should == '13378274@N00'
16
+ doc = Hpricot.XML(read_fixture('people.getPublicGroups')).at('rsp/groups/group')
17
+ @object = Group.new(doc)
34
18
  end
35
19
 
36
- it "should have a value for :name" do
37
- @group.name.should == 'Group #1'
38
- end
20
+ should_have_a_value_for :id => '13378274@N00'
21
+ should_have_a_value_for :name => 'Group #1'
39
22
 
40
23
  end
41
24
  end
@@ -0,0 +1,82 @@
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
@@ -82,6 +82,12 @@ module Fleakr
82
82
  FlickrObject.new
83
83
  end
84
84
 
85
+ it "should not overwrite existing attributes when pulling in a partial new XML document" do
86
+ object = FlickrObject.new(Hpricot.XML('<name>Fleakr</name>'))
87
+ object.populate_from(Hpricot.XML('<desc>Awesome</desc>'))
88
+
89
+ object.name.should == 'Fleakr'
90
+ end
85
91
 
86
92
  end
87
93
 
@@ -2,31 +2,63 @@ require File.dirname(__FILE__) + '/../test_helper'
2
2
 
3
3
  module Fleakr
4
4
  class PhotoTest < Test::Unit::TestCase
5
-
5
+
6
6
  describe "The Photo class" do
7
-
8
- context "when finding all photos by photoset ID" do
9
-
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
10
15
  before do
11
- mock_request_cycle :for => 'photosets.getPhotos', :with => {:photoset_id => '1'}
12
- @photos = Photo.find_all_by_photoset_id('1')
13
- end
14
-
15
- it "should have the correct number of items" do
16
- @photos.length.should == 2
17
- end
18
-
19
- it "should have the proper title" do
20
- @photos.map {|p| p.title}.should == ['Photo #1', 'Photo #2']
16
+ @object = Photo.new(Hpricot.XML(read_fixture('people.getPublicPhotos')).at('rsp/photos/photo'))
21
17
  end
22
-
23
- it "should have the proper id" do
24
- @photos.map {|p| p.id }.should == %w(3044163577 3045001128)
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
25
58
  end
26
-
27
59
  end
28
-
60
+
29
61
  end
30
-
31
62
  end
32
- end
63
+
64
+ end
@@ -4,17 +4,48 @@ module Fleakr
4
4
  class SearchTest < Test::Unit::TestCase
5
5
 
6
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(nil, :tags => 'foo')
10
+ search.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(nil, :tags => %w(foo bar))
15
+ search.tag_list.should == 'foo,bar'
16
+ end
17
+
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'}
21
+ end
22
+
23
+ it "should not have any :tags parameters if none are supplied" do
24
+ search = Search.new
25
+ search.parameters.should == {}
26
+ end
27
+
28
+ it "should convert the search term into the appropriate parameter" do
29
+ search = Search.new('foo')
30
+ search.parameters.should == {:text => 'foo'}
31
+ end
32
+
33
+ it "should be able to search photos based on text" do
34
+ response = mock_request_cycle :for => 'photos.search', :with => {:text => 'foo'}
35
+ search = Search.new('foo')
36
+ search.results
37
+ end
7
38
 
8
39
  it "should be able to search photos based on tags" do
9
40
  response = mock_request_cycle :for => 'photos.search', :with => {:tags => 'one,two'}
10
41
 
11
42
  photo_1, photo_2 = [stub(), stub()]
12
43
  photo_1_doc, photo_2_doc = (response.body/'rsp/photos/photo').map {|doc| doc }
13
-
44
+
14
45
  Photo.expects(:new).with(photo_1_doc).returns(photo_1)
15
46
  Photo.expects(:new).with(photo_2_doc).returns(photo_2)
16
-
17
- search = Search.new(:tags => %w(one two))
47
+
48
+ search = Search.new(nil, :tags => %w(one two))
18
49
  search.results.should == [photo_1, photo_2]
19
50
  end
20
51
 
@@ -26,7 +57,7 @@ module Fleakr
26
57
  Photo.expects(:new).with(doc).once
27
58
  end
28
59
 
29
- search = Search.new(:tags => %w(foo))
60
+ search = Search.new(nil, :tags => %w(foo))
30
61
 
31
62
  2.times { search.results }
32
63
  end
@@ -4,36 +4,25 @@ module Fleakr
4
4
  class SetTest < Test::Unit::TestCase
5
5
 
6
6
  describe "The Set class" do
7
- context "When finding all sets for a user_id" do
8
- before do
9
- user_id = '31066442@N69'
10
- mock_request_cycle :for => 'photosets.getList', :with => {:user_id => user_id}
11
-
12
- @sets = Set.find_all_by_user_id(user_id)
13
- end
14
-
15
- it "should return an array with the expected number of elements" do
16
- @sets.length.should == 2
17
- end
18
-
19
- it "should have the proper titles for each set in the collection" do
20
- @sets.map {|s| s.title }.should == ["Second Set", "First Set"]
21
- end
22
-
23
- it "should have the proper descriptions for each set in the collection" do
24
- @sets.map {|s| s.description }.should == ['This is the second set.', 'This is the first set.']
25
- end
26
-
27
- it "should have the correct IDs for each of the sets" do
28
- @sets.map {|s| s.id }.should == %w(72157609490909659 72157608538140671)
29
- end
30
-
31
- end
7
+
8
+ should_find_all :sets, :by => :user_id, :call => 'photosets.getList', :path => 'rsp/photosets/photoset'
9
+
32
10
  end
33
11
 
34
12
  describe "An instance of the Set class" do
35
- context "when accessing its list of photos" do
13
+
14
+ context "when populating from an XML document" do
15
+ before do
16
+ @object = Set.new(Hpricot.XML(read_fixture('photosets.getList')).at('rsp/photosets/photoset'))
17
+ end
36
18
 
19
+ should_have_a_value_for :id => '72157609490909659'
20
+ should_have_a_value_for :title => 'Second Set'
21
+ should_have_a_value_for :description => 'This is the second set.'
22
+
23
+ end
24
+
25
+ context "when accessing its list of photos" do
37
26
  before do
38
27
  @set = Set.new
39
28
  @set.stubs(:id).with().returns('1')
@@ -51,6 +40,41 @@ module Fleakr
51
40
  Photo.expects(:find_all_by_photoset_id).once.returns([])
52
41
  2.times { @set.photos }
53
42
  end
43
+ end
44
+
45
+ context "when saving the set" do
46
+ before do
47
+ @tmp_dir = create_temp_directory
48
+ end
49
+
50
+ after { FileUtils.rm_rf(@tmp_dir) }
51
+
52
+ it "should save all files of the specified size to the specified directory" do
53
+ image = mock()
54
+ image.expects(:save_to).with("#{@tmp_dir}/set")
55
+
56
+ photo = stub(:small => image)
57
+
58
+ set = Set.new
59
+ set.stubs(:title).with().returns('set')
60
+ set.stubs(:photos).with().returns([photo])
61
+
62
+ FileUtils.expects(:mkdir).with("#{@tmp_dir}/set")
63
+
64
+ set.save_to(@tmp_dir, :small)
65
+ end
66
+
67
+ it "should not create the directory if it already exists" do
68
+ FileUtils.mkdir("#{@tmp_dir}/set")
69
+
70
+ set = Set.new
71
+ set.stubs(:title).with().returns('set')
72
+ set.stubs(:photos).with().returns([])
73
+
74
+ FileUtils.expects(:mkdir).with("#{@tmp_dir}/set").never
75
+
76
+ set.save_to(@tmp_dir, :small)
77
+ end
54
78
 
55
79
  end
56
80
 
@@ -4,68 +4,100 @@ module Fleakr
4
4
  class UserTest < Test::Unit::TestCase
5
5
 
6
6
  describe "The User class" do
7
-
8
- it "should be able to find a user by his username" do
9
- user = stub()
10
- response = mock_request_cycle :for => 'people.findByUsername', :with => {:username => 'frootpantz'}
11
-
12
- User.expects(:new).with(response.body).returns(user)
13
- User.find_by_username('frootpantz').should == user
14
- end
15
-
16
- it "should be able to find a user by his email" do
17
- user = stub()
18
- response = mock_request_cycle :for => 'people.findByEmail', :with => {:find_email => 'frootpantz@example.com'}
19
-
20
- User.expects(:new).with(response.body).returns(user)
21
- User.find_by_email('frootpantz@example.com').should == user
22
- end
23
-
7
+
8
+ should_find_one :user, :by => :username, :call => 'people.findByUsername', :path => 'rsp/user'
9
+ should_find_one :user, :by => :email, :with => :find_email, :call => 'people.findByEmail', :path => 'rsp/user'
10
+
24
11
  end
25
12
 
26
13
  describe "An instance of User" do
14
+ context "when populating the object from an XML document" do
15
+ before do
16
+ @object = User.new(Hpricot.XML(read_fixture('people.findByUsername')))
17
+ @object.populate_from(Hpricot.XML(read_fixture('people.getInfo')))
18
+ end
27
19
 
28
- before do
29
- response_body = Hpricot.XML(read_fixture('people.findByUsername'))
30
- @user = User.new(response_body)
20
+ should_have_a_value_for :id => '31066442@N69'
21
+ should_have_a_value_for :username => 'frootpantz'
22
+ should_have_a_value_for :photos_url => 'http://www.flickr.com/photos/frootpantz/'
23
+ should_have_a_value_for :profile_url => 'http://www.flickr.com/people/frootpantz/'
24
+ should_have_a_value_for :photos_count => '3907'
25
+ should_have_a_value_for :icon_server => '30'
26
+ should_have_a_value_for :icon_farm => '1'
31
27
  end
32
28
 
33
- it "should have a value for :id" do
34
- @user.id.should == '31066442@N69'
35
- end
36
-
37
- it "should have a value for :username" do
38
- @user.username.should == 'frootpantz'
39
- end
40
-
41
- it "should be able to retrieve the user's associated sets" do
42
- sets = [stub()]
43
- @user.stubs(:id).with().returns('1')
44
-
45
- Set.expects(:find_all_by_user_id).with('1').returns(sets)
46
-
47
- @user.sets.should == sets
48
- end
49
-
50
- it "should memoize the results returned for this user's sets" do
51
- Set.expects(:find_all_by_user_id).once.returns([])
52
- 2.times { @user.sets }
53
- end
54
-
55
- it "should be able to retrieve the user's associated groups" do
56
- groups = [stub()]
57
- @user.stubs(:id).with().returns('1')
58
-
59
- Group.expects(:find_all_by_user_id).with('1').returns(groups)
60
-
61
- @user.groups.should == groups
62
- end
63
-
64
- it "should memoize the results returned for this user's group" do
65
- Group.expects(:find_all_by_user_id).once.returns([])
66
- 2.times { @user.groups }
29
+ context "in general" do
30
+
31
+ before do
32
+ @user = User.new
33
+ end
34
+
35
+ it "should be able to retrieve the user's associated sets" do
36
+ sets = [stub()]
37
+ @user.stubs(:id).with().returns('1')
38
+
39
+ Set.expects(:find_all_by_user_id).with('1').returns(sets)
40
+
41
+ @user.sets.should == sets
42
+ end
43
+
44
+ it "should memoize the results returned for this user's sets" do
45
+ Set.expects(:find_all_by_user_id).once.returns([])
46
+ 2.times { @user.sets }
47
+ end
48
+
49
+ it "should be able to retrieve the user's associated groups" do
50
+ groups = [stub()]
51
+ @user.stubs(:id).with().returns('1')
52
+
53
+ Group.expects(:find_all_by_user_id).with('1').returns(groups)
54
+
55
+ @user.groups.should == groups
56
+ end
57
+
58
+ it "should memoize the results returned for this user's group" do
59
+ Group.expects(:find_all_by_user_id).once.returns([])
60
+ 2.times { @user.groups }
61
+ end
62
+
63
+ it "should be able to retrieve the user's photos" do
64
+ photos = [stub()]
65
+ @user.stubs(:id).with().returns('1')
66
+
67
+ Photo.expects(:find_all_by_user_id).with('1').returns(photos)
68
+ @user.photos.should == photos
69
+ end
70
+
71
+ it "should memoize the results returned for this user's photos" do
72
+ Photo.expects(:find_all_by_user_id).once.returns([])
73
+ 2.times { @user.photos }
74
+ end
75
+
76
+ it "should be able to retrieve additional information about the current user" do
77
+ response = mock_request_cycle :for => 'people.getInfo', :with => {:user_id => @user.id}
78
+ @user.expects(:populate_from).with(response.body)
79
+
80
+ @user.load_info
81
+ end
82
+
83
+ it "should be able to generate an icon URL when the :icon_server value is greater than zero" do
84
+ @user.stubs(:icon_server).with().returns('1')
85
+ @user.stubs(:icon_farm).with().returns('2')
86
+ @user.stubs(:id).with().returns('45')
87
+
88
+ @user.icon_url.should == 'http://farm2.static.flickr.com/1/buddyicons/45.jpg'
89
+ end
90
+
91
+ it "should return the default icon URL when the :icon_server value is zero" do
92
+ @user.stubs(:icon_server).with().returns('0')
93
+ @user.icon_url.should == 'http://www.flickr.com/images/buddyicon.jpg'
94
+ end
95
+
96
+ it "should return the default icon URL when the :icon_server value is nil" do
97
+ @user.stubs(:icon_server).with().returns(nil)
98
+ @user.icon_url.should == 'http://www.flickr.com/images/buddyicon.jpg'
99
+ end
67
100
  end
68
-
69
101
  end
70
102
 
71
103
  end
data/test/test_helper.rb CHANGED
@@ -4,10 +4,59 @@ require 'rubygems'
4
4
  require 'matchy'
5
5
  require 'context'
6
6
  require 'mocha'
7
+ require 'activesupport'
7
8
 
8
9
  require File.dirname(__FILE__) + '/../lib/fleakr'
9
10
 
10
11
  class Test::Unit::TestCase
12
+
13
+ def self.should_have_a_value_for(attribute_test)
14
+ it "should have a value for :#{attribute_test.keys.first}" do
15
+ @object.send(attribute_test.keys.first).should == attribute_test.values.first
16
+ end
17
+ end
18
+
19
+ def self.should_find_one(thing, options)
20
+ class_name = thing.to_s.singularize.camelcase
21
+ klass = "Fleakr::#{class_name}".constantize
22
+ object_type = class_name.downcase
23
+
24
+ options[:with] = options[:by] if options[:with].nil?
25
+
26
+ it "should be able to find a #{thing} by #{options[:by]}" do
27
+ condition_value = '1'
28
+ stub = stub()
29
+ response = mock_request_cycle :for => options[:call], :with => {options[:with] => condition_value}
30
+
31
+ klass.expects(:new).with(response.body).returns(stub)
32
+ klass.send("find_by_#{options[:by]}".to_sym, condition_value).should == stub
33
+ end
34
+ end
35
+
36
+ def self.should_find_all(thing, options)
37
+ class_name = thing.to_s.singularize.camelcase
38
+ klass = "Fleakr::#{class_name}".constantize
39
+ object_type = class_name.downcase
40
+
41
+ it "should be able to find all #{thing} by #{options[:by]}" do
42
+ condition_value = '1'
43
+ response = mock_request_cycle :for => options[:call], :with => {options[:by] => condition_value}
44
+
45
+ stubs = []
46
+ elements = (response.body/options[:path]).map
47
+
48
+
49
+ elements.each do |element|
50
+ stub = stub()
51
+ stubs << stub
52
+
53
+ klass.expects(:new).with(element).returns(stub)
54
+ end
55
+
56
+ klass.send("find_all_by_#{options[:by]}".to_sym, condition_value).should == stubs
57
+ end
58
+
59
+ end
11
60
 
12
61
  def read_fixture(method_call)
13
62
  fixture_path = File.dirname(__FILE__) + '/fixtures'
@@ -21,4 +70,11 @@ class Test::Unit::TestCase
21
70
  response
22
71
  end
23
72
 
73
+ def create_temp_directory
74
+ tmp_dir = File.dirname(__FILE__) + '/tmp'
75
+ FileUtils.mkdir(tmp_dir)
76
+
77
+ tmp_dir
78
+ end
79
+
24
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reagent-fleakr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Reagan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-02 00:00:00 -08:00
12
+ date: 2008-12-03 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -36,6 +36,7 @@ files:
36
36
  - lib/fleakr/attribute.rb
37
37
  - lib/fleakr/error.rb
38
38
  - lib/fleakr/group.rb
39
+ - lib/fleakr/image.rb
39
40
  - lib/fleakr/object.rb
40
41
  - lib/fleakr/photo.rb
41
42
  - lib/fleakr/request.rb
@@ -48,7 +49,9 @@ files:
48
49
  - test/fixtures
49
50
  - test/fixtures/people.findByEmail.xml
50
51
  - test/fixtures/people.findByUsername.xml
52
+ - test/fixtures/people.getInfo.xml
51
53
  - test/fixtures/people.getPublicGroups.xml
54
+ - test/fixtures/people.getPublicPhotos.xml
52
55
  - test/fixtures/photos.search.xml
53
56
  - test/fixtures/photosets.getList.xml
54
57
  - test/fixtures/photosets.getPhotos.xml
@@ -56,6 +59,7 @@ files:
56
59
  - test/fleakr/attribute_test.rb
57
60
  - test/fleakr/error_test.rb
58
61
  - test/fleakr/group_test.rb
62
+ - test/fleakr/image_test.rb
59
63
  - test/fleakr/object_test.rb
60
64
  - test/fleakr/photo_test.rb
61
65
  - test/fleakr/request_test.rb