Class: Jets::SpecHelpers::Controllers::Request
- Inherits:
-
Object
- Object
- Jets::SpecHelpers::Controllers::Request
- Extended by:
- Memoist
- Defined in:
- lib/jets/spec_helpers/controllers/request.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #dispatch! ⇒ Object
- #event ⇒ Object
- #extract_parameters ⇒ Object
-
#initialize(method, path, headers = {}, params = {}) ⇒ Request
constructor
A new instance of Request.
- #normalized_path ⇒ Object
- #path_params ⇒ Object
- #route ⇒ Object
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
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
5 6 7 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 5 def params @params end |
#path ⇒ Object
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
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 77 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 |
#event ⇒ Object
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 54 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 10 def event json = {} id_params = route.path.scan(%r{:([^/]+)}).flatten = escape_path(path) 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? escaped_path_param_value = CGI.escape(path_param_value.to_s) .gsub!(":#{id_param}", escaped_path_param_value) path_parameters.deep_merge!(id_param => escaped_path_param_value) end json['resource'] = escape_path(path) json['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.deep_dup end json end |
#extract_parameters ⇒ Object
61 62 63 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 61 def extract_parameters route.extract_parameters(normalized_path).symbolize_keys end |
#normalized_path ⇒ Object
65 66 67 68 69 70 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 65 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_params ⇒ Object
56 57 58 |
# File 'lib/jets/spec_helpers/controllers/request.rb', line 56 def path_params params.path_params.reverse_merge(extract_parameters) end |