Class: Linode
- Inherits:
-
Object
show all
- Defined in:
- lib/linode.rb
Defined Under Namespace
Classes: Avail, Domain, Linode, 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.
32
33
34
35
36
|
# File 'lib/linode.rb', line 32
def initialize(args)
raise ArgumentError, ":api_key is required" unless args[:api_key]
@api_key = args[:api_key]
@api_url = args[:api_url] if args[:api_url]
end
|
Instance Attribute Details
#api_key ⇒ Object
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
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/linode.rb', line 18
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_url ⇒ Object
38
39
40
|
# File 'lib/linode.rb', line 38
def api_url
@api_url || 'https://api.linode.com/'
end
|
#send_request(action, data) ⇒ Object
42
43
44
45
46
47
|
# File 'lib/linode.rb', line 42
def send_request(action, data)
data.delete_if {|k,v| [:api_key, :api_action, :api_responseFormat].include?(k) }
result = Crack::JSON.parse(HTTParty.get(api_url, :query => { :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(result, action)}" if error?(result)
reformat_response(result)
end
|