Sha256: 430d3f529e7d2e625b082a9ee5ccd13c01564607ba5c123888bc6a9510f96a67

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require "msgpack"

module CloudstackClient
  class Api

    DEFAULT_API_VERSION = "4.2"

    attr_reader :commands

    def initialize(options = {})
      if options[:api_file]
        @api_file = options[:api_file]
        @api_version = File.basename(@api_file, ".msgpack")
      else
        @api_version = options[:api_version] || DEFAULT_API_VERSION
        @api_file = File.expand_path("../../../config/#{@api_version}.msgpack", __FILE__)
      end
      @commands = load_commands
    end

    def command_supported?(command)
      @commands.has_key? command
    end

    def command_supports_param?(command, key)
      @commands[command]["params"].detect { |p| p["name"] == key } ? true : false
    end

    def required_params(command)
      @commands[command]["params"].map do |param|
        param["name"] if param["required"] == true
      end.compact
    end

    def all_required_params?(command, args)
      required_params(command).all? { |k| args.key? k }
    end

    def missing_params_msg(command)
      requ = required_params(command)
      "#{command} requires the following parameter#{ 's' if requ.size > 1}: #{requ.join(', ')}"
    end

    private

    def load_commands
      begin
        api = MessagePack.unpack(IO.read @api_file)
      rescue => e
        raise "Error: Unable to read file '#{@api_file}' : #{e.message}"
      end
      commands = Hash.new
      api.each { |command| commands[command["name"]] = command }
      commands
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloudstack_client-1.0.3 lib/cloudstack_client/api.rb