Class: Restify::Context

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, opts = {}) ⇒ Context

Returns a new instance of Context.



15
16
17
18
# File 'lib/restify/context.rb', line 15

def initialize(uri, opts = {})
  @uri  = uri.is_a?(Addressable::URI) ? uri : Addressable::URI.parse(uri.to_s)
  @opts = opts
end

Instance Attribute Details

#uriAddressable::URI (readonly)

Effective context URI.

Returns:

  • (Addressable::URI)

    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



49
50
51
52
53
54
55
56
57
58
# File 'lib/restify/context.rb', line 49

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

#join(uri) ⇒ Object



20
21
22
# File 'lib/restify/context.rb', line 20

def join(uri)
  self.uri.join uri
end

#process(response) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/restify/context.rb', line 24

def process(response)
  context   = Context.new response.uri, @opts
  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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/restify/context.rb', line 32

def request(method, uri, data = nil, opts = {})
  request = Request.new \
    method: method,
    uri: join(uri),
    data: data,
    headers: @opts.fetch(:headers, {})

  Restify.adapter.call(request).then do |response|
    if response.success?
      process response
    else
      Context.raise_response_error(response)
    end
  end
end