File: address_us.rb

package info (click to toggle)
ruby-ffaker 2.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,776 kB
  • sloc: ruby: 12,788; makefile: 8; sh: 1
file content (58 lines) | stat: -rw-r--r-- 1,206 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
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
58
# frozen_string_literal: true

module FFaker
  module AddressUS
    include FFaker::Address

    extend ModuleUtils
    extend self

    ZIP_FORMATS = ['#####', '#####-####'].freeze
    CONTINENTAL_STATE = (STATE - %w[Hawaii Alaska])
    CONTINENTAL_STATE_ABBR = (STATE_ABBR - %w[HI AK])

    def zip_code
      FFaker.numerify(fetch_sample(ZIP_FORMATS))
    end

    def state
      fetch_sample(STATE)
    end

    def state_abbr(st_name = nil)
      return find_abbr(state) unless st_name

      st_name = capitalize_all_words(st_name)
      check_state_existence(st_name)
      find_abbr(st_name)
    end

    def state_and_territories_abbr
      fetch_sample(STATE_AND_TERRITORIES_ABBR)
    end

    def continental_state
      fetch_sample(CONTINENTAL_STATE)
    end

    def continental_state_abbr
      fetch_sample(CONTINENTAL_STATE_ABBR)
    end

    private

    def check_state_existence(state_name)
      return if STATE.include?(state_name)

      raise ArgumentError, "Unexpected state name: '#{state_name}'"
    end

    def find_abbr(state)
      STATE_ABBR[STATE.index(state)]
    end

    def capitalize_all_words(string)
      string.split.map(&:capitalize).join(' ')
    end
  end
end