Class: JpAddressComplement::KenAllDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/jp_address_complement/ken_all_downloader.rb

Overview

郵便番号データ(utf_ken_all.zip)を公式URLからダウンロードし、展開して CSV のパスを返す

Defined Under Namespace

Classes: DownloadError

Constant Summary collapse

DEFAULT_URL =
'https://www.post.japanpost.jp/zipcode/dl/utf/zip/utf_ken_all.zip'
CSV_FILENAME =
'utf_ken_all.csv'

Instance Method Summary collapse

Constructor Details

#initialize(url = DEFAULT_URL) ⇒ KenAllDownloader

Returns a new instance of KenAllDownloader.

Parameters:

  • url (String) (defaults to: DEFAULT_URL) —

    ダウンロードする zip の URL(省略時は DEFAULT_URL)



23
24
25
# File 'lib/jp_address_complement/ken_all_downloader.rb', line 23

def initialize(url = DEFAULT_URL)
  @url = url
end

Instance Method Details

#download_and_extract ⇒ String

zip をダウンロードして一時ディレクトリに展開し、KEN_ALL.CSV の絶対パスを返す。 呼び出し元でインポート完了まで一時ディレクトリは削除されない(プロセス終了時に OS が削除)。

Returns:

  • (String) —

    展開された KEN_ALL.CSV の絶対パス

Raises:

  • (DownloadError) —

    ダウンロード・展開・ファイルが見つからない場合



32
33
34
35
36
37
# File 'lib/jp_address_complement/ken_all_downloader.rb', line 32

def download_and_extract
  zip_path = download_zip
  extract_zip(zip_path)
ensure
  File.unlink(zip_path) if zip_path && File.exist?(zip_path)
end