Class: Fleakr::Api::UploadRequest

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @type    = type
  @options = options
  
  @parameters = ParameterList.new(upload_options)
  parameters.add_upload_option(:photo, filename)
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters


16
17
18
# File 'lib/fleakr/api/upload_request.rb', line 16

def parameters
  @parameters
end

#typeObject (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.

Raises:


22
23
24
25
26
27
28
29
# File 'lib/fleakr/api/upload_request.rb', line 22

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

Instance Method Details

#headersObject

: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

#sendObject

: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_optionsObject

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 upload_options
  option_list = @options.map {|key, value| Option.for(key, value) }
  option_list.inject({}) {|hash, option| hash.merge(option.to_hash)}
end