Class: JapanETC::DatabaseProvider::BaseNEXCO

Inherits:
Base
  • Object
show all
Includes:
Util
Defined in:
lib/japan_etc/database_provider/base_nexco.rb

Direct Known Subclasses

NEXCOCentral, NEXCOEast, NEXCOWest

Constant Summary collapse

WHITESPACE =
/[\s ]/.freeze
ROAD_NAME_PATTERN =
/
  (?<road_name>
    [^#{WHITESPACE}\d(【]
    (?:
      (?:
        (?<!連絡道)
        #{WHITESPACE}
      )?
      [^#{WHITESPACE}]
    )*
  )
/x.freeze
TOLLBOOTH_LINE_PATTERN =
/
  \A
  (?:
    (?:
      #{WHITESPACE}{,10}#{ROAD_NAME_PATTERN}#{WHITESPACE}+
      |
      #{WHITESPACE}{,10}(?:[(【][^#{WHITESPACE}]+)#{WHITESPACE}+ # Obsolete road name
      |
      #{WHITESPACE}{10,}
    )
    (?:
      (?<tollbooth_name>[^#{WHITESPACE}\d(【](?:#{WHITESPACE}?[^#{WHITESPACE}])*)
      #{WHITESPACE}+
    )
    |
    #{WHITESPACE}{10,}
  )
  (?<identifiers>\d{2}#{WHITESPACE}+\d{3}\b.*?)
  (?:
    ※
    (?<note>.+?)
    #{WHITESPACE}*
  )?
  \z
/x.freeze
IDENTIFIER_PATTERN =
/\b(\d{2})#{WHITESPACE}+(\d{3})\b/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

convert_fullwidth_characters_to_halfwidth, convert_to_integer, normalize, remove_whitespaces

Methods inherited from Base

#source_id, #source_url

Instance Attribute Details

#current_road_nameObject (readonly)

Returns the value of attribute current_road_name.



57
58
59
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 57

def current_road_name
  @current_road_name
end

#current_route_nameObject (readonly)

Returns the value of attribute current_route_name.



57
58
59
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 57

def current_route_name
  @current_route_name
end

#current_tollbooth_nameObject (readonly)

Returns the value of attribute current_tollbooth_name.



57
58
59
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 57

def current_tollbooth_name
  @current_tollbooth_name
end

Instance Method Details

#canonicalize(road_name) ⇒ Object



105
106
107
108
109
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 105

def canonicalize(road_name)
  road_name = '首都圏中央連絡自動車道' if road_name == '首都圏中央連絡道'
  road_name = road_name.sub(/高速\z/, '高速道路')
  road_name
end

#extract_route_name_from_road_name(road_name) ⇒ Object



98
99
100
101
102
103
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 98

def extract_route_name_from_road_name(road_name)
  road_name = normalize(road_name)
  match = road_name.match(/\A(?<road_name>.+?)(?<route_name>\d+号.+)?\z/)
  road_name = match[:road_name].sub(/高速\z/, '高速道路')
  [road_name, match[:route_name]]
end

#fetch_tollboothsObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 59

def fetch_tollbooths
  tollbooths = []

  lines.each do |line|
    break if line.include?('【更新リスト】')

    tollbooths << parse_line(line)
  end

  tollbooths.flatten.compact
end

#linesObject



111
112
113
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 111

def lines
  pdf.pages.flat_map { |page| page.text.each_line.map(&:chomp).to_a }
end

#parse_line(line) ⇒ Object



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
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 71

def parse_line(line)
  match = line.match(TOLLBOOTH_LINE_PATTERN)
  return unless match

  if match[:road_name]
    road_name = remove_whitespaces(normalize(match[:road_name]))
    @current_road_name, @current_route_name = extract_route_name_from_road_name(road_name)
    @current_road_name = canonicalize(@current_road_name)
  end

  @current_tollbooth_name = match[:tollbooth_name] if match[:tollbooth_name]

  identifiers = match[:identifiers].scan(IDENTIFIER_PATTERN)

  identifiers.map do |identifier|
    Tollbooth.create(
      road_number: identifier.first,
      tollbooth_number: identifier.last,
      road_name: current_road_name,
      route_name: current_route_name,
      name: current_tollbooth_name,
      note: match[:note],
      source: source_id
    )
  end
end

#pdfObject



115
116
117
118
# File 'lib/japan_etc/database_provider/base_nexco.rb', line 115

def pdf
  response = Faraday.get(source_url)
  PDF::Reader.new(StringIO.new(response.body))
end