Class: Faker::Address

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/address.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.building_numberString

Produces a building number.

Examples:

Faker::Address.building_number #=> "7304"

Returns:

  • (String)

Available since:

  • 0.3.0



74
75
76
# File 'lib/faker/default/address.rb', line 74

def building_number
  bothify(fetch('address.building_number'))
end

.city(options: {}) ⇒ String

Produces the name of a city.

Examples:

Faker::Address.city #=> "Imogeneborough"
Faker::Address.city(options: { with_state: true })
  #=> "Northfort, California"

Parameters:

  • options (Hash) (defaults to: {})
  • with_state (Hash)

    a customizable set of options

Returns:

  • (String)

Available since:

  • 0.3.0



21
22
23
# File 'lib/faker/default/address.rb', line 21

def city(options: {})
  parse(options[:with_state] ? 'address.city_with_state' : 'address.city')
end

.city_prefixString

Produces a city prefix.

Examples:

Faker::Address.city_prefix #=> "Lake"

Returns:

  • (String)

Available since:

  • 0.3.0



178
179
180
# File 'lib/faker/default/address.rb', line 178

def city_prefix
  fetch('address.city_prefix')
end

.city_suffixString

Produces a city suffix.

Examples:

Faker::Address.city_suffix #=> "fort"

Returns:

  • (String)

Available since:

  • 0.3.0



165
166
167
# File 'lib/faker/default/address.rb', line 165

def city_suffix
  fetch('address.city_suffix')
end

.communityString

Produces the name of a community.

Examples:

Faker::Address.community #=> "University Crossing"

Returns:

  • (String)

Available since:

  • 1.8.0



87
88
89
# File 'lib/faker/default/address.rb', line 87

def community
  parse('address.community')
end

.countryString

Produces the name of a country.

Examples:

Faker::Address.country #=> "French Guiana"

Returns:

  • (String)

Available since:

  • 0.3.0



217
218
219
# File 'lib/faker/default/address.rb', line 217

def country
  fetch('address.country')
end

.country_by_code(code: 'US') ⇒ String

Produces a country by ISO country code. See the [List of ISO 3166 country codes](en.wikipedia.org/wiki/List_of_ISO_3166_country_codes) on Wikipedia for a full list.

Examples:

Faker::Address.country_by_code(code: 'NL') #=> "Netherlands"

Parameters:

  • code (String) (defaults to: 'US')

    An ISO country code.

Returns:

  • (String)

Available since:

  • 1.9.2



233
234
235
# File 'lib/faker/default/address.rb', line 233

def country_by_code(code: 'US')
  fetch("address.country_by_code.#{code}")
end

.country_codeString

Produces an ISO 3166 country code.

Examples:

Faker::Address.country_code #=> "IT"

Returns:

  • (String)

Available since:

  • 1.4.0



260
261
262
# File 'lib/faker/default/address.rb', line 260

def country_code
  fetch('address.country_code')
end

.country_code_longString

Produces a long (alpha-3) ISO 3166 country code.

Examples:

Faker::Address.country_code_long #=> "ITA"

Returns:

  • (String)

Available since:

  • 0.3.0



273
274
275
# File 'lib/faker/default/address.rb', line 273

def country_code_long
  fetch('address.country_code_long')
end

.country_name_to_code(name: 'united_states') ⇒ String

Produces an ISO 3166 country code when given a country name.

Examples:

Faker::Address.country_name_to_code(name: 'united_states') #=> "US"

Parameters:

  • name (String) (defaults to: 'united_states')

    Country name in snake_case format.

Returns:

  • (String)

Available since:

  • 1.9.2



247
248
249
# File 'lib/faker/default/address.rb', line 247

def country_name_to_code(name: 'united_states')
  fetch("address.country_by_name.#{name}")
end

.full_addressString

Produces a full address.

Examples:

Faker::Address.full_address
  #=> "282 Kevin Brook, Imogeneborough, CA 58517"

Returns:

  • (String)

Available since:

  • 0.3.0



313
314
315
# File 'lib/faker/default/address.rb', line 313

def full_address
  parse('address.full_address')
end

.full_address_as_hash(*attrs, **attrs_params) ⇒ Hash

Produces Address hash of required fields

Faker::Address.full_address_as_hash(:full_address)
   #=> {:full_address=>"87635 Rice Street, Lake Brentonton, OR 61896-5968"}

Faker::Address.full_address_as_hash(:city, :time_zone)
   #=> {:city=>"East Faustina", :time_zone=>"America/Mexico_City"}

Faker::Address.full_address_as_hash(:street_address, street_address: {include_secondary: true})
   #=> {:street_address=>"29423 Kenneth Causeway Suite 563"}

Examples:

Faker::Address.full_address_as_hash(:longitude,
                                    :latitude,
                                    :country_name_to_code,
                                    country_name_to_code: {name: 'united_states'})
  #=> {:longitude=>-101.74428917174603, :latitude=>-37.40056749089944, :country_name_to_code=>"US"}

