class Cloudhdr::HTTP

Attributes

body[RW]
format[RW]
method[RW]
options[RW]
url[RW]

Public Class Methods

delete(url, options={}) click to toggle source
# File lib/cloudhdr/http.rb, line 35
def self.delete(url, options={})
  new(:delete, url, options).perform_method
end
get(url, options={}) click to toggle source
# File lib/cloudhdr/http.rb, line 31
def self.get(url, options={})
  new(:get, url, options).perform_method
end
new(method, url, options={}) click to toggle source
# File lib/cloudhdr/http.rb, line 15
def initialize(method, url, options={})
  self.method  = method
  self.url     = url
  self.format  = options.delete(:format)
  self.options = options
  self.body    = options.delete(:body)
end
post(url, body, options={}) click to toggle source
# File lib/cloudhdr/http.rb, line 23
def self.post(url, body, options={})
  new(:post, url, options.merge(:body => body)).perform_method
end
put(url, body, options={}) click to toggle source
# File lib/cloudhdr/http.rb, line 27
def self.put(url, body, options={})
  new(:put, url, options.merge(:body => body)).perform_method
end

Public Instance Methods

default_options() click to toggle source
# File lib/cloudhdr/http.rb, line 67
def default_options
  self.class.default_options.recursive_with_indifferent_access
end
http_backend() click to toggle source
# File lib/cloudhdr/http.rb, line 63
def http_backend
  self.class.http_backend
end
options=(value) click to toggle source
# File lib/cloudhdr/http.rb, line 45
def options=(value)
  @options = default_options.recursive_with_indifferent_access.merge(value || {})

  options[:headers] ||= {}
  options[:headers]['Accept'] = "application/#{format}"
  options[:headers]['Content-Type'] = "application/#{format}"

  options
end
perform_method() click to toggle source
# File lib/cloudhdr/http.rb, line 39
def perform_method
  process(http_backend.send(method, url, options))
rescue StandardError => e
  raise HTTPError.new(e)
end

Protected Instance Methods

process(http_response) click to toggle source
# File lib/cloudhdr/http.rb, line 74
def process(http_response)
  response = Response.new
  response.code = http_response.code

  begin
    response.body = decode(http_response.body.to_s, format)
  rescue StandardError # Hack! Returns different exceptions depending on the decoding engine
    response.body = http_response.body
  end

  response.raw_body = http_response.body
  response.raw_response = http_response
  response
end