Class: ResponseFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/dtracer/response_formatters.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ResponseFormatter

Returns a new instance of ResponseFormatter.



3
4
5
6
# File 'lib/dtracer/response_formatters.rb', line 3

def initialize(hash)
  @hash = hash
  generate
end

Instance Method Details

#body_section(hash) ⇒ Object



51
52
53
54
55
# File 'lib/dtracer/response_formatters.rb', line 51

def body_section(hash)
  return nil unless hash["body"]

  ["Body:\n  #{hash["body"]}"]
end


45
46
47
48
49
# File 'lib/dtracer/response_formatters.rb', line 45

def cookie_section(hash)
  return nil unless hash["cookies"]

  hash["cookies"].map { |name, value| "  #{name}: #{value}" }.insert(0, "Cookies:")
end

#error_section(hash) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/dtracer/response_formatters.rb', line 57

def error_section(hash)
  return nil unless hash["error"]
  arr = []
  arr << "Error:"
  arr << "  Error Code: #{hash["error"]["errorCode"]}"
  arr << "  Description: #{hash["error"]["localizedDescription"]}"
  arr
end

#generateObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dtracer/response_formatters.rb', line 8

def generate
  arr = []

  methods = [:response_section, :header_section, :cookie_section, :body_section, :error_section]

  methods.each do |method_name|
    # get content array for a request section
    content_array = self.send(method_name, @hash)
    arr.concat(content_array) if content_array

  end

  @content = arr.join("\n")
end

#header_section(hash) ⇒ Object



39
40
41
42
43
# File 'lib/dtracer/response_formatters.rb', line 39

def header_section(hash)
  return nil unless hash["headers"]

  hash["headers"].map { |key, value| "  #{key}: #{value}" }.insert(0, "Headers:")
end

#response_section(hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dtracer/response_formatters.rb', line 23

def response_section(hash)
  arr = []

  if hash["statusCode"]
    arr << "Status Code:"
    arr << "  #{hash["statusCode"]}"
  end

  if hash["url"]
    arr << "URL:"
    arr << "  #{hash["url"]}"
  end

  arr
end

#to_sObject



66
67
68
# File 'lib/dtracer/response_formatters.rb', line 66

def to_s
  @content
end