Class: UcloudApi::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

debug_output $stderr



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

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

Class Method Details

.list_styled(command, listname) ⇒ Object



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

def self.list_styled command, listname
  class_eval "  def \#{command}(*args, &block)\nh = Hash[*args]\nh[:command] = \"\#{command}\"\nresponse = raw_cmd(h)\nlist = response.parsed_response[\"\#{command.to_s.downcase}response\"][\"\#{listname}\"]\nyield(list) if block_given?\nlist\n  end\n"
end

Instance Method Details

#raw_cmd(options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/ucloud_api.rb', line 23

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::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