Class: OnlinePayments::SDK::MetaDataProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/onlinepayments/sdk/meta_data_provider.rb

Overview

Manages metadata about the server using the SDK

Defined Under Namespace

Classes: ServerMetaInfo

Constant Summary collapse

SDK_VERSION =
'0.0.1'.freeze
SERVER_META_INFO_HEADER =
'X-GCS-ServerMetaInfo'.freeze
PROHIBITED_HEADERS =
[SERVER_META_INFO_HEADER,
'X-GCS-Idempotence-Key', 'Date', 'Content-Type', 'Authorization'].sort!.freeze
CHARSET =
'utf-8'.freeze

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 (OnlinePayments::SDK::Domain::ShoppingCartExtension) (defaults to: nil)

    shopping cart-related metadata.

  • additional_request_headers (Array<OnlinePayments::SDK::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’



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/onlinepayments/sdk/meta_data_provider.rb', line 65

def initialize(integrator, shopping_cart_extension: nil, additional_request_headers: [].freeze)

  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 = 'OnlinePayments'
  server_meta_info.integrator = integrator
  server_meta_info.shopping_cart_extension = shopping_cart_extension if shopping_cart_extension
  server_meta_info_string = DefaultImpl::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?
    @meta_data_headers = [server_meta_info_header].freeze
  else
    request_headers = [server_meta_info_header]
    request_headers += additional_request_headers
    @meta_data_headers = request_headers.freeze
  end
end

Instance Attribute Details

#meta_data_headersArray<OnlinePayments::SDK::RequestHeader> (readonly)

List of headers that should be used in all requests.

Returns:



9
10
11
# File 'lib/onlinepayments/sdk/meta_data_provider.rb', line 9

def 
  @meta_data_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.



144
145
146
# File 'lib/onlinepayments/sdk/meta_data_provider.rb', line 144

def self.PROHIBITED_HEADERS
  PROHIBITED_HEADERS
end

.SDK_VERSIONObject

Version of this SDK being used



133
134
135
# File 'lib/onlinepayments/sdk/meta_data_provider.rb', line 133

def self.SDK_VERSION
  SDK_VERSION
end

.SERVER_META_INFO_HEADERObject

A RequestHeader that contains serialized and encoded ServerMetaInfo.



138
139
140
# File 'lib/onlinepayments/sdk/meta_data_provider.rb', line 138

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.



103
104
105
106
107
# File 'lib/onlinepayments/sdk/meta_data_provider.rb', line 103

def self.validate_additional_request_header(additional_request_header)
  if MetaDataProvider.PROHIBITED_HEADERS.include? additional_request_header.name
    raise ArgumentError, "request header '#{additional_request_header.name}' not allowed"
  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.



91
92
93
94
95
96
97
# File 'lib/onlinepayments/sdk/meta_data_provider.rb', line 91

def self.validate_additional_request_headers(additional_request_headers)
  if additional_request_headers
    additional_request_headers.each { |additional_request_header|
      validate_additional_request_header(additional_request_header)
    }
  end
end