Class: OutsideIn::Resource::Base Abstract
- Inherits:
-
Object
- Object
- OutsideIn::Resource::Base
- Defined in:
- lib/outside_in/resource/base.rb
Overview
Subclass and override #scope and #params to implement a custom resource class.
The base class for API resources.
Resources are exposed by the API service at particular endpoints identified by URLs. Consumers can interact with resources via the HTTP uniform interface.
Direct Known Subclasses
Class Method Summary collapse
-
.parameterize(url, inputs) ⇒ String
Returns a version of
url
with parameters in the query string corresponding toinputs
. -
.scope(url, inputs) ⇒ String
Returns a version of
url
that includes publication scoping wheninputs
contains a non-nilpublication-id
entry. -
.sign(url) ⇒ String
Returns the signed form of
url
.
Instance Method Summary collapse
-
#GET(inputs) ⇒ Object
Calls
GET
on the remote API service and returns the data encapsulated in the response. -
#initialize(relative_url) ⇒ OutsideIn::Resource::Base
constructor
Returns a new instance.
Constructor Details
#initialize(relative_url) ⇒ OutsideIn::Resource::Base
Returns a new instance. Stores the absolutized, signed URL.
58 59 60 |
# File 'lib/outside_in/resource/base.rb', line 58 def initialize(relative_url) @url = "http://#{HOST}/v#{VERSION}#{relative_url}" end |
Class Method Details
.parameterize(url, inputs) ⇒ String
Returns a version of url
with parameters in the query string corresponding to inputs
.
36 37 38 |
# File 'lib/outside_in/resource/base.rb', line 36 def self.parameterize(url, inputs) raise NotImplementedError end |
.scope(url, inputs) ⇒ String
Returns a version of url
that includes publication scoping when inputs
contains a non-nil publication-id
entry.
26 27 28 |
# File 'lib/outside_in/resource/base.rb', line 26 def self.scope(url, inputs) raise NotImplementedError end |
.sign(url) ⇒ String
Returns the signed form of url
. Signing adds the dev_key
and sig
query parameters to the query string.
46 47 48 49 50 51 52 |
# File 'lib/outside_in/resource/base.rb', line 46 def self.sign(url) raise SignatureException, "Key not set" unless OutsideIn.key raise SignatureException, "Secret not set" unless OutsideIn.secret sig_params = "dev_key=#{OutsideIn.key}&sig=#{MD5.new(OutsideIn.key + OutsideIn.secret + Time.now.to_i.to_s).hexdigest}" url =~ /\?/ ? "#{url}&#{sig_params}" : "#{url}?#{sig_params}" end |
Instance Method Details
#GET(inputs) ⇒ Object
Calls GET
on the remote API service and returns the data encapsulated in the response. The URL that is called is created by scoping and parameterizing the canonical resource URL based on inputs
.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/outside_in/resource/base.rb', line 74 def GET(inputs) url = self.class.sign(self.class.parameterize(self.class.scope(@url, inputs), inputs)) OutsideIn.logger.debug("Requesting #{url}") if OutsideIn.logger response = HTTParty.get(url) unless response.code < 300 raise ForbiddenException if response.code == 403 raise NotFoundException if response.code == 404 if response.headers.include?('x-mashery-error-code') raise ServiceException, response.headers['x-mashery-error-code'] else raise QueryException.new(JSON[response.body]) end end JSON[response.body] end |