Class: Fleakr::Api::UploadRequest
- Inherits:
-
Object
- Object
- Fleakr::Api::UploadRequest
- Defined in:
- lib/fleakr/api/upload_request.rb
Overview
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
Constant Summary collapse
- ENDPOINT_URIS =
{ :create => 'http://api.flickr.com/services/upload/', :update => 'http://api.flickr.com/services/replace/' }
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.with_response!(filename, type = :create, options = {}) ⇒ Object
Send a request and return a Response object.
Instance Method Summary collapse
-
#headers ⇒ Object
:nodoc:.
-
#initialize(filename, type = :create, options = {}) ⇒ UploadRequest
constructor
Create a new UploadRequest with the specified filename, type, and options.
-
#send ⇒ Object
:nodoc:.
-
#upload_options ⇒ Object
A list of upload options for this upload request (see Fleakr::Api::Option).
Constructor Details
#initialize(filename, type = :create, options = {}) ⇒ UploadRequest
Create a new UploadRequest with the specified filename, type, and options. Type is one of :create
or :update
to specify whether we are saving a new image or replacing an existing one.
For a list of available options, see the documentation in Fleakr::Objects::Photo
37 38 39 40 41 42 43 |
# File 'lib/fleakr/api/upload_request.rb', line 37 def initialize(filename, type = :create, = {}) @type = type @options = @parameters = ParameterList.new() @parameters << FileParameter.new('photo', filename) end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
16 17 18 |
# File 'lib/fleakr/api/upload_request.rb', line 16 def parameters @parameters end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/fleakr/api/upload_request.rb', line 16 def type @type end |
Class Method Details
.with_response!(filename, type = :create, options = {}) ⇒ Object
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 details.
22 23 24 25 26 27 28 29 |
# File 'lib/fleakr/api/upload_request.rb', line 22 def self.with_response!(filename, type = :create, = {}) request = self.new(filename, type, ) response = request.send raise(Fleakr::ApiError, "Code: #{response.error.code} - #{response.error.}") if response.error? response end |
Instance Method Details
#headers ⇒ Object
:nodoc:
52 53 54 |
# File 'lib/fleakr/api/upload_request.rb', line 52 def headers # :nodoc: {'Content-Type' => "multipart/form-data; boundary=#{self.parameters.boundary}"} end |
#send ⇒ Object
:nodoc:
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fleakr/api/upload_request.rb', line 56 def send # :nodoc: 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 |
#upload_options ⇒ Object
A list of upload options for this upload request (see Fleakr::Api::Option)
47 48 49 50 |
# File 'lib/fleakr/api/upload_request.rb', line 47 def option_list = @options.map {|key, value| Option.for(key, value) } option_list.inject({}) {|hash, option| hash.merge(option.to_hash)} end |