class Linode

Attributes

logger[RW]
password[R]
username[R]

Public Class Methods

action_path(class_name, action) click to toggle source
# 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) click to toggle source
# File lib/linode.rb, line 48
def self.class_to_path(class_name)
  class_name.downcase.sub(/^linode::/, '').gsub(/::/, '.')
end
documentation_category(category) click to toggle source
# File lib/linode.rb, line 36
def self.documentation_category(category)
  @@documentation_category[class_to_path(name)] = category
end
has_method(*actions) click to toggle source
# 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) click to toggle source
# 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
new(args) click to toggle source
# 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

Public Instance Methods

api_key() click to toggle source
# File lib/linode.rb, line 75
def api_key
  @api_key ||= fetch_api_key
end
api_url() click to toggle source
# File lib/linode.rb, line 71
def api_url
  @api_url || 'https://api.linode.com/'
end
documentation_categories() click to toggle source
# File lib/linode.rb, line 40
def documentation_categories
  @@documentation_category
end
documentation_path(action) click to toggle source
# 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) click to toggle source
# 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

Protected Instance Methods

convert_item(item) click to toggle source
# File lib/linode.rb, line 117
def convert_item(item)
  item.keys.each do |k|
    item[k.downcase] = item[k]
    item.delete(k) if k != k.downcase
  end
  Linode::OpenStruct.new(item)
end
error?(response) click to toggle source
# File lib/linode.rb, line 99
def error?(response)
  response and response["ERRORARRAY"] and ! response["ERRORARRAY"].empty?
end
error_message(response, action) click to toggle source
# File lib/linode.rb, line 103
def error_message(response, action)
  response["ERRORARRAY"].collect { |err|
    "  - Error \##{err["ERRORCODE"]} - #{err["ERRORMESSAGE"]}.  "+
    "(Please consult #{documentation_path(action)})"
  }.join("\n")
end
fetch_api_key() click to toggle source
# File lib/linode.rb, line 88
def fetch_api_key
  response = post(:api_action => 'user.getapikey', :api_responseFormat => 'json', :username => username, :password => password)
  raise "Errors completing request [user.getapikey] @ [#{api_url}] for username [#{username}]:\n#{error_message(response, 'user.getapikey')}" if error?(response)
  reformat_response(response).api_key
end
post(data) click to toggle source
# File lib/linode.rb, line 94
def post(data)
  logger.info "POST #{api_url.to_s} body:#{data.inspect}" if logger
  HTTParty.post(api_url, :body => data).parsed_response
end
reformat_response(response) click to toggle source
# File lib/linode.rb, line 110
def reformat_response(response)
  result = response['DATA']
  return result.collect {|item| convert_item(item) } if result.class == Array
  return result unless result.respond_to?(:keys)
  convert_item(result)
end