Class: Restify::Context
- Inherits:
-
Object
- Object
- Restify::Context
- Includes:
- Addressable, Relations
- 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
-
#response ⇒ Response
readonly
The response object.
-
#uri ⇒ Addressable::URI
readonly
Effective context URI.
Class Method Summary collapse
Instance Method Summary collapse
- #add_relation(name, uri) ⇒ Object
- #expand(uri) ⇒ Object
- #follow ⇒ Object
- #inherit(opts = {}) ⇒ Object
- #inherit_value(value) ⇒ Object
-
#initialize(uri, http, response = nil, _opts = {}) ⇒ Context
constructor
A new instance of Context.
- #new_relation(uri) ⇒ Object
- #new_value(value) ⇒ Object
- #relations ⇒ Object
- #request(method, uri, data = nil, opts = {}) ⇒ Object
Methods included from Relations
Constructor Details
#initialize(uri, http, response = nil, _opts = {}) ⇒ Context
Returns a new instance of Context.
24 25 26 27 28 |
# File 'lib/restify/context.rb', line 24 def initialize(uri, http, response = nil, _opts = {}) @uri = uri.is_a?(URI) ? uri : URI.parse(uri.to_s) @http = http @response = response end |
Instance Attribute Details
#response ⇒ Response (readonly)
The response object.
16 17 18 |
# File 'lib/restify/context.rb', line 16 def response @response end |
#uri ⇒ Addressable::URI (readonly)
Effective context URI.
22 23 24 |
# File 'lib/restify/context.rb', line 22 def uri @uri end |
Class Method Details
.raise_response_error(response) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/restify/context.rb', line 114 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
#add_relation(name, uri) ⇒ Object
71 72 73 |
# File 'lib/restify/context.rb', line 71 def add_relation(name, uri) @relations[name] = new_relation(uri) end |
#expand(uri) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/restify/context.rb', line 34 def (uri) case uri when Addressable::Template Addressable::Template.new self.uri.join(uri.pattern).to_s else self.uri.join uri end end |
#follow ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/restify/context.rb', line 43 def follow if follow_location new_relation follow_location else raise RuntimeError.new 'Nothing to follow' end end |
#inherit(opts = {}) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/restify/context.rb', line 75 def inherit(opts = {}) Context.new \ opts.fetch(:uri) { @uri }, opts.fetch(:http) { @http }, opts.fetch(:response) { nil } end |
#inherit_value(value) ⇒ Object
63 64 65 |
# File 'lib/restify/context.rb', line 63 def inherit_value(value) inherit.new_value value end |
#new_relation(uri) ⇒ Object
67 68 69 |
# File 'lib/restify/context.rb', line 67 def new_relation(uri) Relation.new(self, uri.to_s, (uri)) end |
#new_value(value) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/restify/context.rb', line 82 def new_value(value) case value when Hash Resource.new(self, value) when Array Collection.new(self, value) else value end end |
#relations ⇒ Object
30 31 32 |
# File 'lib/restify/context.rb', line 30 def relations @relations ||= load_relations end |
#request(method, uri, data = nil, opts = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/restify/context.rb', line 51 def request(method, uri, data = nil, opts = {}) request = @http.request(method, (uri), data, opts) request.then do |response| if response.success? inherit(uri: response.uri, response: response) .new_value(response.decoded_body) else Context.raise_response_error(response) end end end |