module ConohaApi::Connection

Attributes

current_connection[R]

Public Instance Methods

delete(path, options = {}) click to toggle source
# File lib/conoha_api/connection.rb, line 19
def delete(path, options = {})
  request(:delete, path, nil, options)
end
get(path, options = {}) click to toggle source
# File lib/conoha_api/connection.rb, line 11
def get(path, options = {})
  request(:get, path, nil, options)
end
post(path, data, options = {}) click to toggle source
# File lib/conoha_api/connection.rb, line 23
def post(path, data, options = {})
  request(:post, path, data, options)
end
put(path, data, options = {}) click to toggle source
# File lib/conoha_api/connection.rb, line 15
def put(path, data, options = {})
  request(:put, path, data, options)
end

Private Instance Methods

agent() click to toggle source
# File lib/conoha_api/connection.rb, line 29
def agent
  endpoint = @connection_stack.last
  (@connections ||= {})[endpoint] ||= Sawyer::Agent.new(endpoint, sawyer_options) do |http|
    http.headers['content-type'] = 'application/json'
  end

end
request(method, path, data, options = {}) click to toggle source
# File lib/conoha_api/connection.rb, line 37
def request(method, path, data, options = {})
  # find module original method defined and add endpoint to connection stack
  origin = search_caller(caller(0..2))
  endpoint = URI.parse(endpoints[origin::SERVICE] || @api_endpoint)
  (@connection_stack ||= []).push("#{endpoint.scheme}://#{endpoint.host}")

  unless options[:no_auth]
    options = options.dup
    (options[:headers] ||= {})["X-Auth-Token"] = credential.access.token.id
  end

  path = File.join(endpoint.path, path)
  res = agent.call(method, URI::Parser.new.escape(path), data, options)

  @connection_stack.pop
  res.data
end
sawyer_options() click to toggle source
# File lib/conoha_api/connection.rb, line 60
def sawyer_options
  opts = {
    :links_parser => Sawyer::LinkParsers::Simple.new
  }
  conn_ops = @connection_options
  conn_ops[:builder] = @middleware if @middleware
  conn_ops[:proxy] = @proxy if @proxy
  opts[:faraday] = Faraday.new(conn_ops)

  opts
end
search_caller(call_stack) click to toggle source
# File lib/conoha_api/connection.rb, line 55
def search_caller(call_stack)
  res = call_stack[2].match(/`(?<method>.*)'/)
  self.method(res[:method]).owner
end