Class: Excon::HyperMedia::Middlewares::HypertextCachePattern

Inherits:
Middleware::Base
  • Object
show all
Defined in:
lib/excon/hypermedia/middlewares/hypertext_cache_pattern.rb

Overview

HypertextCachePattern

This middleware handles hcp-enabled requests.

@see: tools.ietf.org/html/draft-kelly-json-hal-06#section-8.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#datumObject (readonly)

Returns the value of attribute datum.



13
14
15
# File 'lib/excon/hypermedia/middlewares/hypertext_cache_pattern.rb', line 13

def datum
  @datum
end

Instance Method Details

#request_call(datum) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/excon/hypermedia/middlewares/hypertext_cache_pattern.rb', line 15

def request_call(datum)
  @datum = datum

  if stubs.any?
    # We've created new stubs.  The request should be marked as `mocked`
    # to make sure the stubbed response is returned.
    datum[:mock] = true

    # The requested resource might not be part of the embedded resources
    # so we allow external requests.
    # datum[:allow_unstubbed_requests] = true

    # Make sure Excon's `Mock` middleware runs after this middleware, as
    # it might have already triggered in the middleware chain.
    orig_stack = @stack
    @stack = Excon::Middleware::Mock.new(orig_stack)
  end

  super
rescue StandardError => e
  raise unless e.class == Excon::Errors::StubNotFound

  # If a request was made to a non-stubbed resource, don't use the Mock
  # middleware, but simply send the request to the server.
  @stack = orig_stack
  super
end

#response_call(datum) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/excon/hypermedia/middlewares/hypertext_cache_pattern.rb', line 43

def response_call(datum)
  @datum = datum

  # After the response is returned, remove any request-specific stubs
  # from Excon, so they can't be accidentally re-used anymore.
  embedded.each { |r| (match = matcher(r)) && Excon.unstub(match) }

  super
end