Class: Restify::Context
- Inherits:
-
Object
- Object
- Restify::Context
- Defined in:
- lib/restify/context.rb
Overview
A resource context.
The resource context contains relations and the effective response URI. The context is used to resolve relative URI and follow links.
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Options passed to this context.
-
#uri ⇒ Addressable::URI
readonly
Effective context URI.
Class Method Summary collapse
Instance Method Summary collapse
- #inherit(uri, **kwargs) ⇒ Object
-
#initialize(uri, **kwargs) ⇒ Context
constructor
A new instance of Context.
- #join(uri) ⇒ Object
- #process(response) ⇒ Object
- #request(method, uri, data = nil, opts = {}) ⇒ Object
Constructor Details
#initialize(uri, **kwargs) ⇒ Context
Returns a new instance of Context.
21 22 23 24 |
# File 'lib/restify/context.rb', line 21 def initialize(uri, **kwargs) @uri = uri.is_a?(Addressable::URI) ? uri : Addressable::URI.parse(uri.to_s) @options = kwargs end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Options passed to this context.
19 20 21 |
# File 'lib/restify/context.rb', line 19 def @options end |
#uri ⇒ Addressable::URI (readonly)
Effective context URI.
13 14 15 |
# File 'lib/restify/context.rb', line 13 def uri @uri end |
Class Method Details
.raise_response_error(response) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/restify/context.rb', line 66 def raise_response_error(response) case response.code when 400...500 raise ClientError.new(response) when 500...600 raise ServerError.new(response) else raise RuntimeError.new "Unknown response code: #{response.code}" end end |
Instance Method Details
#inherit(uri, **kwargs) ⇒ Object
30 31 32 33 |
# File 'lib/restify/context.rb', line 30 def inherit(uri, **kwargs) uri = self.uri unless uri Context.new uri, kwargs.merge() end |
#join(uri) ⇒ Object
26 27 28 |
# File 'lib/restify/context.rb', line 26 def join(uri) self.uri.join uri end |
#process(response) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/restify/context.rb', line 35 def process(response) context = inherit response.uri processor = Restify::PROCESSORS.find { |p| p.accept? response } processor ||= Restify::Processors::Base processor.new(context, response).resource end |
#request(method, uri, data = nil, opts = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/restify/context.rb', line 43 def request(method, uri, data = nil, opts = {}) request = Request.new \ method: method, uri: join(uri), data: data, headers: .fetch(:headers, {}) adapter.call(request).then do |response| if response.success? process response else Context.raise_response_error(response) end end end |