Class: ReqresRspec::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/reqres_rspec/collector.rb

Constant Summary collapse

PARAM_IMPORTANCES =

Param importances

%w[required optional conditional deprecated]
PARAM_TYPES =

Param types NOTE: make sure sub-strings go at the end

['Boolean', 'Text', 'Float', 'DateTime', 'Date', 'File', 'UUID', 'Hash',
'Array of Integer', 'Array of String', 'Array', 'Integer', 'String',
'Email']
EXCLUDE_PARAMS =

Exclude replacement in symbolized path

%w[limit offset format description controller action]
EXCLUDE_RESPONSE_HEADER_PATTERNS =

response headers contain many unnecessary information, everything from this list will be stripped

%w[
  Cache-Control
  ETag
  X-Content-Type-Options
  X-Frame-Options
  X-Request-Id
  X-Runtime
  X-UA-Compatible
  X-XSS-Protection
  Vary
  Last-Modified
]
EXCLUDE_REQUEST_HEADER_PATTERNS =

request headers contain many unnecessary information, everything that match items from this list will be stripped

%w[
  action_controller.
  action_dispatch
  CONTENT_LENGTH
  HTTP_COOKIE
  HTTP_HOST
  HTTP_ORIGIN
  HTTP_USER_AGENT
  HTTPS
  ORIGINAL_FULLPATH
  ORIGINAL_SCRIPT_NAME
  PATH_INFO
  QUERY_STRING
  rack.
  raven.requested_at
  RAW_POST_DATA
  REMOTE_ADDR
  REQUEST_METHOD
  REQUEST_URI
  ROUTES_
  SCRIPT_NAME
  SERVER_NAME
  SERVER_PORT
  sinatra.commonlogger
  sinatra.route
  HTTP_X_API
  warden
  devise.mapping
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



65
66
67
# File 'lib/reqres_rspec/collector.rb', line 65

def initialize
  self.records = []
end

Instance Attribute Details

#recordsObject

Contains spec values read from rspec example, request and response



4
5
6
# File 'lib/reqres_rspec/collector.rb', line 4

def records
  @records
end

Instance Method Details

#collect(spec, example, request, response) ⇒ Object

collects spec data for further processing



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/reqres_rspec/collector.rb', line 70

def collect(spec, example, request, response)
  # TODO: remove boilerplate code
  return if request.nil? || response.nil? || !defined?(request.env)

  description = query_parameters = backend_parameters = 'not available'
  params = []
  if request.env && (request_params = request.env['action_dispatch.request.parameters'])
    if request_params['controller'] && request_params['action']
      description = get_action_description(request_params['controller'], request_params['action'])
      params = get_action_params(request_params['controller'], request_params['action'])
      query_parameters = request_params.reject { |p| %w[controller action format].include? p }
      backend_parameters = request_params.reject { |p| !%w[controller action format].include? p }
    end
  end

  ex_gr = spec.class.example.[:example_group]
  section = ex_gr[:description]
  while !ex_gr.nil? do
    section = ex_gr[:description]
    ex_gr = ex_gr[:parent_example_group]
  end

  self.records << {
    filename: prepare_filename_for(spec.class.),
    group: spec.class.[:reqres_section] || section, # Top level example group
    title: example_title(spec, example),
    description: description,
    params: params,
    request: {
      host: request.host,
      url: request.url,
      path: request.path.to_s.gsub('%2F', '/'),
      symbolized_path: get_symbolized_path(request),
      method: request.request_method,
      query_parameters: query_parameters,
      backend_parameters: backend_parameters,
      body: request.body.read,
      content_length: request.content_length,
      content_type: request.content_type,
      headers: read_request_headers(request),
      accept: (request.accept rescue nil)
    },
    response: {
      code: response.status,
      body: response.body,
      headers: read_response_headers(response),
      format: format(response)
    }
  }

  # cleanup query params
  begin
    body_hash = JSON.parse(self.records.last[:request][:body])
    query_hash = self.records.last[:request][:query_parameters]
    diff = Hash[*((query_hash.size > body_hash.size) ? query_hash.to_a - body_hash.to_a : body_hash.to_a - query_hash.to_a).flatten]
    self.records.last[:request][:query_parameters] = diff
  rescue
  end
end

#prepare_filename_for(metadata) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/reqres_rspec/collector.rb', line 130

def prepare_filename_for()
  description = [:description]
  example_group = if .key?(:example_group)
                    [:example_group]
                  else
                    [:parent_example_group]
                  end

  if example_group
    [prepare_filename_for(example_group), description].join('/')
  else
    description
  end.downcase.gsub(/[\W]+/, '_').gsub('__', '_').gsub(/^_|_$/, '')
end

#sortObject

sorts records alphabetically



146
147
148
149
150
151
# File 'lib/reqres_rspec/collector.rb', line 146

def sort
  self.records.sort! do |x,y|
    comp = x[:request][:symbolized_path] <=> y[:request][:symbolized_path]
    comp.zero? ? (x[:title] <=> y[:title]) : comp
  end
end