Skip to content

Commit

Permalink
Provided facility to retrieve resources based on their Flickr URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Reagan committed Feb 25, 2010
1 parent f289447 commit 2a92011
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/fleakr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class ApiError < StandardError; end
# Fleakr.user('user@host.com')
# Fleakr.user('http://www.flickr.com/photos/the_decapitator/')
#

# TODO: Use User.find_by_identifier for some of this
def self.user(user_data, options = {})
user = nil
[:username, :email, :url].each do |attribute|
Expand Down Expand Up @@ -195,6 +197,10 @@ def self.user_for_token(auth_token)
token = Fleakr::Objects::AuthenticationToken.from_auth_token(auth_token)
token.user
end

def self.resource_from_url(url)
Fleakr::Objects::Url.new(url).resource
end

end

Expand Down
3 changes: 2 additions & 1 deletion lib/fleakr/objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
require 'fleakr/objects/tag'
require 'fleakr/objects/search'
require 'fleakr/objects/set'
require 'fleakr/objects/user'
require 'fleakr/objects/user'
require 'fleakr/objects/url'
72 changes: 72 additions & 0 deletions lib/fleakr/objects/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module Fleakr
module Objects

class Url

def initialize(url)
@url = url
end

def path
URI.parse(@url).path
end

def user_identifier
(resource_type == Set) ? parts[1] : parts[2]
end

# TODO: support identifier for shortened Flickr URLs
def resource_identifier
parts[3]
end

def user
@user ||= User.find_by_identifier(user_identifier)
end

def resource_type
if parts[1] == 'people'
User
elsif parts[1] == 'photos'
Photo
elsif parts[2] == 'sets'
Set
end
end

def collection?
resource_identifier.nil?
end

def resource
if resource_type == User
user
else
collection? ? resource_type.find_all_by_user_id(user.id) : resource_type.find_by_id(resource_identifier)
end
end

private

def parts
path.match(matching_pattern)
end

def matching_pattern
@matching_pattern ||= patterns.detect {|p| path.match(p) }
end

def patterns
[
%r{^/photos/([^/]+)/(sets)/(\d+)},
%r{^/photos/([^/]+)/(sets)},
%r{^/(photos)/([^/]+)/(\d+)},
%r{^/(people)/([^/]+)},
%r{^/(photos)/([^/]+)}
]
end

end

end
end
8 changes: 8 additions & 0 deletions lib/fleakr/objects/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ class User

scoped_search

def self.user_id?(username_or_user_id)
(username_or_user_id =~ /^\d+@N\d{2}$/) ? true : false
end

def self.find_by_identifier(identifier)
user_id?(identifier) ? find_by_id(identifier) : find_by_username(identifier)
end

# Is this a pro account?
def pro?
(self.pro.to_i == 0) ? false : true
Expand Down
171 changes: 171 additions & 0 deletions test/unit/fleakr/objects/url_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
require File.dirname(__FILE__) + '/../../../test_helper'

module Fleakr::Objects
class UrlTest < Test::Unit::TestCase

context "An instance of the Url class" do

should "know the path" do
u = Url.new('http://www.flickr.com/photos/reagent/4041660453/')
u.path.should == '/photos/reagent/4041660453/'
end

should "know the path when there is no hostname" do
u = Url.new('http://flickr.com/photos/reagent/4041660453/')
u.path.should == '/photos/reagent/4041660453/'
end

should "know the path when using the shortened URL" do
u = Url.new('http://flic.kr/p/7a9yQV')
u.path.should == '/p/7a9yQV'
end

should "be able to retrieve a user" do
u = Url.new('')
u.stubs(:user_identifier).with().returns('reagent')

User.expects(:find_by_identifier).with('reagent').returns('user')

u.user.should == 'user'
end

should "memoize the user" do
u = Url.new('')
u.stubs(:user_identifier).with().returns('reagent')

User.expects(:find_by_identifier).with('reagent').once.returns('user')

2.times { u.user }
end

context "when provided a single photo URL" do
subject { Url.new('http://flickr.com/photos/reagent/4041660453/') }

should "know that it's retrieving a photo resource" do
subject.resource_type.should == Photo
end

should "know the :user_identifier" do
subject.user_identifier.should == 'reagent'
end

should "know the :resource_identifier" do
subject.resource_identifier.should == '4041660453'
end

