Class: Worldline::Connect::SDK::Communication::ResponseHeader
- Inherits:
-
Object
- Object
- Worldline::Connect::SDK::Communication::ResponseHeader
- Defined in:
- lib/worldline/connect/sdk/communication/response_header.rb
Overview
Represents HTTP response headers Each header is immutable has a #name and #value attribute
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
HTTP header name.
-
#value ⇒ String
readonly
HTTP header value.
Class Method Summary collapse
-
.get_disposition_filename(headers) ⇒ Object
Returns the value of the filename parameter of the Content-Disposition header from parameter headers If this Response does not contain a header with the given name, return nil instead.
-
.get_header(headers, header_name) ⇒ Object
Return the ResponseHeader that goes by the given header_name, If this Response does not contain a header with the given name, return nil instead.
-
.get_header_value(headers, header_name) ⇒ Object
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.
-
.trim_quotes(filename) ⇒ Object
Trims the single or double quotes at the beginning and end of parameter filename if they exist If they don’t exist, it returns the original filename instead.
Instance Method Summary collapse
-
#initialize(name, value) ⇒ ResponseHeader
constructor
Create a new header using the name and value given as parameters.
- #to_s ⇒ Object
Constructor Details
#initialize(name, value) ⇒ ResponseHeader
Create a new header using the name and value given as parameters.
13 14 15 16 17 |
# File 'lib/worldline/connect/sdk/communication/response_header.rb', line 13 def initialize(name, value) raise ArgumentError.new('name is required') if name.nil? or name.strip.empty? @name = name @value = value end |
Instance Attribute Details
#name ⇒ String (readonly)
HTTP header name
10 11 12 |
# File 'lib/worldline/connect/sdk/communication/response_header.rb', line 10 def name @name end |
#value ⇒ String (readonly)
HTTP header value
10 11 12 |
# File 'lib/worldline/connect/sdk/communication/response_header.rb', line 10 def value @value end |
Class Method Details
.get_disposition_filename(headers) ⇒ Object
Returns the value of the filename parameter of the Content-Disposition header from parameter headers If this Response does not contain a header with the given name, return nil instead
52 53 54 55 56 57 58 59 |
# File 'lib/worldline/connect/sdk/communication/response_header.rb', line 52 def self.get_disposition_filename(headers) header_value = get_header_value(headers, "Content-Disposition") if !header_value.nil? && header_value =~ /(?:^|;)\s*filename\s*=\s*(.*?)\s*(?:;|$)/i return trim_quotes($1) end return nil end |
.get_header(headers, header_name) ⇒ Object
Return the Worldline::Connect::SDK::Communication::ResponseHeader that goes by the given header_name, If this Response does not contain a header with the given name, return nil instead
28 29 30 31 32 33 34 35 |
# File 'lib/worldline/connect/sdk/communication/response_header.rb', line 28 def self.get_header(headers, header_name) selected_headers = headers.select { |h| h.name.casecmp(header_name) == 0 } if selected_headers.nil? || selected_headers.length == 0 return nil else return selected_headers[0] end end |
.get_header_value(headers, header_name) ⇒ Object
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
39 40 41 42 43 44 45 46 47 |
# File 'lib/worldline/connect/sdk/communication/response_header.rb', line 39 def self.get_header_value(headers, header_name) header = get_header(headers, header_name) return ( if header.nil? nil else header.value end) end |
.trim_quotes(filename) ⇒ Object
Trims the single or double quotes at the beginning and end of parameter filename if they exist If they don’t exist, it returns the original filename instead
65 66 67 68 69 70 71 |
# File 'lib/worldline/connect/sdk/communication/response_header.rb', line 65 def self.trim_quotes(filename) if filename.length >= 2 && ((filename.chars.first == '\'' && filename.chars.last == '\'') || (filename.chars.first == '"' && filename.chars.last == '"')) return filename[1...-1] end filename end |
Instance Method Details
#to_s ⇒ Object
22 23 24 |
# File 'lib/worldline/connect/sdk/communication/response_header.rb', line 22 def to_s "#{name}:#{value}" end |