Class: Apipie::Extractor::Recorder
  
  
  
  
  
    - Inherits:
 
    - 
      Object
      
        
          - Object
 
          
            - Apipie::Extractor::Recorder
 
          
        
        show all
      
     
  
  
  
  
  
  
  
  
  
  
    - Defined in:
 
    - lib/apipie/extractor/recorder.rb
 
  
  
 
Defined Under Namespace
  
    
      Modules: FunctionalTestRecording
    
  
    
      Classes: Middleware
    
  
  
    
      Constant Summary
      collapse
    
    
      
        - MULTIPART_BOUNDARY =
          
        
 
        'APIPIE_RECORDER_EXAMPLE_BOUNDARY'
 
      
    
  
  
    
      Instance Method Summary
      collapse
    
    
  
  Constructor Details
  
    
  
  
    
Returns a new instance of Recorder.
   
 
  
  
    
      
6
7
8 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 6
def initialize
  @ignored_params = [:controller, :action]
end 
     | 
  
 
  
 
  
    Instance Method Details
    
      
  
  
    #analyse_controller(controller)  ⇒ Object 
  
  
  
  
    
      
25
26
27
28 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 25
def analyse_controller(controller)
  @controller = controller.class
  @action = controller.params[:action]
end 
     | 
  
 
    
      
  
  
    #analyse_env(env)  ⇒ Object 
  
  
  
  
    
      
10
11
12
13
14
15
16
17
18
19
20
21
22
23 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 10
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"] || {})
  rack_input = env["rack.input"]
  if data = parse_data(rack_input.read)
    @request_data = data
  elsif form_hash = env["rack.request.form_hash"]
    @request_data = reformat_multipart_data(form_hash)
  end
  rack_input.rewind
end
     | 
  
 
    
      
  
  
    #analyse_response(response)  ⇒ Object 
  
  
  
  
    
      
30
31
32
33
34
35
36
37
38
39 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 30
def analyse_response(response)
  if response.last.respond_to?(:body) && data = parse_data(response.last.body)
    @response_data = if response[1]['Content-Disposition'].to_s.start_with?('attachment')
                       '<STREAMED ATTACHMENT FILE>'
                     else
                       data
                     end
  end
  @code = response.first
end
     | 
  
 
    
      
  
  
    #analyze_functional_test(test_context)  ⇒ Object 
  
  
  
  
    
      
41
42
43
44
45
46
47
48
49
50
51
52
53 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 41
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, :PATCH, :DELETE].include?(@verb)
    @request_data = @params
  else
    @query = request.query_string
  end
  @response_data = parse_data(response.body)
  @code = response.code
end
     | 
  
 
    
      
  
  
    #content_disposition(name)  ⇒ Object 
  
  
  
  
    
      
89
90
91 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 89
def content_disposition(name)
  %{Content-Disposition: form-data; name="#{name}"}
end
     | 
  
 
    
      
  
  
    #parse_data(data)  ⇒ Object 
  
  
  
  
    
      
55
56
57
58
59
60 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 55
def parse_data(data)
  return nil if data.strip.blank?
  JSON.parse(data)
rescue StandardError
  data
end 
     | 
  
 
    
      
  
  
    #record  ⇒ Object 
  
  
  
  
    
      
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 105
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
     | 
  
 
    
      
  
  
    
      
93
94
95
96
97
98
99
100
101
102
103 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 93
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
     | 
  
 
    
      
  
  
    
      
77
78
79
80
81
82
83
84
85
86
87 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 77
def reformat_hash(boundary, attrs, lines)
  if head = attrs[:head]
    lines << boundary
    lines.concat(head.split("\r\n"))
        lines << '' << %{... contents of "#{attrs[:name]}" ...}
  else
        attrs.each { |k,v| v.is_a?(Hash) and reformat_hash(boundary, v, lines) }
  end
end
     | 
  
 
    
      
  
  
    
      
62
63
64
65
66
67
68
69
70
71
72
73
74
75 
     | 
    
      # File 'lib/apipie/extractor/recorder.rb', line 62
def reformat_multipart_data(form)
  form.empty? and return ''
  lines = ["Content-Type: multipart/form-data; boundary=#{MULTIPART_BOUNDARY}",'']
  boundary = "--#{MULTIPART_BOUNDARY}"
  form.each do |key, attrs|
    if attrs.is_a?(String)
      lines << boundary << content_disposition(key) << "Content-Length: #{attrs.size}" << '' << attrs
    else
      reformat_hash(boundary, attrs, lines)
    end
  end
  lines << "#{boundary}--"
  lines.join("\n")
end
     |