Class: EAAL::API
- Inherits:
-
Object
- Object
- EAAL::API
- Defined in:
- lib/eaal/eaal.rb
Overview
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#userid ⇒ Object
Returns the value of attribute userid.
Instance Method Summary collapse
-
#format_url_request(opts) ⇒ Object
Turns a hash into ?var=baz&bam=boo stolen from Reve (thx lisa) * opts (Hash).
-
#initialize(userid, key, scope = "account") ⇒ API
constructor
constructor Expects: * userid (String | Integer) the users id * key (String) the apikey Full or Restricted * scope (String) defaults to account.
-
#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.
-
#request_xml(scope, name, opts) ⇒ Object
make a request to the api.
Constructor Details
#initialize(userid, key, scope = "account") ⇒ API
constructor Expects:
-
userid (String | Integer) the users id
-
key (String) the apikey Full or Restricted
-
scope (String) defaults to account
54 55 56 57 58 |
# File 'lib/eaal/eaal.rb', line 54 def initialize(userid, key, scope="account") self.userid = userid.to_s self.key = key.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
65 66 67 68 69 70 |
# File 'lib/eaal/eaal.rb', line 65 def method_missing(method, *args) scope = self.scope args_hash = args.first args_hash = {} unless args_hash self.request_xml(scope, method.id2name, args_hash) end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
47 48 49 |
# File 'lib/eaal/eaal.rb', line 47 def key @key end |
#scope ⇒ Object
Returns the value of attribute scope.
47 48 49 |
# File 'lib/eaal/eaal.rb', line 47 def scope @scope end |
#userid ⇒ Object
Returns the value of attribute userid.
47 48 49 |
# File 'lib/eaal/eaal.rb', line 47 def userid @userid end |
Instance Method Details
#format_url_request(opts) ⇒ Object
Turns a hash into ?var=baz&bam=boo stolen from Reve (thx lisa)
-
opts (Hash)
105 106 107 108 109 110 111 112 |
# File 'lib/eaal/eaal.rb', line 105 def format_url_request(opts) req = "?" opts.stringify_keys! opts.keys.sort.each do |key| req += "#{CGI.escape(key.to_s)}=#{CGI.escape(opts[key].to_s)}&" if opts[key] end req.chop # We are lazy and append a & to each pair even if it's the last one. FIXME: Don't do this. end |
#request_xml(scope, name, opts) ⇒ 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)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/eaal/eaal.rb', line 77 def request_xml(scope, name, opts) opts = EAAL.additional_request_parameters.merge(opts) xml = EAAL.cache.load(self.userid, self.key, scope, name,opts) if not xml source = URI.parse(EAAL.api_base + scope + '/' + name +'.xml.aspx') req_path = source.path + format_url_request(opts.merge({ :userid => self.userid, :apikey => self.key})) req = Net::HTTP::Get.new(req_path) req[EAAL.version_string] res = Net::HTTP.new(source.host, source.port).start {|http| http.request(req) } #one request for now case res when Net::HTTPOK when Net::HTTPNotFound raise EAAL::Exception::APINotFoundError.new("The requested API (#{scope} / #{name}) could not be found.") else raise EAAL::Exception::HTTPError.new("An HTTP Error occured, body: " + res.body) end EAAL.cache.save(self.userid, self.key, scope,name,opts, res.body) xml = res.body end doc = Hpricot.XML(xml) result = EAAL::Result.new(scope.capitalize + name, doc) end |