Skip to content

Commit

Permalink
* Updated and refactored Flickr::Group class and Flickr#groups method…
Browse files Browse the repository at this point in the history
… to work with current Flickr API.

Flickr#groups now searches for given group, rather than groups.getActiveList (which no longer exists as Flickr API call)
  • Loading branch information
Chris Taggart committed May 12, 2008
1 parent f73d71b commit 12a6397
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 29 deletions.
2 changes: 2 additions & 0 deletions History.txt
@@ -1,5 +1,7 @@
== 1.0.5 2008-05-12

* 1 major enhancement:
* Updated and refactored Flickr::Group class and Flickr#groups method to work with current Flickr API. Flickr#groups now searches for given group, rather than groups.getActiveList (which no longer exists as Flickr API call)
* Minor enhancements:
* Tweaked internals so new client instance isn't created each time new object (e.g. photo, user) is created
* Improved test coverage
Expand Down
21 changes: 14 additions & 7 deletions lib/flickr.rb
Expand Up @@ -148,9 +148,12 @@ def users(lookup=nil)
return User.new("id" => user["nsid"], "username" => user["username"], "client" => self)
end

# Implements flickr.groups.getActiveList
def groups
groups_getActiveList['activegroups']['group'].collect { |group| Group.new(group['nsid'], @api_key) }
# Implements flickr.groups.search
def groups(group_name, options={})
groups_search({"text" => group_name}.merge(options))['groups']['group'].collect { |group| Group.new( "id" => group['nsid'],
"name" => group['name'],
"eighteenplus" => group['eighteenplus'],
"client" => self) }
end

# Implements flickr.tags.getRelated
Expand Down Expand Up @@ -581,10 +584,14 @@ def uri_for_photo_from_self(size=nil)
class Group
attr_reader :id, :client, :name, :members, :online, :privacy, :chatid, :chatcount, :url

def initialize(id=nil, api_key=nil)
@id = id
@api_key = api_key
@client = Flickr.new @api_key
def initialize(id_or_params_hash=nil, api_key=nil)
if id_or_params_hash.is_a?(Hash)
id_or_params_hash.each { |k,v| self.instance_variable_set("@#{k}", v) } # convert extra_params into instance variables
else
@id = id_or_params_hash
@api_key = api_key
@client = Flickr.new @api_key
end
end

# Implements flickr.groups.getInfo and flickr.urls.getGroup
Expand Down
120 changes: 98 additions & 22 deletions test/test_flickr.rb
Expand Up @@ -7,7 +7,7 @@ class TestFlickr < Test::Unit::TestCase

# Flickr client tests
#
# instatiation tests
# instantiation tests
def test_should_instantiate_new_flickr_client
Flickr.any_instance.stubs(:login)
flickr = Flickr.new('some_api_key', 'email@test.com', 'some_password', 'some_shared_secret')
Expand All @@ -34,7 +34,7 @@ def test_should_not_try_to_login_using_old_api_when_instantiate_new_flickr_clien
flickr = Flickr.new('api_key' => 'some_api_key', 'email' => 'email@test.com', 'password' => 'some_password', 'shared_secret' => 'some_shared_secret', 'foo' => 'bar')
end

# signature_from tests
# signature_from method tests
def test_should_return_signature_from_given_params
assert_equal Digest::MD5.hexdigest('shared_secret_codea_param1234xb_param5678yc_param97531t'),
authenticated_flickr_client.send(:signature_from, {:b_param => '5678y', 'c_param' => '97531t', :a_param => '1234x', :d_param => nil})
Expand All @@ -44,7 +44,7 @@ def test_should_return_nil_for_signature_when_no_shared_secret
assert_nil flickr_client.send(:signature_from, {:b_param => '5678y', :c_param => '97531t', :a_param => '1234x'})
end

