Module: RequestDataFiltering

Defined in:
lib/macaw_framework/data_filters/request_data_filtering.rb

Overview

Module containing methods to filter Strings

Constant Summary collapse

VARIABLE_PATTERN =
%r{:[^/]+}

Class Method Summary collapse

Class Method Details

.extract_body(client, body_first_line, content_length) ⇒ Object

Method responsible for extracting the body from request



85
86
87
88
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 85

def self.extract_body(client, body_first_line, content_length)
  body = client.read(content_length)
  body_first_line << body.to_s
end

.extract_headers(client) ⇒ Object

Method responsible for extracting the headers from request



72
73
74
75
76
77
78
79
80
81
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 72

def self.extract_headers(client)
  header = client.gets.delete("\n").delete("\r")
  headers = {}
  while header.match(%r{[a-zA-Z0-9\-/*]*: [a-zA-Z0-9\-/*]})
    split_header = header.split(':')
    headers[split_header[0].strip] = split_header[1].strip
    header = client.gets.delete("\n").delete("\r")
  end
  [header, headers]
end

.extract_path(path) ⇒ Object

Method responsible for extracting the path from URI



66
67
68
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 66

def self.extract_path(path)
  path[0] == '/' ? path[1..].gsub('/', '.') : path.gsub('/', '.')
end

.extract_url_parameters(http_first_line) ⇒ Object

Method responsible for extracting the parameters from URI



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 92

def self.extract_url_parameters(http_first_line)
  return http_first_line, nil unless http_first_line =~ /\?/

  path_and_parameters = http_first_line.split('?', 2)
  path = "#{path_and_parameters[0]} "
  parameters_array = path_and_parameters[1].split('&')
  parameters_array.map! do |item|
    split_item = item.split('=')
    { sanitize_parameter_name(split_item[0]) => sanitize_parameter_value(split_item[1]) }
  end
  parameters = {}
  parameters_array.each { |item| parameters.merge!(item) }
  [path, parameters]
end

.match_path_with_route(split_path, split_route) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 47

def self.match_path_with_route(split_path, split_route)
  split_route.each_with_index do |var, index|
    return false if var != split_path[index] && !var.match?(VARIABLE_PATTERN)
  end

  true
end

.parse_request_data(client, routes) ⇒ Object

Method responsible for extracting information provided by the client like Headers and Body



13
14
15
16
17
18
19
20
21
22
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 13

def self.parse_request_data(client, routes)
  path, parameters = extract_url_parameters(client.gets.gsub('HTTP/1.1', ''))
  parameters = {} if parameters.nil?

  method_name = sanitize_method_name(path)
  method_name = select_path(method_name, routes, parameters)
  body_first_line, headers = extract_headers(client)
  body = extract_body(client, body_first_line, headers['Content-Length'].to_i)
  [path, method_name, headers, body, parameters]
end

.sanitize_method_name(path) ⇒ Object

Method responsible for sanitizing the method name



57
58
59
60
61
62
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 57

def self.sanitize_method_name(path)
  path = extract_path(path)
  method_name = path.gsub('/', '.').strip.downcase
  method_name.gsub!(' ', '')
  method_name
end

.sanitize_parameter_name(name) ⇒ Object

Method responsible for sanitizing the parameter name



109
110
111
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 109

def self.sanitize_parameter_name(name)
  name.gsub(/[^\w\s]/, '')
end

.sanitize_parameter_value(value) ⇒ Object

Method responsible for sanitizing the parameter value



115
116
117
118
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 115

def self.sanitize_parameter_value(value)
  value.gsub(/[^\w\s]/, '')
  value.gsub(/\s/, '')
end

.select_path(method_name, routes, parameters) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/macaw_framework/data_filters/request_data_filtering.rb', line 24

def self.select_path(method_name, routes, parameters)
  return method_name if routes.include?(method_name)

  selected_route = nil
  routes.each do |route|
    split_route = route.split('.')
    split_name = method_name.split('.')

    next unless split_route.length == split_name.length
    next unless match_path_with_route(split_name, split_route)

    selected_route = route
    split_route.each_with_index do |var, index|
      parameters[var[1..].to_sym] = split_name[index] if var =~ VARIABLE_PATTERN
    end
    break
  end

  raise EndpointNotMappedError if selected_route.nil?

  selected_route
end