Class: Synthra::Types::AddressLocation::CountryCode

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/address_location/locations.rb

Overview

CountryCode type

Constant Summary collapse

FALLBACK_CODES =

Fallback country codes if Faker is unavailable

ISO 3166-1 alpha-2 country codes used when Faker cannot generate codes.

Returns:

  • (Array<String>)

    list of 2-letter country codes

%w[
  US GB CA AU DE FR JP CN IN BR
  MX ES IT NL SE NO DK FI CH AT
  BE IE NZ SG KR TW HK MY PH ID
  TH VN PL CZ HU RO BG GR PT EG
  ZA NG KE MA AE SA IL TR UA RU
].freeze

Instance Method Summary collapse

Instance Method Details

#generate_edge(rng, context, args) ⇒ Object



164
165
166
# File 'lib/synthra/types/address_location/locations.rb', line 164

def generate_edge(rng, context, args)
  ["", "A", "USA"].sample(random: rng.instance_variable_get(:@random))
end

#generate_invalid(rng, context, args) ⇒ Object



168
169
170
# File 'lib/synthra/types/address_location/locations.rb', line 168

def generate_invalid(rng, context, args)
  [nil, 123, [], {}].sample(random: rng.instance_variable_get(:@random))
end

#generate_random(rng, context, args) ⇒ String

Generate a random country code

Uses Faker::Address.country_code for realistic ISO 3166-1 alpha-2 codes.

Examples:

generate_random(rng, {})
# => "DE"

Parameters:

  • rng (Generator::RNG)

    random number generator

  • args (Hash)

    type arguments (not used)

Returns:

  • (String)

    two-letter country code



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/synthra/types/address_location/locations.rb', line 150

def generate_random(rng, context, args)
  adapter = faker_adapter(context)
  if adapter
    adapter.country_code

  else
    Faker::Address.country_code
  end

rescue StandardError
  rng.sample(FALLBACK_CODES)
end