# request_url tests
# request_url method tests
def test_should_get_signature_for_params_when_building_url
f = authenticated_flickr_client
f.expects(:signature_from).with( 'method' => 'flickr.someMethod',
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_should_generate_flickr_method_from_unkown_method_on_flickr_client
f.some_unknown_methodForFlickr
end

# request tests
# request method tests
def test_should_make_successful_request
f = flickr_client
f.expects(:http_get).with('some.url').returns(successful_xml_response)
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_should_store_authenticated_user_details_in_client
assert_equal f, user.client
end

# photos tests
# photos method tests
def test_should_get_recent_photos_if_no_params_for_photos
f = flickr_client
f.expects(:photos_getRecent).returns({"photos" => {"photo" => []}})
Expand All @@ -180,15 +180,15 @@ def test_should_instantiate_recent_photos_with_id_and_all_params_returned_by_fli
f.photos
end

# photos_search tests
# photos_search method tests
def test_should_search_photos
f = authenticated_flickr_client
f.expects(:request).with('photos.search', anything).returns(dummy_photos_response)
photos = f.photos_search
assert_kind_of Flickr::Photo, photos.first
end

# users tests
# users method tests
def test_should_find_user_from_email
f = flickr_client
f.expects(:request).with('people.findByEmail', anything).returns(dummy_user_response)
Expand All @@ -211,6 +211,30 @@ def test_should_pass_on_flickr_client_when_finding_user
assert_equal f, user.client
end

# groups method tests
def test_should_search_for_given_group
f = flickr_client
f.expects(:request).with("groups.search", {"text" => "foo"}).returns(dummy_groups_response)
f.groups("foo")
end

def test_should_search_for_given_group_with_additional_params
f = flickr_client
f.expects(:request).with("groups.search", {"text" => "foo", "per_page" => "1"}).returns(dummy_groups_response)
f.groups("foo", "per_page" => "1")
end

def test_should_instantiate_groups_from_search_response
f = flickr_client
f.stubs(:request).returns(dummy_groups_response)
assert_kind_of Array, groups = f.groups("foo")
assert_kind_of Flickr::Group, group = groups.first
assert_equal "group1", group.id
assert_equal "Group One", group.name
assert_equal "0", group.instance_variable_get(:@eighteenplus)
assert_equal f, group.client
end

# ##### DIRECT MODE
#
# def test_test_echo
Expand All @@ -232,15 +256,6 @@ def test_should_pass_on_flickr_client_when_finding_user
# assert_equal @user_id, @f.find_by_url(@user_url).getInfo.id # find user by URL
# end
#
# def test_photos
# assert_equal 100, @f.photos.size # find recent
# assert_equal @user_id, @f.photos('user_id'=>@user_id).first.getInfo.owner.id # search by user_id
# end
#
# def test_groups
# assert_kind_of Flickr::Group, @f.groups.first # find all active groups
# end
#
# def test_licenses
# assert_kind_of Array, @f.licenses # find all licenses
# end
Expand All @@ -251,7 +266,7 @@ def test_should_pass_on_flickr_client_when_finding_user
#


# ##### USER
# ##### Flickr::User tests
#
def test_should_instantiate_user
user = Flickr::User.new({ 'id' => 'foo123',
Expand Down Expand Up @@ -414,9 +429,9 @@ def test_should_instantiate_contacts_photos_with_id_and_all_params_returned_by_q
user.contactsPhotos
end

##### PHOTO
# ##### Flickr::Photo tests

def test_should_store_initialize_from_id
def test_should_initialize_photo_from_id
photo = Flickr::Photo.new("foo123")
assert_equal "foo123", photo.id
end
Expand Down Expand Up @@ -599,6 +614,56 @@ def test_should_not_instantiate_context_photos_with_id_of_0
photo.context
end

# ##### Flickr::Group tests
#
def test_should_instantiate_group_from_id
group = Flickr::Group.new("group1")
assert_equal "group1", group.id
end

# tests old api for instantiating groups
def test_should_instantiate_group_from_id_and_api_key
f = flickr_client
Flickr.expects(:new).with("some_api_key").returns(f)
group = Flickr::Group.new("group1", "some_api_key")
assert_equal f, group.client
end

# new api for instantiating groups
def test_should_instantiate_group_from_params_hash
group = Flickr::Group.new("id" => "group1", "name" => "Group One", "foo" => "bar")
assert_equal "group1", group.id
assert_equal "Group One", group.name
assert_equal "bar", group.instance_variable_get(:@foo)
end

def test_should_use_flickr_client_passed_in_params_hash_when_instantiating_group
f = flickr_client
Flickr.expects(:new).never
group = Flickr::Group.new("id" => "group1", "name" => "Group One", "client" => f)
assert_equal f, group.client
end

# def test_should_initialize_photo_from_id
# photo = Flickr::Photo.new("foo123")
# assert_equal "foo123", photo.id
# end
#
# def test_should_save_extra_params_as_instance_variables
# photo = Flickr::Photo.new('foo123', 'some_api_key', { 'key1' => 'value1', 'key2' => 'value2'})
# assert_equal 'value1', photo.instance_variable_get(:@key1)
# assert_equal 'value2', photo.instance_variable_get(:@key2)
# end
#
# def test_should_be_able_to_access_instance_variables_through_hash_like_interface
# photo = Flickr::Photo.new
# photo.instance_variable_set(:@key1, 'value1')
# assert_equal 'value1', photo['key1']
# assert_equal 'value1', photo[:key1]
# assert_nil photo[:key2]
# assert_nil photo['key2']
# end

# ##### PHOTOSETS
#
# #def setup
Expand Down Expand Up @@ -674,15 +739,15 @@ def dummy_photos_response
"key1" => "value1",
"key2" => "value2" },
{ "id" => "bar456",
"key3" => "value3"}] } }
"key3" => "value3"}] } }
end

def dummy_single_photo_response
{ "photos" =>
{ "photo" =>
{ "id" => "foo123",
"key1" => "value1",
"key2" => "value2" } } }
"key1" => "value1",
"key2" => "value2" } } }
end

def dummy_user_response
Expand All @@ -692,6 +757,17 @@ def dummy_user_response
}
end

def dummy_groups_response
{ "groups" =>
{ "group" =>
[{ "nsid" => "group1",
"name" => "Group One",
"eighteenplus" => "0" },
{ "nsid" => "group2",
"name" => "Group Two",
"eighteenplus" => "1"}] } }
end

def successful_xml_response
<<-EOF
<?xml version="1.0" encoding="utf-8" ?>
Expand Down

0 comments on commit 12a6397

Please sign in to comment.