Class: Cloudn::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudn_cli/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
# File 'lib/cloudn_cli/client.rb', line 9

def initialize(opt)
  @api_key    = opt[:api_key].freeze
  @secret_key = opt[:secret_key].freeze
  url = opt[:url]
  @url = url.is_a?(URI::HTTP) ? url : URI.parse(url)
  @url.freeze      
  @api_location = opt[:api_location].freeze
  @json       = opt[:json].freeze
end

Instance Attribute Details

#api_locationObject (readonly)

Returns the value of attribute api_location.



19
20
21
# File 'lib/cloudn_cli/client.rb', line 19

def api_location
  @api_location
end

#jsonObject (readonly)

Returns the value of attribute json.



19
20
21
# File 'lib/cloudn_cli/client.rb', line 19

def json
  @json
end

Instance Method Details

#get(param_hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloudn_cli/client.rb', line 37

def get(param_hash)
  response_text = get_raw(param_hash)
  response = @json ? JSON.parse(response_text) : Crack::XML.parse(response_text)
  command = param_hash.fetch(:command).to_s
  command.downcase!
  key = command + "response"
  begin
    result = response.fetch(key)
  rescue KeyError => ex
    case key
    when "restorevirtualmachineresponse"
      key = "restorevmresponse"
    else
      raise ex
    end
    retry
  end
  return result
end

#get_raw(param_hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cloudn_cli/client.rb', line 21

def get_raw(param_hash)
  param_str = make_param_str(param_hash)
  url = @url.dup
  url.query = param_str
  begin
    response_text = OpenURI.open_uri(url,
  :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,
    "User-Agent" => "Cloudn_cli/#{RUBY_VERSION}",
    "X-api-location" => @api_location
    ).read
  rescue OpenURI::HTTPError => ex
    raise ex.exception("status: #{ex.message} desc: #{ex.io.meta["x-description"]}") # update error message in detail
  end
  return response_text
end