Class: Aeolus::Image::Warehouse::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/aeolus_image/model/warehouse/warehouse_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Connection

Returns a new instance of Connection.



127
128
129
130
131
132
133
134
135
136
137
# 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

Instance Attribute Details

#uriObject

Returns the value of attribute uri.



125
126
127
# File 'lib/aeolus_image/model/warehouse/warehouse_client.rb', line 125

def uri
  @uri
end

Instance Method Details

#do_request(path = '/', opts = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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