should "know that it's not retrieving a collection of resources" do
subject.collection?.should be(false)
end

should "return the resource" do
Fleakr::Objects::Photo.expects(:find_by_id).with('4041660453').returns('photo')
subject.resource.should == 'photo'
end
end

context "when provided with a photoset URL" do
subject { @subject ||= Url.new('http://www.flickr.com/photos/reagent/') }

should "know that it's retrieving a Photo resource" do
subject.resource_type.should == Photo
end

should "know the :user_identifier" do
subject.user_identifier.should == 'reagent'
end

should "not have a :resource_identifier" do
subject.resource_identifier.should be_nil
end

should "know that it's retrieving a collection of resources" do
subject.collection?.should be(true)
end

should "return the resource" do
subject.stubs(:user).with().returns(stub(:id => '1'))

Fleakr::Objects::Photo.expects(:find_all_by_user_id).with('1').returns('photos')
subject.resource.should == 'photos'
end

end

context "when provided with a profile URL" do
subject { @subject ||= Url.new('http://www.flickr.com/people/reagent/') }

should "know that it's retrieving a user resource" do
subject.resource_type.should == User
end

should "know the :user_identifier" do
subject.user_identifier.should == 'reagent'
end

should "not have a :resource_identifier" do
subject.resource_identifier.should be_nil
end

should "return the resource" do
subject.expects(:user).with().returns('user')
subject.resource.should == 'user'
end
end

context "when provided a profile URL with a user ID" do
subject { Url.new('http://www.flickr.com/people/43955217@N05/') }

should "know the :user_identifier" do
subject.user_identifier.should == '43955217@N05'
end

end

context "when provided with a single set URL" do
subject { Url.new('http://www.flickr.com/photos/reagent/sets/72157622660138146/') }

should "know that it's retrieving a set resource" do
subject.resource_type.should == Set
end

should "know the :user_identifier" do
subject.user_identifier.should == 'reagent'
end

should "know the :resource_identifier" do
subject.resource_identifier.should == '72157622660138146'
end

should "return the resource" do
Fleakr::Objects::Set.expects(:find_by_id).with('72157622660138146').returns('set')
subject.resource.should == 'set'
end
end

context "when provided a set listing URL" do
subject { @subject ||= Url.new('http://www.flickr.com/photos/reagent/sets/') }

should "know that it's retrieving a set resource" do
subject.resource_type.should == Set
end

should "know the :user_identifier" do
subject.user_identifier.should == 'reagent'
end

should "not have a :resource_identifier" do
subject.resource_identifier.should be_nil
end

should "return the resource" do
subject.stubs(:user).with().returns(stub(:id => '1'))

Fleakr::Objects::Set.expects(:find_all_by_user_id).with('1').returns('sets')
subject.resource.should == 'sets'
end
end

end

end
end
22 changes: 22 additions & 0 deletions test/unit/fleakr/objects/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ class UserTest < Test::Unit::TestCase
should_find_one :user, :by => :id, :with => :user_id, :call => 'people.getInfo', :path => 'rsp/person'
should_find_one :user, :by => :url, :call => 'urls.lookupUser', :path => 'rsp/user'

should "recognize a string as not being a Flickr user ID" do
User.user_id?('reagent').should be(false)
end

should "recognize a string as being a Flickr user ID" do
User.user_id?('43955217@N05').should be(true)
end

should "be able to find a user by ID when supplied with an identifier" do
id = '43955217@N05'
User.expects(:find_by_id).with(id).returns('user')

User.find_by_identifier(id).should == 'user'
end

should "be able to find a user by username when supplied with an identifier" do
username = 'reagent'
User.expects(:find_by_username).with(username).returns('user')

User.find_by_identifier(username).should == 'user'
end

end

context "An instance of User" do
Expand Down
7 changes: 7 additions & 0 deletions test/unit/fleakr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ class FleakrTest < Test::Unit::TestCase
Fleakr.user_for_token('toke').should == 'user'
end

should "be able to find the correct resource for a URL" do
url = stub() {|u| u.stubs(:resource).with().returns('resource') }
Fleakr::Objects::Url.expects(:new).with('url').returns(url)

Fleakr.resource_from_url('url').should == 'resource'
end

end

end

0 comments on commit 2a92011

Please sign in to comment.