class LinodeAPI::Raw

Raw API object

Public Class Methods

new(params = {}) click to toggle source
# File lib/linodeapi/raw.rb, line 13
def initialize(params = {})
  @options = params
  @options[:endpoint] ||= DEFAULT_ENDPOINT
  @options[:names] ||= []
  @options[:spec] ||= LinodeAPI.spec
  @options[:apikey] ||= authenticate(params)
  @options[:user_agent_prefix] ||= ''
end

Public Instance Methods

apikey() click to toggle source
# File lib/linodeapi/raw.rb, line 39
def apikey
  @apikey ||= @options[:apikey]
end
endpoint() click to toggle source
# File lib/linodeapi/raw.rb, line 43
def endpoint
  @endpoint ||= @options[:endpoint]
end
inspect()
Alias for: to_s
names() click to toggle source
# File lib/linodeapi/raw.rb, line 31
def names
  @names ||= @options[:names]
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/linodeapi/raw.rb, line 22
def respond_to_missing?(method, include_private = false)
  spec[:subs].include?(method) || super
end
spec() click to toggle source
# File lib/linodeapi/raw.rb, line 35
def spec
  @spec ||= @options[:spec]
end
to_s() click to toggle source
# File lib/linodeapi/raw.rb, line 26
def to_s
  'LinodeAPI::Raw object'
end
Also aliased as: inspect

Private Instance Methods

authenticate(params = {}) click to toggle source
# File lib/linodeapi/raw.rb, line 49
def authenticate(params = {})
  return [] unless names.empty?
  unless (params.values_at :username, :password).all?
    raise ArgumentError, 'You must provide either an API key or user/pass'
  end
  user.getapikey(params).api_key
end
build_call_body(method, params) click to toggle source
# File lib/linodeapi/raw.rb, line 101
def build_call_body(method, params)
  mspec = spec[:subs][method]
  mname = (names + [method.to_s]).join '.'
  options = self.class.validate method, mspec[:params], params
  options[:api_key] = apikey
  options[:api_action] = mname
  options
end
call(method, params = {}) click to toggle source
# File lib/linodeapi/raw.rb, line 83
def call(method, params = {})
  options = build_call_body(method, params)
  self.class.error_check self.class.post(
    '', body: options,
        base_uri: endpoint,
        headers: { 'User-Agent' => user_agent }
  )
end
make_call(method, *args) click to toggle source
# File lib/linodeapi/raw.rb, line 78
def make_call(method, *args)
  define_singleton_method(method) { |*a| call(method, *a) }
  send(method, *args)
end
make_group(method) click to toggle source
# File lib/linodeapi/raw.rb, line 65
def make_group(method)
  group = self.class.new(
    @options.dup.merge(
      spec: spec[:subs][method],
      names: names + [method]
    )
  )
  name = "@#{method}".to_sym
  instance_variable_set name, group
  define_singleton_method(method) { instance_variable_get(name) }
  group
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/linodeapi/raw.rb, line 57
def method_missing(method, *args, &block)
  return super unless respond_to? method
  case spec[:subs][method][:type]
  when :group then make_group method
  when :call then make_call method, *args
  end
end
user_agent() click to toggle source
# File lib/linodeapi/raw.rb, line 92
def user_agent
  user_agent_fragments = [
    @options[:user_agent_prefix],
    "linodeapi/#{LinodeAPI::VERSION}",
    "ruby/#{RUBY_VERSION}"
  ]
  user_agent_fragments.reject(&:empty?).join(' ')
end