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.



17
18
19
20
21
22
# File 'lib/checkout_sdk/environment_subdomain.rb', line 17

def initialize(environment, subdomain)
  @environment = environment
  @subdomain = subdomain
  @base_uri = create_url_with_subdomain(environment.base_uri, subdomain)
  @authorization_uri = create_url_with_subdomain(environment.authorization_uri, subdomain)
end

Instance Attribute Details

#authorization_uriString

Returns:

  • (String)


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
54
55
56
57
# File 'lib/checkout_sdk/environment_subdomain.rb', line 10

class EnvironmentSubdomain
  attr_reader :base_uri, :authorization_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 = create_url_with_subdomain(environment.base_uri, subdomain)
    @authorization_uri = create_url_with_subdomain(environment.authorization_uri, subdomain)
  end

  private

  # Applies subdomain transformation to any given URI.
  # If the subdomain is valid (alphanumeric pattern), prepends it to the host.
  # Otherwise, returns the original URI unchanged.
  #
  # @param original_url [String] The original URL to transform.
  # @param subdomain [String] The subdomain to prepend to the host.
  # @return [String] The transformed URL with subdomain, or original URL if subdomain is invalid.
  def create_url_with_subdomain(original_url, subdomain)
    new_environment = original_url

    if subdomain =~ /^[0-9a-z]+$/
      url_parts = URI.parse(original_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

#base_uriObject (readonly)

Returns the value of attribute base_uri.



11
12
13
# File 'lib/checkout_sdk/environment_subdomain.rb', line 11

def base_uri
  @base_uri
end

#environmentEnvironment

Returns:



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
54
55
56
57
# File 'lib/checkout_sdk/environment_subdomain.rb', line 10

class EnvironmentSubdomain
  attr_reader :base_uri, :authorization_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 = create_url_with_subdomain(environment.base_uri, subdomain)
    @authorization_uri = create_url_with_subdomain(environment.authorization_uri, subdomain)
  end

  private

  # Applies subdomain transformation to any given URI.
  # If the subdomain is valid (alphanumeric pattern), prepends it to the host.
  # Otherwise, returns the original URI unchanged.
  #
  # @param original_url [String] The original URL to transform.
  # @param subdomain [String] The subdomain to prepend to the host.
  # @return [String] The transformed URL with subdomain, or original URL if subdomain is invalid.
  def create_url_with_subdomain(original_url, subdomain)
    new_environment = original_url

    if subdomain =~ /^[0-9a-z]+$/
      url_parts = URI.parse(original_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)


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
54
55
56
57
# File 'lib/checkout_sdk/environment_subdomain.rb', line 10

class EnvironmentSubdomain
  attr_reader :base_uri, :authorization_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 = create_url_with_subdomain(environment.base_uri, subdomain)
    @authorization_uri = create_url_with_subdomain(environment.authorization_uri, subdomain)
  end

  private

  # Applies subdomain transformation to any given URI.
  # If the subdomain is valid (alphanumeric pattern), prepends it to the host.
  # Otherwise, returns the original URI unchanged.
  #
  # @param original_url [String] The original URL to transform.
  # @param subdomain [String] The subdomain to prepend to the host.
  # @return [String] The transformed URL with subdomain, or original URL if subdomain is invalid.
  def create_url_with_subdomain(original_url, subdomain)
    new_environment = original_url

    if subdomain =~ /^[0-9a-z]+$/
      url_parts = URI.parse(original_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