Skip to content

Commit

Permalink
method caching and result parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
akerl committed Jan 9, 2014
1 parent 4417bbc commit f3f6dc2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/linodeapi/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(params = {})
self.class.base_uri params.fetch(:endpoint, DEFAULT_ENDPOINT)
@names = params.fetch(:names) { [] }
@spec = params.fetch(:spec) { SPEC }
@apikey = params.fetch(:apikey) { authenticate(params) }
@apikey = params.fetch(:apikey) { authenticate(params).first }
end

def respond_to?(method, include_private = false)
Expand Down Expand Up @@ -58,24 +58,28 @@ def make_group(method)
end

def make_call(method, *args)
call(method, *args)
instance_eval "def #{method}(*args) call(:#{method}, *args) end"
send(method, *args)
end

def call(method, params = {})
spec = @spec[:subs][method]
method = (@names + [method.to_s]).join '.'
options = self.class.validate method, spec[:params], params
options.merge! api_key: @apikey, api_action: method
p options
self.class.parse self.class.post('', body: options).parsed_response
end

def self.parse(resp)
unless resp['ERRORARRAY'].empty?
fail "API Error on #{resp['ACTION']}: #{resp['ERRORARRAY']}"
end
p resp['DATA']
Hash[resp['DATA'].map { |k, v| [k.downcase.to_sym, v] }]
data = resp['DATA']
data.is_a?(Hash) ? clean(data) : data.map { |x| clean x }
end

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

def self.validate(method, spec, given)
Expand Down

0 comments on commit f3f6dc2

Please sign in to comment.