class UcloudApi::Client

Public Class Methods

list_styled(command, listname) click to toggle source
# 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
new(apikey=nil, secretkey=nil) click to toggle source

debug_output $stderr

# File lib/ucloud_api/client.rb, line 16
def initialize(apikey=nil, secretkey=nil)
  @apikey = apikey || ENV["UCLOUDAPIKEY"]
  @secretkey = secretkey || ENV["UCLOUDSECRET"]
end

Public Instance Methods

raw_cmd(options={}) click to toggle source
# 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