class Kamome::Transformations::GeneralCsv

Transform a GENERAL csv row to a Hash or others

Constants

VALID_ROW_SIZE

Public Instance Methods

generate_detail_model(row) click to toggle source
# File lib/kamome/transformations/general_csv.rb, line 20
def generate_detail_model(row)
  attributes = generate_hash(row)
  ::Kamome::Models::General.new(attributes)
end
generate_hash(row) click to toggle source

Generate hash from a general csv row @return [Hash]

# File lib/kamome/transformations/general_csv.rb, line 15
def generate_hash(row)
  validate_row!(row)
  transform(row)
end
generate_model(row) click to toggle source
# File lib/kamome/transformations/general_csv.rb, line 25
def generate_model(row)
  attributes = extract(generate_hash(row))
  ::Kamome::Models::Address.new(attributes)
end

Private Instance Methods

extract(attributes) click to toggle source
# File lib/kamome/transformations/general_csv.rb, line 59
def extract(attributes)
  {
    code: attributes[:code],
    zipcode: attributes[:zipcode],
    prefecture: attributes[:prefecture],
    city: attributes[:city],
    town: attributes[:town],
    street: '',
    company_name: '',
    post_office_box: '',
    state: attributes[:state],
    ambiguous_town: attributes[:ambiguous_town]
  }
end
transform(row) click to toggle source
# File lib/kamome/transformations/general_csv.rb, line 40
def transform(row) # rubocop:disable Metrics/AbcSize
  {
    code: row[0].to_s,
    zipcode: row[2].to_s,
    prefecture_kana: row[3].to_s,
    city_kana: row[4].to_s,
    town_kana: clean_town_kana(row[5].to_s),
    prefecture: row[6].to_s,
    city: row[7].to_s,
    town: clean_town(row[8].to_s),
    ambiguous_town: row[9].to_i == 1,
    ambiguous_street: row[10].to_i == 1,
    required_chome: row[11].to_i == 1,
    ambiguous_zipcode: row[12].to_i == 1,
    state: row[13].to_i,
    reason: row[14].to_i
  }
end
validate_row!(row) click to toggle source
# File lib/kamome/transformations/general_csv.rb, line 34
def validate_row!(row)
  return if row&.size == VALID_ROW_SIZE

  raise ArgumentError, "Wrong row size (expected #{VALID_ROW_SIZE}, got #{row&.size})"
end