Class: CrapiDocs::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/crapidocs/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Session

Returns a new instance of Session.



5
6
7
8
# File 'lib/crapidocs/session.rb', line 5

def initialize(pattern)
  @pattern = pattern
  @actions = {}
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



3
4
5
# File 'lib/crapidocs/session.rb', line 3

def actions
  @actions
end

Instance Method Details

#body(path, method) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/crapidocs/session.rb', line 52

def body(path, method)
  res = @actions[path][method]
    .map { |a| a[:response] }
    .find { |r| r[:status] / 100 == 2 }
  return nil unless res
  parse_body(res[:body])
end

#merge(sessions) ⇒ Object



60
61
62
63
64
65
# File 'lib/crapidocs/session.rb', line 60

def merge(sessions)
  sessions = [sessions] unless sessions.is_a?(Array)
  @actions = sessions.reduce(@actions) do |a, session|
    a.deep_merge(session.actions)
  end
end

#methods(path) ⇒ Object



41
42
43
# File 'lib/crapidocs/session.rb', line 41

def methods(path)
  @actions[path].keys.sort
end

#params(path, method) ⇒ Object



45
46
47
48
49
50
# File 'lib/crapidocs/session.rb', line 45

def params(path, method)
  reqs = @actions[path][method].map { |a| a[:request] }
  params = merge_params(reqs)
  return nil unless params.keys.any?
  params
end

#pathsObject



37
38
39
# File 'lib/crapidocs/session.rb', line 37

def paths
  @actions.keys.sort
end

#track(args, result) ⇒ 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
# File 'lib/crapidocs/session.rb', line 10

def track(args, result)
  uri, env = args
  status, headers, body = result
  body = body.respond_to?(:body) ? body.body : ''
  method = env['REQUEST_METHOD']

  return unless relevant?(uri)

  action = {
    request: {
      method: method,
      body: env['rack.input'].string,
      headers: clean_headers(env)
    },
    response: {
      status: status,
      headers: headers,
      body: body
    }
  }

  path = clean_path(uri.path)
  @actions[path] ||= {}
  @actions[path][method] ||= []
  @actions[path][method] << action
end