class S3restful::S3::Request

Constants

VALID_HTTP_METHODS

Attributes

http_method[RW]
options[RW]
response[RW]
url[RW]

Public Class Methods

new(http_method, url, options = {}) click to toggle source
# File lib/s3restful/s3/request.rb, line 10
def initialize(http_method, url, options = {})
  @options = {
    :timeout => 10,
    :retry_count => 4,
    :headers => {},
    :on_error => nil,
    :on_success => nil,
    :data => nil,
    :ssl => {
      :cert_chain_file => nil,
      :verify_peer => false
    }
  }.update(options)
  assert_valid_keys(options, :timeout, :on_success, :on_error, :retry_count, :headers, :data, :ssl, :file)
  @http_method = http_method
  @url = url
  
  validate
end

Public Instance Methods

execute() click to toggle source
# File lib/s3restful/s3/request.rb, line 30
def execute
  S3restful::Log.debug "Request: #{http_method.to_s.upcase} #{url}"
  @response = http_class.new(url).send(http_method, :timeout => options[:timeout], :head => options[:headers], :body => options[:data], :ssl => options[:ssl], :file => options[:file])

  @response.errback { error_callback }
  @response.callback { success_callback }
  @response
end
http_class() click to toggle source
# File lib/s3restful/s3/request.rb, line 39
def http_class
  EventMachine::HttpRequest
end

Protected Instance Methods

call_user_error_handler() click to toggle source
# File lib/s3restful/s3/request.rb, line 84
def call_user_error_handler
  options[:on_error].call(response) if options[:on_error].respond_to?(:call)
end
call_user_success_handler() click to toggle source
# File lib/s3restful/s3/request.rb, line 80
def call_user_success_handler
  options[:on_success].call(response) if options[:on_success].respond_to?(:call)
end
error_callback() click to toggle source
# File lib/s3restful/s3/request.rb, line 49
def error_callback
  S3restful::Log.error "Response error: #{http_method.to_s.upcase} #{url}: #{response.response_header.status rescue ''}"
  if should_retry?
    S3restful::Log.info "#{http_method.to_s.upcase} #{url}: retrying after error: status #{response.response_header.status rescue ''}"
    handle_retry
  elsif options[:on_error].respond_to?(:call)
    call_user_error_handler
  else
    raise S3restful::Error.new("#{http_method.to_s.upcase} #{url}: Failed reponse! Status code was #{response.response_header.status rescue ''}")
  end
end
handle_redirect() click to toggle source
# File lib/s3restful/s3/request.rb, line 101
def handle_redirect
  new_location = response.response_header['LOCATION'] rescue ''
  raise "Could not find the location to redirect to, empty location header?" if blank?(new_location)

  new_request = self.class.new(http_method, new_location, options)
  new_request.execute
end
handle_retry() click to toggle source
# File lib/s3restful/s3/request.rb, line 92
def handle_retry
  if should_retry?
    new_request = self.class.new(http_method, url, options.update(:retry_count => options[:retry_count] - 1 ))
    new_request.execute
  else
    S3restful::Log.error "#{http_method.to_s.upcase} #{url}: Re-tried too often - giving up"
  end
end
should_retry?() click to toggle source
# File lib/s3restful/s3/request.rb, line 88
def should_retry?
  options[:retry_count] > 0
end
success_callback() click to toggle source
# File lib/s3restful/s3/request.rb, line 61
def success_callback
  S3restful::Log.debug "Response success: #{http_method.to_s.upcase} #{url}: #{response.response_header.status rescue ''}"
  case response.response_header.status
  when 0, 400, 401, 404, 403, 409, 411, 412, 416, 500, 503
    if should_retry?
      S3restful::Log.info "#{http_method.to_s.upcase} #{url}: retrying after: status #{response.response_header.status rescue ''}"
      handle_retry
    else
      S3restful::Log.error "#{http_method.to_s.upcase} #{url}: Re-tried too often - giving up"
      error_callback
    end
  when 300, 301, 303, 304, 307
    S3restful::Log.info "#{http_method.to_s.upcase} #{url}: being redirected_to: #{response.response_header['LOCATION'] rescue ''}"
    handle_redirect
  else
    call_user_success_handler
  end
end
validate() click to toggle source
# File lib/s3restful/s3/request.rb, line 45
def validate
  raise ArgumentError, "#{http_method} is not a valid HTTP method that #{self.class.name} understands." unless VALID_HTTP_METHODS.include?(http_method)
end