Class: AWS::EC2::Client

Inherits:
Object
  • Object
show all
Includes:
AWS::EC2, Parser, Request
Defined in:
lib/aws/ec2/client.rb

Constant Summary

Constants included from AWS::EC2

API

Instance Method Summary collapse

Methods included from Parser

#parse

Methods included from Request

#aws_request

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
# File 'lib/aws/ec2/client.rb', line 12

def initialize(options = {})
  @access_key_id = options[:access_key_id]
  @secret_access_key = options[:secret_access_key]
  @region = options[:region]
  @debug = options[:debug] || false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aws/ec2/client.rb', line 19

def method_missing(method_name, *args)
  action = camelize(method_name)

  config = EC2::API.find{|x, y| y[:methods].include?(method_name.to_sym)}

  raise ArgumentError, "Unknown Method: #{method_name}" unless config

  host = config.last[:host]
  regions = config.last[:regions] || []
  host = @region + '.' + host if @region and regions.include?(@region)
  version = config.last[:version]
  
  user_options = args[0] || {}
  user_options.merge!(:action => action, :version => version)
  camelized_options = {}
  user_options.each{|key, value| camelized_options[camelize(key).to_sym] = value}
  
  puts "calling host=#{host}, params=#{camelized_options.inspect}" if @debug
  
  xml_response = aws_request(host, camelized_options)
  parse(xml_response)
end

Instance Method Details

#camelize(key) ⇒ Object



42
43
44
# File 'lib/aws/ec2/client.rb', line 42

def camelize(key)
  key.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
end