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:



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

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.



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

def aftership_client
  @aftership_client
end

#as_api_keystring

Defines API keys used with API Key authentications.

Returns:

  • (string)


30
31
32
# File 'lib/aftership-tracking-sdk/configuration.rb', line 30

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)


35
36
37
# File 'lib/aftership-tracking-sdk/configuration.rb', line 35

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")


25
26
27
# File 'lib/aftership-tracking-sdk/configuration.rb', line 25

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)


48
49
50
# File 'lib/aftership-tracking-sdk/configuration.rb', line 48

def debugging
  @debugging
end

#domainObject

Defines domain



20
21
22
# File 'lib/aftership-tracking-sdk/configuration.rb', line 20

def domain
  @domain
end

#headersObject

Returns the value of attribute headers.



69
70
71
# File 'lib/aftership-tracking-sdk/configuration.rb', line 69

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)


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

def logger
  @logger
end

#max_retryObject

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



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

def max_retry
  @max_retry
end

#proxyObject

HTTP proxy



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

def proxy
  @proxy
end

#timeoutObject

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



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

def timeout
  @timeout
end

#user_agentstring

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

Returns:

  • (string)


41
42
43
# File 'lib/aftership-tracking-sdk/configuration.rb', line 41

def user_agent
  @user_agent
end

Class Method Details

.defaultObject

The default Configuration object.



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

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

Instance Method Details

#checkObject



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

def check
		fail InvalidOptionError.new("Invalid authentication type: #{authentication_type}") unless [AUTHENTICATION_TYPE_API_KEY, AUTHENTICATION_TYPE_AES, AUTHENTICATION_TYPE_RSA].include?(authentication_type)
		fail InvalidOptionError.new("as_api_key cannot be empty") unless as_api_key.to_s.size > 0
		fail InvalidOptionError.new("Invalid base URL: #{domain}") unless valid_url?(domain)
		fail InvalidOptionError.new("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 InvalidOptionError.new("timeout cannot be negative, value #{timeout}") unless timeout.to_i >= 0
		fail InvalidOptionError.new("max_retry must be in range 0..10, value #{max_retry}") unless max_retry.to_i >= 0 and max_retry.to_i <=10
		fail InvalidOptionError.new("max_retry cannot be negative, value #{max_retry}") unless max_retry.to_i >= 0
end

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

Yields:

  • (_self)

Yield Parameters:



96
97
98
# File 'lib/aftership-tracking-sdk/configuration.rb', line 96

def configure
		yield(self) if block_given?
end

#get_env(key) ⇒ Object



15
16
17
# File 'lib/aftership-tracking-sdk/configuration.rb', line 15

def get_env(key)
		ENV[SDK_PREFIX + '_' + key]
end

#valid_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


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

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