Class: Ingenico::Connect::SDK::Logging::LogMessageBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ingenico/connect/sdk/logging/log_message_builder.rb

Overview

Abstract class used to construct a message describing a request or response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_id, body_obfuscator = Obfuscation::BodyObfuscator.default_obfuscator, header_obfuscator = Obfuscation::HeaderObfuscator.default_obfuscator) ⇒ LogMessageBuilder

Create a new LogMessageBuilder

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 24

def initialize(request_id,
               body_obfuscator = Obfuscation::BodyObfuscator.default_obfuscator,
               header_obfuscator = Obfuscation::HeaderObfuscator.default_obfuscator)
  raise ArgumentError if request_id.nil? or request_id.empty?
  raise ArgumentError if body_obfuscator.nil?
  raise ArgumentError if header_obfuscator.nil?
  @request_id = request_id
  @headers = ''
  @body_obfuscator = body_obfuscator
  @header_obfuscator = header_obfuscator
end

Instance Attribute Details

#bodyString (readonly)

Request or response body as a string

Returns:

  • (String)

    the current value of body



14
15
16
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 14

def body
  @body
end

#body_obfuscatorObject (readonly)

Returns the value of attribute body_obfuscator.



20
21
22
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 20

def body_obfuscator
  @body_obfuscator
end

#content_typeString (readonly)

Content type of the body, generally ‘application/json’ or ‘text/html’

Returns:

  • (String)

    the current value of content_type



14
15
16
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 14

def content_type
  @content_type
end

#header_obfuscatorObject (readonly)

Returns the value of attribute header_obfuscator.



21
22
23
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 21

def header_obfuscator
  @header_obfuscator
end

#headersString (readonly)

Request or response headers in string form

Returns:

  • (String)

    the current value of headers



14
15
16
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 14

def headers
  @headers
end

#request_idString (readonly)

An identifier assigned to the request and response

Returns:

  • (String)

    the current value of request_id



14
15
16
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 14

def request_id
  @request_id
end

Instance Method Details

#add_headers(name, value) ⇒ Object

Adds a single header to the #headers string



37
38
39
40
41
42
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 37

def add_headers(name, value)
  @headers += ', ' if @headers.length > 0
  @headers += name + '="'
  @headers += @header_obfuscator.obfuscate_header(name, value) unless value.nil?
  @headers += '"'
end

#get_messageObject

Constructs and returns the log message as a string.

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 58

def get_message
  raise NotImplementedError.new("#{self.class.name}#get_message() is not implemented.")
end

#is_binary(content_type) ⇒ Object

Returns whether or not the content type is binary



76
77
78
79
80
81
82
83
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 76

def is_binary(content_type)
  if content_type.nil?
    false
  else
    content_type = content_type.downcase
    !(content_type.start_with?("text/") || content_type.include?("json") || content_type.include?("xml"))
  end
end

#set_body(body, content_type) ⇒ Object

Parameters:

  • body (String)

    the message body

  • content_type (String)

    the content type of the body



48
49
50
51
52
53
54
55
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 48

def set_body(body, content_type)
  if is_binary(content_type)
    @body = "<binary content>"
  else
    @body = @body_obfuscator.obfuscate_body(body)
  end
  @content_type = content_type
end

#to_sObject



62
63
64
65
66
67
68
# File 'lib/ingenico/connect/sdk/logging/log_message_builder.rb', line 62

def to_s
  if self.class == LogMessageBuilder
    super.to_s
  else
    get_message
  end
end