Class: Datasets::PostalCodeJapan

Inherits:
Dataset
  • Object
show all
Defined in:
lib/datasets/postal-code-japan.rb

Defined Under Namespace

Classes: Record

Constant Summary collapse

VALID_READINGS =
[
  :lowercase,
  :uppercase,
  :romaji,
]

Instance Attribute Summary

Attributes inherited from Dataset

#metadata

Instance Method Summary collapse

Methods inherited from Dataset

#clear_cache!, #to_table

Constructor Details

#initialize(reading: :lowercase) ⇒ PostalCodeJapan

Returns a new instance of PostalCodeJapan.


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/datasets/postal-code-japan.rb', line 40

def initialize(reading: :lowercase)
  super()
  @reading = reading
  unless VALID_READINGS.include?(@reading)
    message = ":reading must be one of ["
    message << VALID_READINGS.collect(&:inspect).join(", ")
    message << "]: #{@reading.inspect}"
    raise ArgumentError, message
  end
  @metadata.id = "postal-code-japan-#{@reading}"
  @metadata.name = "Postal code in Japan (#{@reading})"
  @metadata.url = "https://www.post.japanpost.jp/zipcode/download.html"
  @metadata.licenses = [
    "CC0-1.0",
  ]
  @metadata.description = "Postal code in Japan (reading: #{@reading})"
end

Instance Method Details

#each(&block) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/datasets/postal-code-japan.rb', line 58

def each(&block)
  return to_enum(__method__) unless block_given?

  open_data do |input|
    utf8_data = input.read.encode(Encoding::UTF_8, Encoding::CP932)
    options = {
      quote_char: nil,
      strip: %Q["],
    }
    if @reading == :romaji
      CSV.parse(utf8_data, **options) do |row|
        yield(Record.new(nil,
                         nil,
                         row[0],
                         row[4],
                         row[5],
                         row[6],
                         row[1],
                         row[2],
                         row[3],
                         false,
                         false,
                         false,
                         false,
                         false,
                         nil))
      end
    else
      CSV.parse(utf8_data, **options) do |row|
        yield(Record.new(row[0],
                         row[1].rstrip,
                         row[2],
                         row[3],
                         row[4],
                         row[5],
                         row[6],
                         row[7],
                         row[8],
                         (row[9] == "1"),
                         (row[10] == "1"),
                         (row[11] == "1"),
                         (row[12] == "1"),
                         (row[13] != "0"),
                         convert_change_reason(row[14])))
      end
    end
  end
end