Module: AdsCommon::ApiConfig
- Defined in:
- lib/ads_common/api_config.rb
Overview
Contains helper methods for loading and managing the available services. This module is meant to be imported into API-specific modules.
Constant Summary collapse
- ADS_COMMON_VERSION =
'0.6.2'
Instance Method Summary collapse
-
#api_name ⇒ Object
Get the API name.
-
#api_path ⇒ Object
Get the API path.
-
#auth_server(environment) ⇒ Object
Get the authentication server details for an environment.
-
#default_config_filename ⇒ Object
Get the default filename for the config file.
-
#default_environment ⇒ Object
Get the default environment.
-
#default_version ⇒ Object
Get the default API version.
-
#do_require(version, service) ⇒ Object
Perform the loading of the necessary source files for a version.
-
#endpoint(environment, version, service) ⇒ Object
Get the endpoint for a service on a given environment and API version.
-
#environment_has_version(environment, version) ⇒ Object
Does the given environment exist and contain the given version?.
-
#environments ⇒ Object
Get the available environments.
-
#get_wsdl_base(environment, version) ⇒ Object
Returns WSDL base url defined in Service configuration.
-
#get_wsdls(version) ⇒ Object
Generates an array of WSDL URLs based on defined Services and version supplied.
-
#interface_name(version, service) ⇒ Object
Returns the full interface class name for a given service.
-
#latest_version ⇒ Object
Get the latest API version.
-
#module_name(version, service) ⇒ Object
Returns the full module name for a given service.
-
#services(version) ⇒ Object
Get the list of service names for a given version.
-
#subdir(version, service) ⇒ Object
Get the subdirectory for a service, for a given API version.
-
#version_has_service(version, service) ⇒ Object
Does the given version exist and contain the given service?.
-
#versions ⇒ Object
Get the available API versions.
-
#wrapper_name(version, service) ⇒ Object
Returns the full wrapper class name for a given service.
Instance Method Details
#api_name ⇒ Object
Get the API name.
102 103 104 |
# File 'lib/ads_common/api_config.rb', line 102 def api_name raise NotImplementedError, 'api_name not overriden.' end |
#api_path ⇒ Object
Get the API path.
107 108 109 |
# File 'lib/ads_common/api_config.rb', line 107 def api_path return api_name.to_s.snakecase end |
#auth_server(environment) ⇒ Object
Get the authentication server details for an environment. Allows to override the auth URL via environmental variable.
Args:
-
environment: the service environment to be used (as a string)
Returns: The full URL for the auth server.
166 167 168 169 170 171 |
# File 'lib/ads_common/api_config.rb', line 166 def auth_server(environment) auth_server_url = ENV['ADSAPI_AUTH_URL'] || auth_server_config[environment] return auth_server_url end |
#default_config_filename ⇒ Object
Get the default filename for the config file.
121 122 123 |
# File 'lib/ads_common/api_config.rb', line 121 def default_config_filename raise NotImplementedError, 'default_config_filename not overriden.' end |
#default_environment ⇒ Object
Get the default environment.
Returns: Default environment (as a string)
116 117 118 |
# File 'lib/ads_common/api_config.rb', line 116 def default_environment raise NotImplementedError, 'default_environment not overriden.' end |
#default_version ⇒ Object
Get the default API version.
Returns: Default version (as an integer)
76 77 78 |
# File 'lib/ads_common/api_config.rb', line 76 def default_version nil end |
#do_require(version, service) ⇒ Object
Perform the loading of the necessary source files for a version.
Args:
-
version: the API version (as a symbol)
-
service: service name (as a symbol)
Returns: The filename that was loaded.
182 183 184 185 186 187 |
# File 'lib/ads_common/api_config.rb', line 182 def do_require(version, service) filename = "%s/%s/%s" % [api_path, version.to_s, service.to_s.snakecase] require filename return filename end |
#endpoint(environment, version, service) ⇒ Object
Get the endpoint for a service on a given environment and API version.
Args:
-
environment: the service environment to be used (as a string)
-
version: the API version (as an integer)
-
service: the name of the API service (as a string)
Returns: The endpoint URL (as a string)
135 136 137 138 139 140 141 |
# File 'lib/ads_common/api_config.rb', line 135 def endpoint(environment, version, service) base = get_wsdl_base(environment, version) if !subdir_config().nil? base = base.to_s + subdir_config()[[version, service]].to_s end return base.to_s + version.to_s + '/' end |
#environment_has_version(environment, version) ⇒ Object
Does the given environment exist and contain the given version?
Returns: Boolean indicating whether the given environment exists and contains the given version
55 56 57 58 |
# File 'lib/ads_common/api_config.rb', line 55 def environment_has_version(environment, version) return environment_config.include?(environment) && environment_config[environment].include?(version) end |
#environments ⇒ Object
Get the available environments.
Returns: List of available environments (as strings)
97 98 99 |
# File 'lib/ads_common/api_config.rb', line 97 def environments environment_config.keys end |
#get_wsdl_base(environment, version) ⇒ Object
Returns WSDL base url defined in Service configuration. Allows to override the base URL via environmental variable.
Args:
- environment: environment to use like :SANDBOX or :PRODUCTION
- version: the API version.
Returns:
String containing base URL.
263 264 265 266 267 |
# File 'lib/ads_common/api_config.rb', line 263 def get_wsdl_base(environment, version) wsdl_base = ENV['ADSAPI_BASE_URL'] || wsdl_base = environment_config[environment][version] return wsdl_base end |
#get_wsdls(version) ⇒ Object
Generates an array of WSDL URLs based on defined Services and version supplied. This method is used by generators to determine what service wrappers to generate.
Args:
- version: the API version.
Returns
hash of pairs Service => WSDL URL.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/ads_common/api_config.rb', line 239 def get_wsdls(version) res = {} wsdl_base = get_wsdl_base(default_environment(), version) services(version).each do |service| path = wsdl_base if (!subdir_config().nil?) subdir_name = subdir(version, service); path = path + subdir_name if subdir_name and !subdir_name.empty? end path = path + version.to_s + '/' + service.to_s + '?wsdl' res[service.to_s] = path end return res end |
#interface_name(version, service) ⇒ Object
Returns the full interface class name for a given service.
Args:
-
version: the API version (as an integer)
-
service: the service name (as a string)
Returns: The full interface class name for the given service (as a string)
212 213 214 |
# File 'lib/ads_common/api_config.rb', line 212 def interface_name(version, service) return module_name(version, service) + "::" + service.to_s end |
#latest_version ⇒ Object
Get the latest API version.
Returns: Latest version (as an integer)
45 46 47 |
# File 'lib/ads_common/api_config.rb', line 45 def latest_version service_config.keys.select { |service| service.is_a? Integer }.max end |
#module_name(version, service) ⇒ Object
Returns the full module name for a given service.
Args:
-
version: the API version (as a symbol)
-
service: the service name (as a symbol)
Returns: The full module name for the given service (as a string).
198 199 200 201 |
# File 'lib/ads_common/api_config.rb', line 198 def module_name(version, service) return "%s::%s::%s" % [api_name, version.to_s.upcase, service.to_s] end |
#services(version) ⇒ Object
Get the list of service names for a given version
Args:
-
version: the API version (as an integer)
Returns: List of names of services (as strings) available for given version
88 89 90 |
# File 'lib/ads_common/api_config.rb', line 88 def services(version) service_config[version] end |
#subdir(version, service) ⇒ Object
Get the subdirectory for a service, for a given API version.
Args:
-
version: the API version (as an integer)
-
service: the name of the API service (as a string)
Returns: The endpoint URL (as a string)
152 153 154 155 |
# File 'lib/ads_common/api_config.rb', line 152 def subdir(version, service) return nil if subdir_config().nil? subdir_config()[[version, service]] end |
#version_has_service(version, service) ⇒ Object
Does the given version exist and contain the given service?
Returns: Boolean indicating whether the given version exists and contains the given service
66 67 68 69 |
# File 'lib/ads_common/api_config.rb', line 66 def version_has_service(version, service) return service_config.include?(version) && service_config[version].include?(service) end |
#versions ⇒ Object
Get the available API versions.
Returns: List of versions available (as integers)
36 37 38 |
# File 'lib/ads_common/api_config.rb', line 36 def versions service_config.keys end |
#wrapper_name(version, service) ⇒ Object
Returns the full wrapper class name for a given service
Args:
-
version: the API version (as an integer)
-
service: the service name (as a string)
Returns: The full wrapper class name for the given service (as a string)
225 226 227 |
# File 'lib/ads_common/api_config.rb', line 225 def wrapper_name(version, service) return module_name(version, service) + "::#{service}Wrapper" end |