Class: Jets::SpecHelpers::Controllers::Request

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/spec_helpers/controllers/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, headers = {}, params = {}) ⇒ Request

Returns a new instance of Request.



6
7
8
# File 'lib/jets/spec_helpers/controllers/request.rb', line 6

def initialize(method, path, headers={}, params={})
  @method, @path, @headers, @params = method, path, headers, params
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



5
6
7
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5

def method
  @method
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5

def params
  @params
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5

def path
  @path
end

Instance Method Details

#dispatch!Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jets/spec_helpers/controllers/request.rb', line 76

def dispatch!
  klass = Object.const_get(route.controller_name)
  controller = klass.new(event, {}, route.action_name)
  response = controller.process! # response is API Gateway Hash

  unless response['statusCode'] && response['body']
    raise "Expected response to an API Gateway Hash Structure. Are you rendering correctly?"
  end

  Response.new(response) # converts APIGW hash to prettier object
end

#eventObject



10
11
12
13
14
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
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jets/spec_helpers/controllers/request.rb', line 10

def event
  json = {}
  id_params = route.path.scan(%r{:([^/]+)}).flatten
  expanded_path = path.dup
  path_parameters = {}

  id_params.each do |id_param|
    raise "missing param: :#{id_param}" unless path_params.include? id_param.to_sym

    path_param_value = path_params[id_param.to_sym]
    raise "Path param :#{id_param} value cannot be blank" if path_param_value.blank?

    expanded_path.gsub!(":#{id_param}", path_param_value.to_s)
    path_parameters.deep_merge!(id_param => path_param_value.to_s)
  end

  json['resource'] = path
  json['path'] = expanded_path
  json['httpMethod'] = method.to_s.upcase
  json['pathParameters'] = path_parameters
  json['headers'] = (headers || {}).stringify_keys

  if method != :get
    json['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
    body = Rack::Multipart.build_multipart(params.body_params)

    if body
      json['headers']['Content-Length'] ||= body.length.to_s
      json['headers']['Content-Type'] = "multipart/form-data; boundary=#{Rack::Multipart::MULTIPART_BOUNDARY}"
    else
      body = Rack::Utils.build_nested_query(params.body_params)
    end

    json['body'] = Base64.encode64(body)
    json['isBase64Encoded'] = true
  end

  params.query_params.each do |key, value|
    json['queryStringParameters'] ||= {}
    json['queryStringParameters'][key.to_s] = value.to_s
  end

  json
end

#extract_parametersObject



60
61
62
# File 'lib/jets/spec_helpers/controllers/request.rb', line 60

def extract_parameters
  route.extract_parameters(normalized_path).symbolize_keys
end

#normalized_pathObject



64
65
66
67
68
69
# File 'lib/jets/spec_helpers/controllers/request.rb', line 64

def normalized_path
  path = self.path
  path = path[0..-2] if path.end_with? '/'
  path = path[1..-1] if path.start_with? '/'
  path
end

#path_paramsObject



55
56
57
# File 'lib/jets/spec_helpers/controllers/request.rb', line 55

def path_params
  params.path_params.reverse_merge(extract_parameters)
end

#routeObject



71
72
73
# File 'lib/jets/spec_helpers/controllers/request.rb', line 71

def route
  Jets::Router::Finder.new(normalized_path, method).run
end