Class: CheckoutSdk::EnvironmentSubdomain
- Inherits:
-
Object
- Object
- CheckoutSdk::EnvironmentSubdomain
- Defined in:
- lib/checkout_sdk/environment_subdomain.rb
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
- #environment ⇒ Environment
- #subdomain ⇒ String
Instance Method Summary collapse
-
#initialize(environment, subdomain) ⇒ EnvironmentSubdomain
constructor
Initializes the EnvironmentSubdomain with the given environment and subdomain.
Constructor Details
#initialize(environment, subdomain) ⇒ EnvironmentSubdomain
Initializes the EnvironmentSubdomain with the given environment and subdomain.
15 16 17 18 19 |
# File 'lib/checkout_sdk/environment_subdomain.rb', line 15 def initialize(environment, subdomain) @environment = environment @subdomain = subdomain @base_uri = add_subdomain_to_api_url_environment(environment, subdomain) end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
9 10 11 |
# File 'lib/checkout_sdk/environment_subdomain.rb', line 9 def base_uri @base_uri end |
#environment ⇒ Environment
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/checkout_sdk/environment_subdomain.rb', line 8 class EnvironmentSubdomain attr_reader :base_uri, :environment, :subdomain # Initializes the EnvironmentSubdomain with the given environment and subdomain. # # @param environment [Environment] The environment object which should have a base_uri method. # @param subdomain [String] The subdomain to add to the environment's API URL. def initialize(environment, subdomain) @environment = environment @subdomain = subdomain @base_uri = add_subdomain_to_api_url_environment(environment, subdomain) end private # Adds the subdomain to the API URL of the given environment. # # @param environment [Environment] The environment object which should have a base_uri method. # @param subdomain [String] The subdomain to add to the environment's API URL. # @return [String] The new environment URL with the subdomain added. def add_subdomain_to_api_url_environment(environment, subdomain) api_url = environment.base_uri new_environment = api_url if subdomain =~ /^[0-9a-z]+$/ url_parts = URI.parse(api_url) new_host = "#{subdomain}.#{url_parts.host}" port = url_parts.scheme == 'https' && url_parts.port == 443 ? nil : url_parts.port new_url_parts = URI::Generic.build( scheme: url_parts.scheme, userinfo: url_parts.userinfo, host: new_host, port: port, path: url_parts.path, query: url_parts.query, fragment: url_parts.fragment ) new_environment = new_url_parts.to_s end new_environment end end |
#subdomain ⇒ String
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/checkout_sdk/environment_subdomain.rb', line 8 class EnvironmentSubdomain attr_reader :base_uri, :environment, :subdomain # Initializes the EnvironmentSubdomain with the given environment and subdomain. # # @param environment [Environment] The environment object which should have a base_uri method. # @param subdomain [String] The subdomain to add to the environment's API URL. def initialize(environment, subdomain) @environment = environment @subdomain = subdomain @base_uri = add_subdomain_to_api_url_environment(environment, subdomain) end private # Adds the subdomain to the API URL of the given environment. # # @param environment [Environment] The environment object which should have a base_uri method. # @param subdomain [String] The subdomain to add to the environment's API URL. # @return [String] The new environment URL with the subdomain added. def add_subdomain_to_api_url_environment(environment, subdomain) api_url = environment.base_uri new_environment = api_url if subdomain =~ /^[0-9a-z]+$/ url_parts = URI.parse(api_url) new_host = "#{subdomain}.#{url_parts.host}" port = url_parts.scheme == 'https' && url_parts.port == 443 ? nil : url_parts.port new_url_parts = URI::Generic.build( scheme: url_parts.scheme, userinfo: url_parts.userinfo, host: new_host, port: port, path: url_parts.path, query: url_parts.query, fragment: url_parts.fragment ) new_environment = new_url_parts.to_s end new_environment end end |