Class: Hoodoo::Services::Middleware::Interaction

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodoo/services/middleware/interaction.rb

Overview

Encapsulate all data related to an interaction (API call) within one object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, owning_middleware_instance, session = nil) ⇒ Interaction

Create a new Interaction instance, acquiring a new interaction ID automatically or picking up one from an X-Interaction-ID header if available.

A new context instance (see #context) is generated with a new empty request and response object attached, along with nil session data or the given session information in the input parameters:

env

The raw Rack request Hash. May be “{}” in some test scenarios. Converted to a Rack::Request instance. If this describes an X-Interaction-ID header then this Interaction will use - without validation - whatever value the header holds, else a new UUID is generated.

owning_middleware_instance

See #owning_middleware_instance.

session

The session data attached to the #context value; optional; if omitted, nil is used.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hoodoo/services/middleware/interaction.rb', line 98

def initialize( env, owning_middleware_instance, session = nil )
  @rack_request   = ::Rack::Request.new( env )
  @interaction_id = find_or_generate_interaction_id()
  @context        = Hoodoo::Services::Context.new(
    session,
    Hoodoo::Services::Request.new,
    Hoodoo::Services::Response.new( @interaction_id ),
    self
  )

  @owning_middleware_instance = owning_middleware_instance
  @context.request.headers    = env.select() do | k,v |
    k.to_s.start_with?( 'HTTP_' ) || k == 'CONTENT_TYPE' || k == 'CONTENT_LENGTH'
  end.freeze()
end

Instance Attribute Details

#contextObject

A Hoodoo::Services::Context instance representing this API call. May be updated/replaced during processing.



39
40
41
# File 'lib/hoodoo/services/middleware/interaction.rb', line 39

def context
  @context
end

#interaction_idObject (readonly)

Every interaction has a UUID passed back in API responses via the X-Interaction-ID HTTP header. This is that UUID.



34
35
36
# File 'lib/hoodoo/services/middleware/interaction.rb', line 34

def interaction_id
  @interaction_id
end

#owning_middleware_instanceObject (readonly)

API calls are handled by the middleware, so Interactions are created by Hoodoo::Services::Middleware instances. This is that creating instance, or the instance that should be treated as if it were the creator.



25
26
27
# File 'lib/hoodoo/services/middleware/interaction.rb', line 25

def owning_middleware_instance
  @owning_middleware_instance
end

#rack_requestObject (readonly)

The inbound Rack request; a Rack::Request instance.



29
30
31
# File 'lib/hoodoo/services/middleware/interaction.rb', line 29

def rack_request
  @rack_request
end

#requested_actionObject

The requested action, as a symbol; see Hoodoo::Services::Middleware::ALLOWED_ACTIONS.



54
55
56
# File 'lib/hoodoo/services/middleware/interaction.rb', line 54

def requested_action
  @requested_action
end

#requested_content_encodingObject

The requested content encoding as a String - e.g. “utf-8”.



62
63
64
# File 'lib/hoodoo/services/middleware/interaction.rb', line 62

def requested_content_encoding
  @requested_content_encoding
end

#requested_content_typeObject

The requested content type as a String - e.g. “application/json”.



58
59
60
# File 'lib/hoodoo/services/middleware/interaction.rb', line 58

def requested_content_type
  @requested_content_type
end

#target_implementationObject

The target Hoodoo::Services::Implementation instance for the API call. See #target_interface.



49
50
51
# File 'lib/hoodoo/services/middleware/interaction.rb', line 49

def target_implementation
  @target_implementation
end

#target_interfaceObject

The Hoodoo::Services::Interface subclass describing the resource interface that is the target of the API call.



44
45
46
# File 'lib/hoodoo/services/middleware/interaction.rb', line 44

def target_interface
  @target_interface
end

Instance Method Details

#using_test_sessionObject

Hoodoo middleware calls here to say “I’m using the test session” (or not), so that this can be enquired about via #using_test_session? if need be.



68
69
70
# File 'lib/hoodoo/services/middleware/interaction.rb', line 68

def using_test_session
  @using_test_session = true
end

#using_test_session?Boolean

Returns true if Hoodoo has previously called #using_test_session.

Returns:

  • (Boolean)


74
75
76
# File 'lib/hoodoo/services/middleware/interaction.rb', line 74

def using_test_session?
  @using_test_session == true
end