Class: CheckoutSdk::EnvironmentSubdomain

Inherits:
Object
  • Object
show all
Defined in:
lib/checkout_sdk/environment_subdomain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, subdomain) ⇒ EnvironmentSubdomain

Initializes the EnvironmentSubdomain with the given environment and subdomain.

Parameters:

  • environment (Environment)

    The environment object which should have a base_uri method.

  • subdomain (String)

    The subdomain to add to the environment’s API URL.



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_uriObject (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

#environmentEnvironment

Returns:



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

#subdomainString

Returns:

  • (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