Class: Worldline::Acquiring::SDK::Communication::MetadataProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/worldline/acquiring/sdk/communication/metadata_provider.rb

Overview

Manages metadata about the server using the SDK

Defined Under Namespace

Classes: ServerMetaInfo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(integrator, shopping_cart_extension: nil, additional_request_headers: [].freeze) ⇒ MetadataProvider

Create a new MetadataProvider instance that can be used to access platform-related information

Parameters:

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/worldline/acquiring/sdk/communication/metadata_provider.rb', line 73

def initialize(integrator, shopping_cart_extension: nil, additional_request_headers: [].freeze)
  raise ArgumentError.new('integrator is required') if integrator.nil? or integrator.strip.empty?
  MetadataProvider.validate_additional_request_headers(additional_request_headers)
  server_meta_info = ServerMetaInfo.new
  server_meta_info.platform_identifier = get_platform_identifier
  server_meta_info.sdk_identifier = get_sdk_identifier
  server_meta_info.sdk_creator = 'Worldline'
  server_meta_info.integrator = integrator
  server_meta_info.shopping_cart_extension = shopping_cart_extension unless shopping_cart_extension.nil?
  server_meta_info_string = Worldline::Acquiring::SDK::JSON::DefaultMarshaller.instance.marshal(server_meta_info)
  server_meta_info_header = RequestHeader.new(
    SERVER_META_INFO_HEADER, Base64.strict_encode64(
    server_meta_info_string.force_encoding('iso-8859-1').encode(CHARSET)))
  if additional_request_headers.nil? || additional_request_headers.empty?
    @metadata_headers = [server_meta_info_header].freeze
  else
    request_headers = [server_meta_info_header]
    request_headers += additional_request_headers
    @metadata_headers = request_headers.freeze
  end
end

Instance Attribute Details

#metadata_headersArray<Worldline::Acquiring::SDK::Communication::RequestHeader> (readonly)

List of headers that should be used in all requests.

Returns:



14
15
16
# File 'lib/worldline/acquiring/sdk/communication/metadata_provider.rb', line 14

def 
  @metadata_headers
end

Class Method Details

.prohibited_headersObject

A list of header names that should not be used by any added headers. These headers are reserved for specific purposes.



153
154
155
# File 'lib/worldline/acquiring/sdk/communication/metadata_provider.rb', line 153

def self.prohibited_headers
  PROHIBITED_HEADERS
end

.sdk_versionObject

Version of this SDK being used



141
142
143
# File 'lib/worldline/acquiring/sdk/communication/metadata_provider.rb', line 141

def self.sdk_version
  SDK_VERSION
end

.server_meta_info_headerObject

A RequestHeader that contains serialized and encoded ServerMetaInfo.



147
148
149
# File 'lib/worldline/acquiring/sdk/communication/metadata_provider.rb', line 147

def self.server_meta_info_header
  SERVER_META_INFO_HEADER
end

.validate_additional_request_header(additional_request_header) ⇒ Object

Checks that the RequestHeaders additional_request_header is equal to any of the forbidden headers. The forbidden headers are: ‘Date’, ‘Content-Type’, ‘Authorization’ and ‘X-WL-ServerMetaInfo’ If the header is equal to one of the forbidden headers an ArgumentError is raised.



111
112
113
114
115
# File 'lib/worldline/acquiring/sdk/communication/metadata_provider.rb', line 111

def self.validate_additional_request_header(additional_request_header)
  if prohibited_headers.include? additional_request_header.name
    raise ArgumentError.new('request header not allowed' + additional_request_header.name)
  end
end

.validate_additional_request_headers(additional_request_headers) ⇒ Object

Checks that none of the RequestHeaders in additional_request_headers is equal to any of the forbidden headers. The forbidden headers are: ‘Date’, ‘Content-Type’, ‘Authorization’ and ‘X-WL-ServerMetaInfo’ If a header is found that is equal to one of the forbidden headers an ArgumentError is raised.



99
100
101
102
103
104
105
# File 'lib/worldline/acquiring/sdk/communication/metadata_provider.rb', line 99

def self.validate_additional_request_headers(additional_request_headers)
  unless additional_request_headers.nil?
    additional_request_headers.each { |additional_request_header|
      validate_additional_request_header(additional_request_header)
    }
  end
end