Class: LinodeAPI::Raw

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/linodeapi/raw.rb

Overview

Raw API object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Raw

Returns a new instance of Raw.



14
15
16
17
18
19
# File 'lib/linodeapi/raw.rb', line 14

def initialize(params = {})
  self.class.base_uri params.fetch(:endpoint, DEFAULT_ENDPOINT)
  @names = params.fetch(:names) { [] }
  @spec = params.fetch(:spec) { LinodeAPI.spec }
  @apikey = params.fetch(:apikey) { authenticate(params) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



40
41
42
43
44
45
46
# File 'lib/linodeapi/raw.rb', line 40

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

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



12
13
14
# File 'lib/linodeapi/raw.rb', line 12

def apikey
  @apikey
end

#namesObject (readonly)

Returns the value of attribute names.



12
13
14
# File 'lib/linodeapi/raw.rb', line 12

def names
  @names
end

#specObject (readonly)

Returns the value of attribute spec.



12
13
14
# File 'lib/linodeapi/raw.rb', line 12

def spec
  @spec
end

Class Method Details

.clean(object) ⇒ Object



93
94
95
# File 'lib/linodeapi/raw.rb', line 93

def clean(object)
  OpenStruct.new(Hash[object.map { |k, v| [k.downcase.to_sym, v] }])
end

.parse(resp) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/linodeapi/raw.rb', line 84

def parse(resp)
  resp['ERRORARRAY'].reject! { |x| x['ERRORCODE'].zero? }
  unless resp['ERRORARRAY'].empty?
    raise "API Error on #{resp['ACTION']}: #{resp['ERRORARRAY']}"
  end
  data = resp['DATA']
  data.is_a?(Hash) ? clean(data) : data.map { |x| clean x }
end

.validate(method, spec, given) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/linodeapi/raw.rb', line 97

def validate(method, spec, given)
  spec.each_with_object({}) do |(param, info), options|
    if given.include? param
      options[param] = VALIDATION_METHODS[info[:type]].call given[param]
    elsif info[:required]
      raise ArgumentError, "#{method} requires #{param}"
    end
  end
end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/linodeapi/raw.rb', line 21

def respond_to?(method, include_private = false)
  super || @spec[:subs].include?(method)
end

#to_sObject Also known as: inspect



25
26
27
# File 'lib/linodeapi/raw.rb', line 25

def to_s
  'LinodeAPI::Raw object'
end