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, **kwargs) ⇒ Context

Returns a new instance of Context.



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

def initialize(uri, **kwargs)
  @uri = if uri.is_a?(Addressable::URI)
           uri
         else
           @uri = Addressable::URI.parse(uri.to_s)
         end

  @options = kwargs
end

Instance Attribute Details

#optionsHash (readonly)

Options passed to this context.

Returns:

  • (Hash)

    Options.



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

def options
  @options
end

#uriAddressable::URI (readonly)

Effective context URI.

Returns:

  • (Addressable::URI)

    Effective context URI.



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

def uri
  @uri
end

Class Method Details

.raise_response_error(response) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/restify/context.rb', line 104

def raise_response_error(response)
  case response.code
    when 400...500
      raise ClientError.new(response)
    when 500...600
      raise ServerError.new(response)
    else
      raise "Unknown response code: #{response.code}"
  end
end

Instance Method Details

#encode_with(coder) ⇒ Object



69
70
71
# File 'lib/restify/context.rb', line 69

def encode_with(coder)
  coder.map = marshal_dump
end

#inherit(uri, **kwargs) ⇒ Object



37
38
39
40
# File 'lib/restify/context.rb', line 37

def inherit(uri, **kwargs)
  uri = self.uri unless uri
  Context.new uri, kwargs.merge(options)
end

#init_with(coder) ⇒ Object



73
74
75
# File 'lib/restify/context.rb', line 73

def init_with(coder)
  marshal_load(coder.map)
end

#join(uri) ⇒ Object



33
34
35
# File 'lib/restify/context.rb', line 33

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

#marshal_dumpObject



77
78
79
80
81
82
# File 'lib/restify/context.rb', line 77

def marshal_dump
  {
    uri: uri.to_s,
    headers: headers
  }
end

#marshal_load(dump) ⇒ Object



84
85
86
87
# File 'lib/restify/context.rb', line 84

def marshal_load(dump)
  initialize dump.delete(:uri), \
    headers: dump.fetch(:headers)
end

#process(response) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/restify/context.rb', line 42

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, **kwargs) ⇒ Object

rubocop:disable Metrics/MethodLength



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/restify/context.rb', line 51

def request(method, uri, data: nil, **kwargs)
  request = Request.new(**kwargs,
    method: method,
    uri: join(uri),
    data: data,
    headers: headers
  )

  ret = cache.call(request) {|req| adapter.call(req) }
  ret.then do |response|
    if response.success?
      process response
    else
      Context.raise_response_error(response)
    end
  end
end