class Ingenico::Direct::SDK::RequestHeader
Represents HTTP request headers Each header is immutable has a name
and value
attribute
@attr_reader [String] name HTTP header name @attr_reader [String] value HTTP header value
Attributes
name[R]
value[R]
Public Class Methods
get_header(headers, header_name)
click to toggle source
Return the {Ingenico::Direct::SDK::ResponseHeader} that goes by the given header_name, If this Response does not contain a header with the given name, return nil instead
# File lib/ingenico/direct/sdk/request_header.rb, line 26 def self.get_header(headers, header_name) selected_headers = headers.select { |h| h.name == header_name } return selected_headers&.length.positive? ? selected_headers[0] : nil end
get_header_value(headers, header_name)
click to toggle source
Returns the header value of the header that goes by the given header_name, If this response does not contain a header with the given name, return nil instead
# File lib/ingenico/direct/sdk/request_header.rb, line 35 def self.get_header_value(headers, header_name) return get_header(headers, header_name)&.value end
new(name, value)
click to toggle source
Create a new header using the name and value given as parameters.
# File lib/ingenico/direct/sdk/request_header.rb, line 11 def initialize(name, value) raise ArgumentError, 'name is required' if name.nil? || name.strip.empty? @name = name @value = normalize_value(value) end
Public Instance Methods
to_s()
click to toggle source
# File lib/ingenico/direct/sdk/request_header.rb, line 20 def to_s "#{name}:#{value}" end
Private Instance Methods
normalize_value(value)
click to toggle source
# File lib/ingenico/direct/sdk/request_header.rb, line 41 def normalize_value(value) return value if value.nil? || value.empty? # Replace all sequences of whitespace*-linebreak-whitespace* into a single linebreak-space # This will ensure that: # - no line ends with whitespace, because this causes authentication failures # - each line starts with a single whitespace, so it is a valid header value value.gsub(/[\s&&[^\r\n]]*(\r?\n)[\s&&[^\r\n]]*/, '\1 ') end