Returns:

  • (Hash)

Available since:

  • 2.13.0



339
340
341
342
343
344
345
# File 'lib/faker/default/address.rb', line 339

def full_address_as_hash(*attrs, **attrs_params)
  attrs.map!(&:to_sym)
  attrs_params.transform_keys!(&:to_sym)
  attrs.map do |attr|
    { "#{attr}": attrs_params[attr] ? send(attr, **attrs_params[attr]) : send(attr) }
  end.reduce({}, :merge)
end

.latitudeFloat

Produces a latitude.

Examples:

Faker::Address.latitude #=> -58.17256227443719

Returns:

  • (Float)

Available since:

  • 1.0.0



286
287
288
# File 'lib/faker/default/address.rb', line 286

def latitude
  ((rand * 180) - 90).to_f
end

.longitudeFloat

Produces a longitude.

Examples:

Faker::Address.longitude #=> -156.65548382095133

Returns:

  • (Float)

Available since:

  • 1.0.0



299
300
301
# File 'lib/faker/default/address.rb', line 299

def longitude
  ((rand * 360) - 180).to_f
end

.mail_boxString

Produces a mail box number.

Examples:

Faker::Address.mail_box #=> "PO Box 123"

Returns:

  • (String)

Available since:

  • 2.9.1



100
101
102
# File 'lib/faker/default/address.rb', line 100

def mail_box
  bothify(fetch('address.mail_box'))
end

.secondary_addressString

Produces a secondary address.

Examples:

Faker::Address.secondary_address #=> "Apt. 672"

Returns:

  • (String)

Available since:

  • 0.3.0



61
62
63
# File 'lib/faker/default/address.rb', line 61

def secondary_address
  bothify(fetch('address.secondary_address'))
end

.stateString

Produces the name of a state.

Examples:

Faker::Address.state #=> "California"

Returns:

  • (String)

Available since:

  • 0.3.0



204
205
206
# File 'lib/faker/default/address.rb', line 204

def state
  fetch('address.state')
end

.state_abbrString

Produces a state abbreviation.

Examples:

Faker::Address.state_abbr #=> "AP"

Returns:

  • (String)

Available since:

  • 0.3.0



191
192
193
# File 'lib/faker/default/address.rb', line 191

def state_abbr
  fetch('address.state_abbr')
end

.street_address(include_secondary: false) ⇒ String

Produces a street address.

Examples:

Faker::Address.street_address #=> "282 Kevin Brook"

Parameters:

  • include_secondary (Boolean) (defaults to: false)

    Whether or not to include the secondary address.

Returns:

  • (String)

Available since:

  • 0.3.0



48
49
50
# File 'lib/faker/default/address.rb', line 48

def street_address(include_secondary: false)
  numerify(parse('address.street_address') + (include_secondary ? " #{secondary_address}" : ''))
end

.street_nameString

Produces a street name.

Examples:

Faker::Address.street_name #=> "Larkin Fork"

Returns:

  • (String)

Available since:

  • 0.3.0



34
35
36
# File 'lib/faker/default/address.rb', line 34

def street_name
  parse('address.street_name')
end

.street_suffixString

Produces a street suffix.

Examples:

Faker::Address.street_suffix #=> "Street"

Returns:

  • (String)

Available since:

  • 0.3.0



152
153
154
# File 'lib/faker/default/address.rb', line 152

def street_suffix
  fetch('address.street_suffix')
end

.time_zoneString

Produces the name of a time zone.

Examples:

Faker::Address.time_zone #=> "Asia/Yakutsk"

Returns:

  • (String)

Available since:

  • 1.2.0



136
137
138
# File 'lib/faker/default/address.rb', line 136

def time_zone
  fetch('address.time_zone')
end

.zip_code(state_abbreviation: '') ⇒ String Also known as: zip, postcode

Produces a Zip Code.

Examples:

Faker::Address.zip_code #=> "58517"
Faker::Address.zip_code #=> "23285-4905"
Faker::Address.zip_code(state_abbreviation: 'CO') #=> "80011"

Parameters:

  • state_abbreviation (String) (defaults to: '')

    an abbreviation for a state where the zip code should be located.

Returns:

  • (String)

Available since:

  • 0.3.0



116
117
118
119
120
121
122
123
124
125
# File 'lib/faker/default/address.rb', line 116

def zip_code(state_abbreviation: '')
  if state_abbreviation.empty?
    letterified_string = letterify(fetch('address.postcode'))
    return numerify(letterified_string, leading_zero: true)
  end

  # provide a zip code that may be valid for the state provided
  # note: zip code may appear in the correct format for the state provided but may not be an actual state zip.
  bothify(fetch("address.postcode_by_state.#{state_abbreviation}"))
end