Class: Linode

Inherits:
Object
  • Object
show all
Defined in:
lib/linode.rb

Direct Known Subclasses

Avail, Domain, Linode, Test, User

Defined Under Namespace

Classes: Avail, Domain, Linode, Stackscript, Test, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Linode

Returns a new instance of Linode.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/linode.rb', line 42

def initialize(args)
  if args.include?(:api_key)
    @api_key = args[:api_key]
    @api_url = args[:api_url] if args[:api_url]
  elsif args.include?(:username) and args.include?(:password)
    @api_url = args[:api_url] if args[:api_url]
    result = send_unauthenticated_request("user.getapikey", { :username => args[:username], :password => args[:password] })
    @api_key = result.api_key
  else
    raise ArgumentError, "Either :api_key, or both :username and :password, are required"
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/linode.rb', line 6

def api_key
  @api_key
end

Class Method Details

.has_method(*actions) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/linode.rb', line 8

def self.has_method(*actions)
  actions.each do |action|
    define_method(action.to_sym) do |*data|
      data = data.shift if data
      data ||= {}
      send_request(self.class.name.downcase.sub(/^linode::/, '').gsub(/::/, '.') + ".#{action}", data)
    end
  end
end

.has_namespace(*namespaces) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/linode.rb', line 28

def self.has_namespace(*namespaces)
  namespaces.each do |namespace|
    define_method(namespace.to_sym) do ||
      lookup = instance_variable_get("@#{namespace}")
      return lookup if lookup
      subclass = self.class.const_get(namespace.to_s.capitalize).new(:api_key => api_key, :api_url => api_url)
      instance_variable_set("@#{namespace}", subclass)
      subclass
    end
  end
end

.has_unauthenticated_method(*actions) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/linode.rb', line 18

def self.has_unauthenticated_method(*actions)
  actions.each do |action|
    define_method(action.to_sym) do |*data|
      data = data.shift if data
      data ||= {}
      send_unauthenticated_request(self.class.name.downcase.sub(/^linode::/, '').gsub(/::/, '.') + ".#{action}", data)
    end
  end
end

Instance Method Details

#api_urlObject



55
56
57
# File 'lib/linode.rb', line 55

def api_url
  @api_url || 'https://api.linode.com/'
end

#send_request(action, data) ⇒ Object



59
60
61
62
# File 'lib/linode.rb', line 59

def send_request(action, data)
  data.delete_if {|k,v| [:api_key ].include?(k) }
  send_unauthenticated_request(action, { :api_key => api_key }.merge(data))
end

#send_unauthenticated_request(action, data) ⇒ Object



64
65
66
67
68
69
# File 'lib/linode.rb', line 64

def send_unauthenticated_request(action, data)
  data.delete_if {|k,v| [ :api_action, :api_responseFormat].include?(k) }
  result = Crack::JSON.parse(HTTParty.get(api_url, :query => { :api_action => action, :api_responseFormat => 'json' }.merge(data)))
  raise "Errors completing request [#{action}] @ [#{api_url}] with data [#{data.inspect}]:\n#{error_message(result, action)}" if error?(result)
  reformat_response(result)
end