Class: Apipie::Extractor::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie/extractor/recorder.rb

Defined Under Namespace

Modules: FunctionalTestRecording Classes: Middleware

Instance Method Summary collapse

Constructor Details

#initializeRecorder

Returns a new instance of Recorder.



4
5
6
# File 'lib/apipie/extractor/recorder.rb', line 4

def initialize
  @ignored_params = [:controller, :action]
end

Instance Method Details

#analyse_controller(controller) ⇒ Object



20
21
22
23
# File 'lib/apipie/extractor/recorder.rb', line 20

def analyse_controller(controller)
  @controller = controller.class
  @action = controller.params[:action]
end

#analyse_env(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/apipie/extractor/recorder.rb', line 8

def analyse_env(env)
  @verb = env["REQUEST_METHOD"].to_sym
  @path = env["PATH_INFO"].sub(/^\/*/,"/")
  @query = env["QUERY_STRING"] unless env["QUERY_STRING"].blank?
  @params = Rack::Utils.parse_nested_query(@query)
  @params.merge!(env["action_dispatch.request.request_parameters"] || {})
  if data = parse_data(env["rack.input"].read)
    @request_data = data
    env["rack.input"].rewind
  end
end

#analyse_response(response) ⇒ Object



25
26
27
28
29
30
# File 'lib/apipie/extractor/recorder.rb', line 25

def analyse_response(response)
  if response.last.respond_to?(:body) && data = parse_data(response.last.body)
    @response_data = data
  end
  @code = response.first
end

#analyze_functional_test(test_context) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/apipie/extractor/recorder.rb', line 32

def analyze_functional_test(test_context)
  request, response = test_context.request, test_context.response
  @verb = request.request_method.to_sym
  @path = request.path
  @params = request.request_parameters
  if [:POST, :PUT].include?(@verb)
    @request_data = @params
  else
    @query = request.query_string
  end
  @response_data = parse_data(response.body)
  @code = response.code
end

#parse_data(data) ⇒ Object



46
47
48
49
50
51
# File 'lib/apipie/extractor/recorder.rb', line 46

def parse_data(data)
  return nil if data.to_s =~ /^\s*$/
  JSON.parse(data)
rescue Exception => e
  data
end

#recordObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/apipie/extractor/recorder.rb', line 65

def record
  if @controller
    {:controller => @controller,
     :action => @action,
     :verb => @verb,
     :path => @path,
     :params => @params,
     :query => @query,
     :request_data => @request_data,
     :response_data => @response_data,
     :code => @code}
  else
    nil
  end
end

#reformat_data(data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/apipie/extractor/recorder.rb', line 53

def reformat_data(data)
  parsed = parse_data(data)
  case parsed
  when nil
    nil
  when String
    parsed
  else
    JSON.pretty_generate().gsub(/: \[\s*\]/,": []").gsub(/\{\s*\}/,"{}")
  end
end