Class: UcloudApi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ucloud_api/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey = nil, secretkey = nil) ⇒ Client

debug_output $stderr



16
17
18
19
# File 'lib/ucloud_api/client.rb', line 16

def initialize(apikey=nil, secretkey=nil)
  @apikey = apikey || ENV["UCLOUDAPIKEY"]
  @secretkey = secretkey || ENV["UCLOUDSECRET"]
end

Class Method Details

.list_styled(command, listname) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ucloud_api/client.rb', line 31

def self.list_styled command, listname
  class_eval <<"EOD"
  def #{command}(*args, &block)
h = Hash[*args]
h[:command] = "#{command}"
response = raw_cmd(h)
list = response.parsed_response["#{command.to_s.downcase}response"]["#{listname}"]
yield(list) if block_given?
list
  end
EOD
end

Instance Method Details

#raw_cmd(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/ucloud_api/client.rb', line 21

def raw_cmd options={}
  uri = Addressable::URI.new
  hashable_opt = options.merge( {:apiKey => @apikey, :response=>"json"} )
  uri.query_values = hashable_opt.sort
  hmac = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), uri.query.downcase, @secretkey)
  signature = Base64.encode64("#{OpenSSL::HMAC.digest('sha1', @secretkey, uri.query.downcase)}")[0..-2]
  query =  options.merge( {:response=>"json", :apiKey=>@apikey, :signature => signature } )
  self.class.get "/api" , :query => query
end