Class: Restify::Context

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relations

#rel, #rel?

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

#responseResponse (readonly)

The response object.

Returns:



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

def response
  @response
end

#uriAddressable::URI (readonly)

Effective context URI.

Returns:

  • (Addressable::URI)

    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 expand(uri)
  case uri
    when Addressable::Template
      Addressable::Template.new self.uri.join(uri.pattern).to_s
    else
      self.uri.join uri
  end
end

#followObject



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, expand(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

#relationsObject



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, expand(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