Class: Worldline::Connect::SDK::Communication::MetadataProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/worldline/connect/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:

  • integrator (String)

    Name of the integrator

  • shopping_cart_extension (Worldline::Connect::SDK::Domain::ShoppingCartExtension) (defaults to: nil)

    shopping cart-related metadata.

  • additional_request_headers (Array<Worldline::Connect::SDK::Communication::RequestHeader>) (defaults to: [].freeze)

    list of additional headers to include in all requests made. The following headers are not allowed due to conflicts with already added headers: ‘X-GCS-Idempotence-Key’, ‘Date’, ‘Content-Type’, ‘Authorization’ and ‘X-GCS-ServerMetaInfo’

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/connect/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::Connect::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::Connect::SDK::Communication::RequestHeader> (readonly)

List of headers that should be used in all requests.

Returns:



14
15
16
# File 'lib/worldline/connect/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/connect/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/connect/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/connect/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: ‘X-GCS-Idempotence-Key’, ‘Date’, ‘Content-Type’, ‘Authorization’ and ‘X-GCS-ServerMetaInfo’ If the header is equal to one of the forbidden headers an ArgumentError is raised.



111
112
113
114
115
# File 'lib/worldline/connect/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: ‘X-GCS-Idempotence-Key’, ‘Date’, ‘Content-Type’, ‘Authorization’ and ‘X-GCS-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/connect/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