Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :rubygems
source 'https://rubygems.org'

gemspec

Expand All @@ -9,4 +9,4 @@ group :development do
gem "mocha", "0.9.8"
gem "blankslate", "~> 2.1.2.0"
gem "hanna", "~> 0.1.0"
end
end
2 changes: 1 addition & 1 deletion fleakr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.date = %q{2011-01-16}
s.email = %q{[email protected]}
s.extra_rdoc_files = ["README.rdoc"]
s.files = ["README.rdoc", "Rakefile", "lib/fleakr", "lib/fleakr/api", "lib/fleakr/api/authentication_request.rb", "lib/fleakr/api/file_parameter.rb", "lib/fleakr/api/method_request.rb", "lib/fleakr/api/option.rb", "lib/fleakr/api/parameter_list.rb", "lib/fleakr/api/response.rb", "lib/fleakr/api/upload_request.rb", "lib/fleakr/api/value_parameter.rb", "lib/fleakr/api.rb", "lib/fleakr/core_ext", "lib/fleakr/core_ext/false_class.rb", "lib/fleakr/core_ext/hash.rb", "lib/fleakr/core_ext/true_class.rb", "lib/fleakr/core_ext.rb", "lib/fleakr/objects", "lib/fleakr/objects/authentication_token.rb", "lib/fleakr/objects/collection.rb", "lib/fleakr/objects/comment.rb", "lib/fleakr/objects/contact.rb", "lib/fleakr/objects/error.rb", "lib/fleakr/objects/group.rb", "lib/fleakr/objects/image.rb", "lib/fleakr/objects/metadata.rb", "lib/fleakr/objects/metadata_collection.rb", "lib/fleakr/objects/photo.rb", "lib/fleakr/objects/photo_context.rb", "lib/fleakr/objects/search.rb", "lib/fleakr/objects/set.rb", "lib/fleakr/objects/tag.rb", "lib/fleakr/objects/url.rb", "lib/fleakr/objects/user.rb", "lib/fleakr/objects.rb", "lib/fleakr/support", "lib/fleakr/support/attribute.rb", "lib/fleakr/support/object.rb", "lib/fleakr/support/request.rb", "lib/fleakr/support/url_expander.rb", "lib/fleakr/support/utility.rb", "lib/fleakr/support.rb", "lib/fleakr/version.rb", "lib/fleakr.rb", "test/fixtures", "test/fixtures/auth.checkToken.xml", "test/fixtures/auth.getFullToken.xml", "test/fixtures/auth.getToken.xml", "test/fixtures/collections.getInfo.xml", "test/fixtures/collections.getTree.xml", "test/fixtures/contacts.getList.xml", "test/fixtures/contacts.getPublicList.xml", "test/fixtures/groups.pools.getPhotos.xml", "test/fixtures/people.findByEmail.xml", "test/fixtures/people.findByUsername.xml", "test/fixtures/people.getInfo.xml", "test/fixtures/people.getPublicGroups.xml", "test/fixtures/people.getPublicPhotos.xml", "test/fixtures/photos.comments.getList.xml", "test/fixtures/photos.getContext.xml", "test/fixtures/photos.getExif.xml", "test/fixtures/photos.getInfo.xml", "test/fixtures/photos.getSizes.xml", "test/fixtures/photos.search.xml", "test/fixtures/photosets.comments.getList.xml", "test/fixtures/photosets.getInfo.xml", "test/fixtures/photosets.getList.xml", "test/fixtures/photosets.getPhotos.xml", "test/fixtures/tags.getListPhoto.xml", "test/fixtures/tags.getListUser.xml", "test/fixtures/tags.getRelated.xml", "test/fixtures/urls.lookupUser.xml", "test/test_helper.rb", "test/unit", "test/unit/fleakr", "test/unit/fleakr/api", "test/unit/fleakr/api/authentication_request_test.rb", "test/unit/fleakr/api/file_parameter_test.rb", "test/unit/fleakr/api/method_request_test.rb", "test/unit/fleakr/api/option_test.rb", "test/unit/fleakr/api/parameter_list_test.rb", "test/unit/fleakr/api/response_test.rb", "test/unit/fleakr/api/upload_request_test.rb", "test/unit/fleakr/api/value_parameter_test.rb", "test/unit/fleakr/core_ext", "test/unit/fleakr/core_ext/false_class_test.rb", "test/unit/fleakr/core_ext/hash_test.rb", "test/unit/fleakr/core_ext/true_class_test.rb", "test/unit/fleakr/objects", "test/unit/fleakr/objects/authentication_token_test.rb", "test/unit/fleakr/objects/collection_test.rb", "test/unit/fleakr/objects/comment_test.rb", "test/unit/fleakr/objects/contact_test.rb", "test/unit/fleakr/objects/error_test.rb", "test/unit/fleakr/objects/group_test.rb", "test/unit/fleakr/objects/image_test.rb", "test/unit/fleakr/objects/metadata_collection_test.rb", "test/unit/fleakr/objects/metadata_test.rb", "test/unit/fleakr/objects/photo_context_test.rb", "test/unit/fleakr/objects/photo_test.rb", "test/unit/fleakr/objects/search_test.rb", "test/unit/fleakr/objects/set_test.rb", "test/unit/fleakr/objects/tag_test.rb", "test/unit/fleakr/objects/url_test.rb", "test/unit/fleakr/objects/user_test.rb", "test/unit/fleakr/support", "test/unit/fleakr/support/attribute_test.rb", "test/unit/fleakr/support/object_test.rb", "test/unit/fleakr/support/request_test.rb", "test/unit/fleakr/support/url_expander_test.rb", "test/unit/fleakr/support/utility_test.rb", "test/unit/fleakr_test.rb"]
s.files = ["README.rdoc", "Rakefile", "lib/fleakr", "lib/fleakr/api", "lib/fleakr/api/authentication_request.rb", "lib/fleakr/api/file_parameter.rb", "lib/fleakr/api/method_request.rb", "lib/fleakr/api/option.rb", "lib/fleakr/api/parameter_list.rb", "lib/fleakr/api/response.rb", "lib/fleakr/api/upload_request.rb", "lib/fleakr/api/value_parameter.rb", "lib/fleakr/api.rb", "lib/fleakr/objects", "lib/fleakr/objects/authentication_token.rb", "lib/fleakr/objects/collection.rb", "lib/fleakr/objects/comment.rb", "lib/fleakr/objects/contact.rb", "lib/fleakr/objects/error.rb", "lib/fleakr/objects/group.rb", "lib/fleakr/objects/image.rb", "lib/fleakr/objects/metadata.rb", "lib/fleakr/objects/metadata_collection.rb", "lib/fleakr/objects/photo.rb", "lib/fleakr/objects/photo_context.rb", "lib/fleakr/objects/search.rb", "lib/fleakr/objects/set.rb", "lib/fleakr/objects/tag.rb", "lib/fleakr/objects/url.rb", "lib/fleakr/objects/user.rb", "lib/fleakr/objects.rb", "lib/fleakr/support", "lib/fleakr/support/attribute.rb", "lib/fleakr/support/object.rb", "lib/fleakr/support/request.rb", "lib/fleakr/support/url_expander.rb", "lib/fleakr/support/utility.rb", "lib/fleakr/support.rb", "lib/fleakr/version.rb", "lib/fleakr.rb", "test/fixtures", "test/fixtures/auth.checkToken.xml", "test/fixtures/auth.getFullToken.xml", "test/fixtures/auth.getToken.xml", "test/fixtures/collections.getInfo.xml", "test/fixtures/collections.getTree.xml", "test/fixtures/contacts.getList.xml", "test/fixtures/contacts.getPublicList.xml", "test/fixtures/groups.pools.getPhotos.xml", "test/fixtures/people.findByEmail.xml", "test/fixtures/people.findByUsername.xml", "test/fixtures/people.getInfo.xml", "test/fixtures/people.getPublicGroups.xml", "test/fixtures/people.getPublicPhotos.xml", "test/fixtures/photos.comments.getList.xml", "test/fixtures/photos.getContext.xml", "test/fixtures/photos.getExif.xml", "test/fixtures/photos.getInfo.xml", "test/fixtures/photos.getSizes.xml", "test/fixtures/photos.search.xml", "test/fixtures/photosets.comments.getList.xml", "test/fixtures/photosets.getInfo.xml", "test/fixtures/photosets.getList.xml", "test/fixtures/photosets.getPhotos.xml", "test/fixtures/tags.getListPhoto.xml", "test/fixtures/tags.getListUser.xml", "test/fixtures/tags.getRelated.xml", "test/fixtures/urls.lookupUser.xml", "test/test_helper.rb", "test/unit", "test/unit/fleakr", "test/unit/fleakr/api", "test/unit/fleakr/api/authentication_request_test.rb", "test/unit/fleakr/api/file_parameter_test.rb", "test/unit/fleakr/api/method_request_test.rb", "test/unit/fleakr/api/option_test.rb", "test/unit/fleakr/api/parameter_list_test.rb", "test/unit/fleakr/api/response_test.rb", "test/unit/fleakr/api/upload_request_test.rb", "test/unit/fleakr/api/value_parameter_test.rb", "test/unit/fleakr/objects", "test/unit/fleakr/objects/authentication_token_test.rb", "test/unit/fleakr/objects/collection_test.rb", "test/unit/fleakr/objects/comment_test.rb", "test/unit/fleakr/objects/contact_test.rb", "test/unit/fleakr/objects/error_test.rb", "test/unit/fleakr/objects/group_test.rb", "test/unit/fleakr/objects/image_test.rb", "test/unit/fleakr/objects/metadata_collection_test.rb", "test/unit/fleakr/objects/metadata_test.rb", "test/unit/fleakr/objects/photo_context_test.rb", "test/unit/fleakr/objects/photo_test.rb", "test/unit/fleakr/objects/search_test.rb", "test/unit/fleakr/objects/set_test.rb", "test/unit/fleakr/objects/tag_test.rb", "test/unit/fleakr/objects/url_test.rb", "test/unit/fleakr/objects/user_test.rb", "test/unit/fleakr/support", "test/unit/fleakr/support/attribute_test.rb", "test/unit/fleakr/support/object_test.rb", "test/unit/fleakr/support/request_test.rb", "test/unit/fleakr/support/url_expander_test.rb", "test/unit/fleakr/support/utility_test.rb", "test/unit/fleakr_test.rb"]
s.homepage = %q{http://fleakr.org}
s.rdoc_options = ["--main", "README.rdoc"]
s.require_paths = ["lib"]
Expand Down
30 changes: 15 additions & 15 deletions lib/fleakr/api/method_request.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
module Fleakr
module Api # :nodoc:

# = MethodRequest
#
#
# Handles all API requests that are non-upload related. For upload requests see the
# UploadRequest class.
#
class MethodRequest

include Fleakr::Support::Request

attr_reader :method

# Makes a request to the Flickr API and returns a valid Response object. If
# there are errors on the response it will raise an ApiError exception. See
# there are errors on the response it will raise an ApiError exception. See
# MethodRequest#new for details about the additional parameters
#
def self.with_response!(method, additional_parameters = {})
request = self.new(method, additional_parameters)
response = request.send

raise(Fleakr::ApiError, "Code: #{response.error.code} - #{response.error.message}") if response.error?

response
end

# Create a new request for the specified API method and pass along any additional
# parameters. The Flickr API uses namespacing for its methods - this is optional
# when calling this method.
#
# This must be called after initializing the library with the required API key
# see (#Fleakr.api_key=)
#
# The <tt>additional_parameters</tt> is a list of parameters to pass directly to
# The <tt>additional_parameters</tt> is a list of parameters to pass directly to
# the Flickr API call. The exception to this is the <tt>:authenticate?</tt> option
# that will force the call to not be authenticated if it is set to false (The default
# behavior is to authenticate all calls when we have a token).
Expand All @@ -41,25 +41,25 @@ def initialize(method, additional_parameters = {})
super(additional_parameters)
self.method = method
end

def method=(method) # :nodoc:
@method = method.sub(/^(flickr\.)?/, 'flickr.')
parameters.add_option(:method, @method)
end

def endpoint_url
'http://api.flickr.com/services/rest/'
'https://api.flickr.com/services/rest/'
end

def send # :nodoc:
logger.info("Sending request to: #{endpoint_uri}")
response_xml = Net::HTTP.get(endpoint_uri)
logger.debug("Response data:\n#{response_xml}")

Response.new(response_xml)
end

end

end
end
end
32 changes: 16 additions & 16 deletions lib/fleakr/api/upload_request.rb
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
module Fleakr
module Api # :nodoc:

# = UploadRequest
#
#
# This implements the upload functionality of the Flickr API which is needed
# to create new photos and replace the photo content of existing photos
#
class UploadRequest

ENDPOINT_URIS = {
:create => 'http://api.flickr.com/services/upload/',
:update => 'http://api.flickr.com/services/replace/'
:create => 'https://api.flickr.com/services/upload/',
:update => 'https://api.flickr.com/services/replace/'
}

attr_reader :parameters, :type

# Send a request and return a Response object. If an API error occurs, this raises
# a Fleakr::ApiError with the reason for the error. See UploadRequest#new for more
# a Fleakr::ApiError with the reason for the error. See UploadRequest#new for more
# details.
#
def self.with_response!(filename, type = :create, options = {})
request = self.new(filename, type, options)
response = request.send

raise(Fleakr::ApiError, "Code: #{response.error.code} - #{response.error.message}") if response.error?

response
end

# Create a new UploadRequest with the specified filename, type, and options. Type
# is one of <tt>:create</tt> or <tt>:update</tt> to specify whether we are saving a new
# image or replacing an existing one.
# image or replacing an existing one.
#
# For a list of available options, see the documentation in Fleakr::Objects::Photo
#
def initialize(filename, type = :create, options = {})
@type = type
@options = options

@parameters = ParameterList.new(upload_options)
parameters.add_upload_option(:photo, filename)
end

# A list of upload options for this upload request (see Fleakr::Api::Option)
#
def upload_options
option_list = @options.map {|key, value| Option.for(key, value) }
option_list.inject({}) {|hash, option| hash.merge(option.to_hash)}
end

def headers # :nodoc:
{'Content-Type' => "multipart/form-data; boundary=#{self.parameters.boundary}"}
end

def send # :nodoc:
response = Net::HTTP.start(endpoint_uri.host, endpoint_uri.port) do |http|
response = Net::HTTP.start(endpoint_uri.host, endpoint_uri.port) do |http|
logger.info("Sending upload request to: #{endpoint_uri}")
logger.debug("Request data:\n#{self.parameters.to_form}")
logger.debug("Request headers:\n#{self.headers.inspect}")

http.post(endpoint_uri.path, self.parameters.to_form, self.headers)
end
logger.debug("Response data:\n#{response.body}")
Response.new(response.body)
end

private
def endpoint_uri
@endpoint_uri ||= URI.parse(ENDPOINT_URIS[self.type])
end
end

end
end
end
4 changes: 2 additions & 2 deletions test/unit/fleakr/api/method_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MethodRequestTest < Test::Unit::TestCase

should "know the endpoint URL" do
request = MethodRequest.new('people.findByUsername')
request.endpoint_url.should == 'http://api.flickr.com/services/rest/'
request.endpoint_url.should == 'https://api.flickr.com/services/rest/'
end

should "add the method name to the list of parameters" do
Expand Down Expand Up @@ -73,4 +73,4 @@ class MethodRequestTest < Test::Unit::TestCase
end

end
end
end
6 changes: 3 additions & 3 deletions test/unit/fleakr/api/upload_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class UploadRequestTest < Test::Unit::TestCase

should "know the endpoint_uri" do
request = UploadRequest.new('filename')
request.__send__(:endpoint_uri).should == URI.parse('http://api.flickr.com/services/upload/')
request.__send__(:endpoint_uri).should == URI.parse('https://api.flickr.com/services/upload/')
end

should "know the endpoint_uri for an :update request" do
request = UploadRequest.new('filename', :update)
request.__send__(:endpoint_uri).should == URI.parse('http://api.flickr.com/services/replace/')
request.__send__(:endpoint_uri).should == URI.parse('https://api.flickr.com/services/replace/')
end

should "only parse the endpoint URI once" do
Expand Down Expand Up @@ -136,4 +136,4 @@ class UploadRequestTest < Test::Unit::TestCase
end

end
end
end