class Aeolus::Image::Warehouse::Connection

Attributes

uri[RW]

Public Class Methods

new(uri) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_client.rb, line 127
def initialize(uri)
  @uri = uri
  if WarehouseModel.use_oauth? && WarehouseModel.iwhd_url && uri.match(WarehouseModel.iwhd_url)
    @consumer = OAuth::Consumer.new(
      WarehouseModel.oauth_consumer_key,
      WarehouseModel.oauth_consumer_secret,
      :site => WarehouseModel.iwhd_url
    )
    @token = OAuth::AccessToken.new(@consumer)
  end
end

Public Instance Methods

do_request(path = '/', opts={}) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_client.rb, line 139
def do_request(path = '/', opts={})
  opts[:method]  ||= :get
  opts[:content] ||= ''
  opts[:plain]   ||= false
  opts[:headers] ||= {}

  # Using restclient seems to break OAuth POSTs, so use the oauth gem directly if we're using OAuth:
  if @token
    # TODO - I'm not sure how to pass :headers through here, but we don't actually use them anywhere
    response = @token.request(opts[:method], (@uri + path).to_s, opts[:content].to_s)
    # Errors don't cause exceptions like they do with RestClient, so detect and raise them:
    if response.is_a?(Net::HTTPSuccess)
      result = response.body
    # Translate a few common errors to their RestClient counterparts that we're used to checking for:
    elsif response.is_a?(Net::HTTPNotFound)
      raise RestClient::ResourceNotFound
    elsif response.is_a?(Net::HTTPInternalServerError)
      raise RestClient::InternalServerError
    # Otherwise, raise the error:
    else
      response.error!
    end
  else # no @token -- use RestClient
    result = RestClient::Request.execute(:method => opts[:method], :url => @uri + path, :payload => opts[:content], :headers => opts[:headers])
  end
  return opts[:plain] ? result : Nokogiri::XML(result)
end