Class: Fog::IBM::Connection

Inherits:
Connection show all
Defined in:
lib/fog/ibm.rb

Instance Method Summary collapse

Methods inherited from Connection

#reset

Constructor Details

#initialize(user, password) ⇒ Connection

Returns a new instance of Connection.



20
21
22
23
24
25
26
27
# File 'lib/fog/ibm.rb', line 20

def initialize(user, password)
  require 'multi_json'
  @user = user
  @password = password
  @endpoint = URI.parse('https://www-147.ibm.com/computecloud/enterprise/api/rest/20100331')
  @base_path = @endpoint.path
  super("#{@endpoint.scheme}://#{@endpoint.host}:#{@endpoint.port}")
end

Instance Method Details

#auth_headerObject



46
47
48
# File 'lib/fog/ibm.rb', line 46

def auth_header
  @auth_header ||= 'Basic ' + Base64.encode64("#{@user}:#{@password}").gsub("\n",'')
end

#form_encode(params) ⇒ Object



50
51
52
# File 'lib/fog/ibm.rb', line 50

def form_encode(params)
  params.reject {|k, v| v.nil? }.map {|pair| pair.map {|x| URI.escape(x.to_s) }.join('=') }.join('&')
end

#request(options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/ibm.rb', line 29

def request(options)
  options[:path] = @base_path + options[:path]
  options[:headers] ||= {}
  options[:headers]['Authorization'] = auth_header
  options[:headers]['Accept'] = 'application/json'
  options[:headers]['Accept-Encoding'] = 'gzip'
  unless options[:body].nil?
    options[:headers]['Content-Type'] = 'application/x-www-form-urlencoded'
    options[:body] = form_encode(options[:body])
  end
  response = super(options)
  unless response.body.empty?
    response.body = MultiJson.decode(response.body)
  end
  response
end