Class: Linode

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

Direct Known Subclasses

Avail, Domain, Image, Linode, Test, User

Defined Under Namespace

Classes: Account, Avail, Domain, Image, Linode, Nodebalancer, OpenStruct, Stackscript, Test, User

Constant Summary collapse

@@documentation_category =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Linode

Returns a new instance of Linode.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/linode.rb', line 57

def initialize(args)
  @api_url = args[:api_url] if args[:api_url]
  @logger = args[:logger]

  if args.include?(:api_key)
    @api_key = args[:api_key]
  elsif args.include?(:username) and args.include?(:password)
    @username = args[:username]
    @password = args[:password]
  else
    raise ArgumentError, "Either :api_key, or both :username and :password, are required."
  end
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/linode.rb', line 8

def logger
  @logger
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Class Method Details

.action_path(class_name, action) ⇒ Object



44
45
46
# File 'lib/linode.rb', line 44

def self.action_path(class_name, action)
  Linode.class_to_path(class_name) + ".#{action}"
end

.class_to_path(class_name) ⇒ Object



48
49
50
# File 'lib/linode.rb', line 48

def self.class_to_path(class_name)
  class_name.downcase.sub(/^linode::/, '').gsub(/::/, '.')
end

.documentation_category(category) ⇒ Object



36
37
38
# File 'lib/linode.rb', line 36

def self.documentation_category(category)
  @@documentation_category[class_to_path(name)] = category
end

.has_method(*actions) ⇒ Object



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

def self.has_method(*actions)
  actions.each do |action|
    define_method(action.to_sym) do |*data|
      data = data.shift if data
      data ||= {}
      send_request(Linode.action_path(self.class.name, action), data)
    end
  end
end

.has_namespace(*namespaces) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/linode.rb', line 20

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

Instance Method Details

#api_keyObject



75
76
77
# File 'lib/linode.rb', line 75

def api_key
  @api_key ||= fetch_api_key
end

#api_urlObject



71
72
73
# File 'lib/linode.rb', line 71

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

#documentation_categoriesObject



40
41
42
# File 'lib/linode.rb', line 40

def documentation_categories
  @@documentation_category
end

#documentation_path(action) ⇒ Object



52
53
54
55
# File 'lib/linode.rb', line 52

def documentation_path(action)
   hits = action.match(/^(.*)\.[^.]+$/)
  "https://www.linode.com/api/" + @@documentation_category[hits[1]] + '/' + action
end

#send_request(action, data) ⇒ Object



79
80
81
82
83
84
# File 'lib/linode.rb', line 79

def send_request(action, data)
  data.delete_if {|k,v| [:api_key, :api_action, :api_responseFormat].include?(k) }
  response = post({ :api_key => api_key, :api_action => action, :api_responseFormat => 'json' }.merge(data))
  raise "Errors completing request [#{action}] @ [#{api_url}] with data [#{data.inspect}]:\n#{error_message(response, action)}" if error?(response)
  reformat_response(response)
end