class CloudstackClient::Api

Constants

API_PATH
DEFAULT_API_VERSION

Attributes

api_file[R]
api_path[R]
api_version[R]
commands[R]

Public Class Methods

new(options = {}) click to toggle source
   # File lib/cloudstack_client/api.rb
21 def initialize(options = {})
22   set_api_path(options)
23   set_api_version_and_file(options)
24   load_commands
25 end
versions(api_path = API_PATH) click to toggle source
   # File lib/cloudstack_client/api.rb
15 def self.versions(api_path = API_PATH)
16   Dir[api_path + "/*.json.gz"].map do |path|
17     File.basename(path, ".json.gz")
18   end
19 end

Public Instance Methods

all_required_params?(command, args) click to toggle source
   # File lib/cloudstack_client/api.rb
48 def all_required_params?(command, args)
49   required_params(command).all? { |k| args.key? k }
50 end
command_supported?(command) click to toggle source
   # File lib/cloudstack_client/api.rb
27 def command_supported?(command)
28   @commands.has_key? underscore_to_camel_case(command)
29 end
command_supports_param?(command, key) click to toggle source
   # File lib/cloudstack_client/api.rb
31 def command_supports_param?(command, key)
32   command = underscore_to_camel_case(command)
33   @commands[command]["params"].detect do |params|
34     params["name"] == key.to_s
35   end ? true : false
36 end
missing_params_msg(command) click to toggle source
   # File lib/cloudstack_client/api.rb
52 def missing_params_msg(command)
53   "#{command} requires the following parameter" +
54   "#{ 's' if required_params(command).size > 1 }: " +
55   required_params(command).join(", ")
56 end
params(command) click to toggle source
   # File lib/cloudstack_client/api.rb
44 def params(command)
45   @commands[command]["params"]
46 end
required_params(command) click to toggle source
   # File lib/cloudstack_client/api.rb
38 def required_params(command)
39   self.params(command).map do |param|
40     param["name"] if param["required"] == true
41   end.compact
42 end

Private Instance Methods

load_commands() click to toggle source
   # File lib/cloudstack_client/api.rb
92 def load_commands
93   @commands = {}
94   Zlib::GzipReader.open(@api_file) do |gz|
95     JSON.parse(gz.read)
96   end.each {|cmd| @commands[cmd["name"]] = cmd }
97 rescue => e
98   raise "Error: Unable to read file '#{@api_file}': #{e.message}"
99 end
set_api_path(options) click to toggle source
   # File lib/cloudstack_client/api.rb
70 def set_api_path(options)
71   @api_path = if options[:api_path]
72     File.expand_path(options[:api_path])
73   else
74     API_PATH
75   end
76 end
set_api_version(options) click to toggle source
   # File lib/cloudstack_client/api.rb
78 def set_api_version(options)
79   @api_version = options[:api_version] || DEFAULT_API_VERSION
80   unless Api.versions(@api_path).include? @api_version
81     if options[:api_version]
82       raise "API definition not found for version '#{@api_version}' in api_path '#{@api_path}'"
83     elsif Api.versions(@api_path).size < 1
84       raise "no API file available in api_path '#{@api_path}'"
85     else
86       @api_version = Api.versions(@api_path).last
87     end
88   end
89   @api_version
90 end
set_api_version_and_file(options) click to toggle source
   # File lib/cloudstack_client/api.rb
60 def set_api_version_and_file(options)
61   if options[:api_file]
62     @api_file = options[:api_file]
63     @api_version = File.basename(@api_file, ".json.gz")
64   else
65     set_api_version(options)
66     @api_file = File.join(@api_path, "#{@api_version}.json.gz")
67   end
68 end