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
# 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, prepending the subdomain to the host.
  #
  # @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.
  # @raise [CheckoutArgumentException] If the subdomain is not a valid merchant-specific
  #   subdomain.
  def create_url_with_subdomain(original_url, subdomain)
    unless subdomain =~ /\A(?:pl-)?[a-z0-9]+\z/
      raise CheckoutArgumentException,
            'invalid environment subdomain - provide your merchant-specific subdomain, ' \
            'typically your client ID excluding the cli_ prefix (see ' \
            'https://api-reference.checkout.com/#section/Base-URLs)'
    end

    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

    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
    ).to_s
  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
# 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, prepending the subdomain to the host.
  #
  # @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.
  # @raise [CheckoutArgumentException] If the subdomain is not a valid merchant-specific
  #   subdomain.
  def create_url_with_subdomain(original_url, subdomain)
    unless subdomain =~ /\A(?:pl-)?[a-z0-9]+\z/
      raise CheckoutArgumentException,
            'invalid environment subdomain - provide your merchant-specific subdomain, ' \
            'typically your client ID excluding the cli_ prefix (see ' \
            'https://api-reference.checkout.com/#section/Base-URLs)'
    end

    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

    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
    ).to_s
  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
# 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, prepending the subdomain to the host.
  #
  # @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.
  # @raise [CheckoutArgumentException] If the subdomain is not a valid merchant-specific
  #   subdomain.
  def create_url_with_subdomain(original_url, subdomain)
    unless subdomain =~ /\A(?:pl-)?[a-z0-9]+\z/
      raise CheckoutArgumentException,
            'invalid environment subdomain - provide your merchant-specific subdomain, ' \
            'typically your client ID excluding the cli_ prefix (see ' \
            'https://api-reference.checkout.com/#section/Base-URLs)'
    end

    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

    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
    ).to_s
  end
end