Class: AftershipAPI::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/aftership-tracking-sdk/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Configuration

Returns a new instance of Configuration.

Yields:

  • (_self)

Yield Parameters:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/aftership-tracking-sdk/configuration.rb', line 69

def initialize
  default_user_agent = "aftership-sdk-ruby/#{AftershipAPI::VERSION} (https://www.aftership.com) typhoeus/#{Typhoeus::VERSION}"

  @domain = get_env('DOMAIN') || 'https://api.aftership.com'
  @authentication_type = get_env('AUTHENTICATION_TYPE') || AUTHENTICATION_TYPE_API_KEY
  @as_api_key = get_env('API_KEY') || ''
  @as_api_secret = get_env('API_SECRET') || ''
  @user_agent = get_env('USER_AGENT') || default_user_agent
  @aftership_client = default_user_agent
  @timeout = (get_env('TIMEOUT') || 30).to_i
  @max_retry = (get_env('MAX_RETRY') || 2).to_i
  @proxy = get_env('PROXY')
  @debugging = get_env('DEBUGGING') || false
  @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
  @headers = {}

  yield(self) if block_given?
end

Instance Attribute Details

#aftership_clientObject

Returns the value of attribute aftership_client.



64
65
66
# File 'lib/aftership-tracking-sdk/configuration.rb', line 64

def aftership_client
  @aftership_client
end

#as_api_keystring

Defines API keys used with API Key authentications.

Returns:

  • (string)


27
28
29
# File 'lib/aftership-tracking-sdk/configuration.rb', line 27

def as_api_key
  @as_api_key
end

#as_api_secretstring

Defines AES secret or RSA private key used with AES or RSA authentications.

Returns:

  • (string)


32
33
34
# File 'lib/aftership-tracking-sdk/configuration.rb', line 32

def as_api_secret
  @as_api_secret
end

#authentication_type"API_KEY", ...

Defines the authentication type used in the API.

Returns:

  • ("API_KEY", "AES", "RSA")


22
23
24
# File 'lib/aftership-tracking-sdk/configuration.rb', line 22

def authentication_type
  @authentication_type
end

#debuggingtrue, false

Set this to enable/disable debugging. When enabled (set to true), HTTP request/response details will be logged with ‘logger.debug` (see the `logger` attribute). Default to false.

Returns:

  • (true, false)


45
46
47
# File 'lib/aftership-tracking-sdk/configuration.rb', line 45

def debugging
  @debugging
end

#domainObject

Defines domain



17
18
19
# File 'lib/aftership-tracking-sdk/configuration.rb', line 17

def domain
  @domain
end

#headersObject

Returns the value of attribute headers.



66
67
68
# File 'lib/aftership-tracking-sdk/configuration.rb', line 66

def headers
  @headers
end

#logger#debug

Defines the logger used for debugging. Default to ‘Rails.logger` (when in Rails) or logging to STDOUT.

Returns:

  • (#debug)


51
52
53
# File 'lib/aftership-tracking-sdk/configuration.rb', line 51

def logger
  @logger
end

#max_retryObject

When response is a retryable error, retry current request Default to 2



59
60
61
# File 'lib/aftership-tracking-sdk/configuration.rb', line 59

def max_retry
  @max_retry
end

#proxyObject

HTTP proxy



62
63
64
# File 'lib/aftership-tracking-sdk/configuration.rb', line 62

def proxy
  @proxy
end

#timeoutObject

The time limit for HTTP request in seconds. Default to 30



55
56
57
# File 'lib/aftership-tracking-sdk/configuration.rb', line 55

def timeout
  @timeout
end

#user_agentstring

Defines the user agent used in the API requests. Default to ‘aftership-sdk-ruby/$VERSION’

Returns:

  • (string)


38
39
40
# File 'lib/aftership-tracking-sdk/configuration.rb', line 38

def user_agent
  @user_agent
end

Class Method Details

.defaultObject

The default Configuration object.



89
90
91
# File 'lib/aftership-tracking-sdk/configuration.rb', line 89

def self.default
  @@default ||= Configuration.new
end

Instance Method Details

#checkObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/aftership-tracking-sdk/configuration.rb', line 101

def check
  fail ApiError.new(:error_code => INVALID_OPTION, :message => "Invalid authentication type: #{authentication_type}") unless [AUTHENTICATION_TYPE_API_KEY, AUTHENTICATION_TYPE_AES, AUTHENTICATION_TYPE_RSA].include?(authentication_type)
  fail ApiError.new(:error_code => INVALID_API_KEY, :message => "Invalid API key") unless as_api_key.to_s.size > 0
  fail ApiError.new(:error_code => INVALID_OPTION, :message => "Invalid base URL: #{domain}") unless valid_url?(domain)
  fail ApiError.new(:error_code => INVALID_OPTION, :message => "authentication type must not be API_KEY if as_api_secret is set") if as_api_secret.to_s.size > 0 && authentication_type == AUTHENTICATION_TYPE_API_KEY
  fail ApiError.new(:error_code => INVALID_OPTION, :message => "as_api_secret cannot be empty when authentication type is #{authentication_type}") if [AUTHENTICATION_TYPE_AES, AUTHENTICATION_TYPE_RSA].include?(authentication_type) && as_api_secret.to_s.size == 0
  fail ApiError.new(:error_code => INVALID_OPTION, :message => "timeout cannot be negative, value #{timeout}") unless timeout.to_i >= 0
  fail ApiError.new(:error_code => INVALID_OPTION, :message => "max_retry must be in range 0..10, value #{max_retry}") unless max_retry.to_i >= 0 and max_retry.to_i <= 10
  fail ApiError.new(:error_code => INVALID_OPTION, :message => "max_retry cannot be negative, value #{max_retry}") unless max_retry.to_i >= 0
end

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



93
94
95
# File 'lib/aftership-tracking-sdk/configuration.rb', line 93

def configure
  yield(self) if block_given?
end

#get_env(key) ⇒ Object



12
13
14
# File 'lib/aftership-tracking-sdk/configuration.rb', line 12

def get_env(key)
  ENV["AFTERSHIP_TRACKING_SDK_" + key]
end

#valid_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
# File 'lib/aftership-tracking-sdk/configuration.rb', line 112

def valid_url?(url)
  uri = URI.parse(url)
  uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
  rescue URI::InvalidURIError
    false
end