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, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, 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


82
83
84
# File 'lib/faker/default/address.rb', line 82

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

.city(legacy_options = NOT_GIVEN, 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
24
25
26
27
# File 'lib/faker/default/address.rb', line 21

def city(legacy_options = NOT_GIVEN, options: {})
  warn_for_deprecated_arguments do |keywords|
    keywords << :options if legacy_options != NOT_GIVEN
  end

  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


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

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


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

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


95
96
97
# File 'lib/faker/default/address.rb', line 95

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


229
230
231
# File 'lib/faker/default/address.rb', line 229

def country
  fetch('address.country')
end

.country_by_code(legacy_code = NOT_GIVEN, 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


245
246
247
248
249
250
251
# File 'lib/faker/default/address.rb', line 245

def country_by_code(legacy_code = NOT_GIVEN, code: 'US')
  warn_for_deprecated_arguments do |keywords|
    keywords << :code if legacy_code != NOT_GIVEN
  end

  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


280
281
282
# File 'lib/faker/default/address.rb', line 280

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


293
294
295
# File 'lib/faker/default/address.rb', line 293

def country_code_long
  fetch('address.country_code_long')
end

.country_name_to_code(legacy_name = NOT_GIVEN, 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


263
264
265
266
267
268
269
# File 'lib/faker/default/address.rb', line 263

def country_name_to_code(legacy_name = NOT_GIVEN, name: 'united_states')
  warn_for_deprecated_arguments do |keywords|
    keywords << :name if legacy_name != NOT_GIVEN
  end

  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


333
334
335
# File 'lib/faker/default/address.rb', line 333

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


359
360
361
362
363
364
365
# File 'lib/faker/default/address.rb', line 359

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


306
307
308
# File 'lib/faker/default/address.rb', line 306

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


319
320
321
# File 'lib/faker/default/address.rb', line 319

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


108
109
110
# File 'lib/faker/default/address.rb', line 108

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


69
70
71
# File 'lib/faker/default/address.rb', line 69

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


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

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


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

def state_abbr
  fetch('address.state_abbr')
end

.street_address(legacy_include_secondary = NOT_GIVEN, 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


52
53
54
55
56
57
58
# File 'lib/faker/default/address.rb', line 52

def street_address(legacy_include_secondary = NOT_GIVEN, include_secondary: false)
  warn_for_deprecated_arguments do |keywords|
    keywords << :include_secondary if legacy_include_secondary != NOT_GIVEN
  end

  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


38
39
40
# File 'lib/faker/default/address.rb', line 38

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


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

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


148
149
150
# File 'lib/faker/default/address.rb', line 148

def time_zone
  fetch('address.time_zone')
end

.zip_code(legacy_state_abbreviation = NOT_GIVEN, 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


124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/faker/default/address.rb', line 124

def zip_code(legacy_state_abbreviation = NOT_GIVEN, state_abbreviation: '')
  warn_for_deprecated_arguments do |keywords|
    keywords << :state_abbreviation if legacy_state_abbreviation != NOT_GIVEN
  end

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

  # provide a zip code that is valid for the state provided
  # see http://www.fincen.gov/forms/files/us_state_territory_zip_codes.pdf
  bothify(fetch('address.postcode_by_state.' + state_abbreviation))
end