Class: JapanETC::DatabaseProvider::MetropolitanExpressway

Inherits:
Base
  • Object
show all
Defined in:
lib/japan_etc/database_provider/metropolitan_expressway.rb

Overview

Constant Summary collapse

OPPOSITE_DIRECTIONS =
{
  '上' => '下',
  '下' => '上',
  '外' => '内',
  '内' => '外',
  '東' => '西',
  '西' => '東'
}.freeze
DIRECTION_SUFFIX_PATTERN =
/[#{OPPOSITE_DIRECTIONS.keys.join('')}]\z/.freeze

Instance Method Summary collapse

Methods inherited from Base

#source_id

Instance Method Details

#csvObject



74
75
76
# File 'lib/japan_etc/database_provider/metropolitan_expressway.rb', line 74

def csv
  shiftjis_csv.encode(Encoding::UTF_8)
end

#extract_direction_from_name!(tollbooth) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/japan_etc/database_provider/metropolitan_expressway.rb', line 35

def extract_direction_from_name!(tollbooth)
  match = tollbooth.name.match(DIRECTION_SUFFIX_PATTERN)

  return unless match

  direction = match.to_s

  return if %w[東 西].include?(direction) && tollbooth.road.route_name != '湾岸線'

  opposite_name = tollbooth.name.sub(DIRECTION_SUFFIX_PATTERN, OPPOSITE_DIRECTIONS[direction])

  opposite_tollbooth_exists = original_tollbooths.find do |other_tollbooth|
    other_tollbooth.road == tollbooth.road && other_tollbooth.name == opposite_name
  end

  return unless opposite_tollbooth_exists

  tollbooth.direction = Direction.from(direction)
  tollbooth.name.sub!(DIRECTION_SUFFIX_PATTERN, '')
end

#fetch_tollboothsObject



27
28
29
30
31
32
33
# File 'lib/japan_etc/database_provider/metropolitan_expressway.rb', line 27

def fetch_tollbooths
  original_tollbooths.map do |original_tollbooth|
    tollbooth = original_tollbooth.dup
    extract_direction_from_name!(tollbooth)
    tollbooth
  end
end

#original_tollboothsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/japan_etc/database_provider/metropolitan_expressway.rb', line 56

def original_tollbooths
  @original_tollbooths ||= rows.map do |row|
    Tollbooth.create(
      road_number: row[0],
      tollbooth_number: row[1],
      road_name: '首都高速道路',
      route_name: row[2],
      name: row[3],
      entrance_or_exit: EntranceOrExit.from(row[4]),
      source: source_id
    )
  end
end

#rowsObject



70
71
72
# File 'lib/japan_etc/database_provider/metropolitan_expressway.rb', line 70

def rows
  CSV.parse(csv, headers: :first_row)
end

#shiftjis_csvObject



78
79
80
81
# File 'lib/japan_etc/database_provider/metropolitan_expressway.rb', line 78

def shiftjis_csv
  response = Faraday.get(source_url)
  response.body.force_encoding(Encoding::Shift_JIS)
end

#source_urlObject



12
13
14
# File 'lib/japan_etc/database_provider/metropolitan_expressway.rb', line 12

def source_url
  'https://www.shutoko.jp/fee/tollbooth/~/media/pdf/customer/fee/tollbooth/code200322_.csv/'
end