Class: OutsideIn::Resource::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/outside_in/resource/base.rb

Overview

This class is abstract.

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.

Examples:

resource = new MyResource("/an/endpoint")
data = resource.GET({'publication-id' => 1234, 'limit' => 5})

Since:

  • 1.0

Direct Known Subclasses

LocationFinder, StoryFinder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relative_url) ⇒ OutsideIn::Resource::Base

Returns a new instance. Stores the absolutized, signed URL.

Parameters:

  • relative_url (String)

    a URL relative to the version component of the base service URL

Since:

  • 1.0



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.

Parameters:

  • url (String)

    the URL

  • inputs (Hash<String, Object>)

    the data inputs

Returns:

  • (String)

    the URL including query parameters

Raises:

  • (NotImplementedError)

Since:

  • 1.0



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.

Parameters:

  • url (String)

    the URL

  • inputs (Hash<String, Object>)

    the data inputs

Returns:

  • (String)

    the potentially scoped URL

Raises:

  • (NotImplementedError)

Since:

  • 1.0



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.

Parameters:

  • url (String)

    a URL to be signed

Returns:

  • (String)

    the signed URL

Raises:

Since:

  • 1.0



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.

Parameters:

  • inputs (Hash<String, Object>)

    the data inputs

Returns:

  • (Object)

    the returned data structure as defined by the API specification (as parsed from the JSON envelope)

Raises:

Since:

  • 1.0



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