Class: AgileProxy::RequestSpec

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/agile_proxy/model/request_spec.rb

Overview

The Request Spec model

The request spec is an input/output specification that incoming HTTP(s) requests are matched against.

It uses the action dispatch router to do this matching, which is fed data from the database as it’s input - i.e. its routing table is generated on the fly.

This model is responsible not only for retrieving and persisting these request specifications, but also for creating the response

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ RequestSpec

Returns a new instance of RequestSpec.



23
24
25
26
# File 'lib/agile_proxy/model/request_spec.rb', line 23

def initialize(attrs = {})
  attrs[:http_method] = attrs.delete(:method) if attrs.key?(:method)
  super
end

Instance Method Details

#call(params, headers, body) ⇒ Array

This method’s output is a ‘rack’ response, but its input is not. When the router has determined that this request spec is the one that is going to be sent to the client, it will call this method with the request’s parameters, headers and body.

if no response has been specified, an empty body will be returned, otherwise a ‘rack’ version of the response is returned

Parameters:

  • params (Hash)

    the request parameters

  • headers (Hash)

    The request headers

  • body (String)

    The request body

Returns:

  • (Array)

    The rack response



44
45
46
# File 'lib/agile_proxy/model/request_spec.rb', line 44

def call(params, headers, body)
  response.nil? ? [204, { 'Content-Type' => 'text/plain' }, ''] : response.to_rack(params, headers, body)
end

#conditions_jsonHash

The conditions are at present, stored as a JSON string. This is editable as a string in the UI, and therefore accessible using ‘conditions’ as normal. This method returns a JSON decoded version of this as a HASH

Returns:

  • (Hash)

    decoded conditions



31
32
33
# File 'lib/agile_proxy/model/request_spec.rb', line 31

def conditions_json
  ActiveSupport::JSON.decode(conditions).with_indifferent_access
end