Class: EAAL::API

Inherits:
Object
  • Object
show all
Defined in:
lib/eaal/api.rb

Overview

EAAL::API class Usage Example:

api = EAAL::API.new("my keyID", "my API key")
result = api.Characters
result.characters.each{|character|
    puts character.name
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyid, vcode, scope = "account") ⇒ API

constructor Expects:

  • keyID (String | Integer) the keyID

  • vCode (String) the vCode

  • scope (String) defaults to account



16
17
18
19
20
# File 'lib/eaal/api.rb', line 16

def initialize(keyid, vcode, scope="account")
  self.keyid = keyid.to_s
  self.vcode = vcode.to_s
  self.scope = scope.to_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

create an xml request according to the method called this is used to dynamicaly create api calls and should usually not be called directly

  • method (const)

  • args



27
28
29
30
31
32
33
# File 'lib/eaal/api.rb', line 27

def method_missing(method, *args)
  scope = self.scope
  args_hash = args.first
  cache_only = (args_hash && args_hash.delete(:cache_only)) || false
  args_hash = {} unless args_hash
  self.request_xml(scope, method.id2name, args_hash, cache_only)
end

Instance Attribute Details

#keyidObject

Returns the value of attribute keyid.



9
10
11
# File 'lib/eaal/api.rb', line 9

def keyid
  @keyid
end

#scopeObject

Returns the value of attribute scope.



9
10
11
# File 'lib/eaal/api.rb', line 9

def scope
  @scope
end

#vcodeObject

Returns the value of attribute vcode.



9
10
11
# File 'lib/eaal/api.rb', line 9

def vcode
  @vcode
end

Instance Method Details

#request_path(name) ⇒ Object



78
79
80
# File 'lib/eaal/api.rb', line 78

def request_path(name)
  "/#{scope}/#{name}.xml.aspx"
end

#request_xml(scope, name, opts, cache_only = false) ⇒ Object

make a request to the api. will use cache if set. usually not called by the user directly

  • scope (String)

  • name (String)

  • opts (Hash)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/eaal/api.rb', line 40

def request_xml(scope, name, opts, cache_only = false)
  opts = EAAL.additional_request_parameters.merge(opts)
  xml = EAAL.cache.load(self.keyid, self.vcode, scope, name,opts)

  if (not xml) && (not cache_only)
    
    conn = Faraday.new(:url => "#{EAAL.api_base}") do |faraday|
      faraday.request :url_encoded
      faraday.adapter Faraday.default_adapter
    end

    response = conn.get(
      request_path(name), 
      opts.merge({
        :keyid => self.keyid,
        :vcode => self.vcode}))
    
    case response.status
    when 200
      # Nothing
    when 404
      raise EAAL::Exception::APINotFoundError.new("The requested API (#{scope} / #{name}) could not be found.")
    else
      raise EAAL::Exception::HTTPError.new(response.status), "An HTTP Error occurred, body: #{response.body}"
    end

    EAAL.cache.save(self.keyid, self.vcode, scope,name,opts, response.body)
    xml = response.body
  end

  if xml
    doc = Hpricot.XML(xml)
    result = EAAL::Result.new(scope.capitalize + name, doc)
  else 
    result = nil
  end
end