Class: MoesifHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/moesif_rack/moesif_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(debug) ⇒ MoesifHelpers



5
6
7
# File 'lib/moesif_rack/moesif_helpers.rb', line 5

def initialize(debug)
  @debug = debug
end

Instance Method Details

#format_replacement_body(replacement_body, original_body) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moesif_rack/moesif_helpers.rb', line 15

def format_replacement_body(replacement_body, original_body)
  # replacement_body is an hash or array json in this case.
  # but original body could be in chunks already. we want to follow suit.
  return original_body if replacement_body.nil?

  if original_body.instance_of?(Hash) || original_body.instance_of?(Array)
    log_debug 'original_body is a hash or array return as is'
    return replacement_body
  end

  if original_body.is_a? String
    log_debug 'original_body is a string, return a string format'
    return replacement_body.to_json.to_s
  end

  if original_body.respond_to?(:each) && original_body.respond_to?(:inject)
    # we know it is an chunks
    log_debug 'original_body respond to iterator, must likely chunks'
    [replacement_body.to_json.to_s]
  end

  [replacement_body.to_json.to_s]
rescue StandardError => e
  log_debug 'failed to convert replacement body ' + e.to_s
  [replacement_body.to_json.to_s]
end

#log_debug(message) ⇒ Object



9
10
11
12
13
# File 'lib/moesif_rack/moesif_helpers.rb', line 9

def log_debug(message)
  return unless @debug

  puts("#{Time.now} [Moesif Middleware] PID #{Process.pid} TID #{Thread.current.object_id} #{message}")
end

#parse_multipart(multipart_form_data, content_type) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moesif_rack/moesif_helpers.rb', line 42

def parse_multipart(multipart_form_data, content_type)
  log_debug("try to parse multiple part #{content_type}")

  sanitized_multipart_form_data = multipart_form_data.gsub(/\r?\n/, "\r\n")

  io = StringIO.new(sanitized_multipart_form_data)
  tempfile = Rack::Multipart::Parser::TEMPFILE_FACTORY
  bufsize = Rack::Multipart::Parser::BUFSIZE
  query_parser = Rack::Utils.default_query_parser
  result = Rack::Multipart::Parser.parse(io, sanitized_multipart_form_data.length, content_type, tempfile, bufsize,
                                         query_parser)

  log_debug('multipart parse result')
  log_debug(result.inspect)

  # this is a hash should be treated as JSON down the road.
  result.params
end