Class: SFCBus::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sfcbus/parser.rb

Class Method Summary collapse

Class Method Details

.parse(body) ⇒ Object

[View source]

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sfcbus/parser.rb', line 6

def self.parse(body)
  body =~ /callback\((.+)\);/
  json = JSON.parse($1)
  updated_at = parse_updated_at(json.pop)

  timetable = []
  json.each do |json2|
    json2.each_with_index do |buses, day_type|
      buses.each do |bus|
        timetable << SFCBus::Bus.new(
          :day_type  => parse_day_type(day_type),
          :from      => parse_location(bus['from']),
          :to        => parse_location(bus['to']),
          :hour      => bus['h'],
          :min       => bus['m'],
          :type      => parse_bus_type(bus['type']),
          :is_rotary => parse_is_rotary(bus['type'])
        )
      end
    end
  end
  {:timetable => timetable, :updated_at => updated_at}
end

.parse_bus_type(str) ⇒ Object

[View source]

43
44
45
46
47
48
49
50
51
# File 'lib/sfcbus/parser.rb', line 43

def self.parse_bus_type(str)
  char = str.sub('r', '')
  return SFCBus::Types::Normal if char == ''
  {
    't' => SFCBus::Types::Twinliner,
    's' => SFCBus::Types::Sasakubo,
    'n' => SFCBus::Types::Night
  }[char]
end

.parse_day_type(num) ⇒ Object

[View source]

35
36
37
38
39
40
41
# File 'lib/sfcbus/parser.rb', line 35

def self.parse_day_type(num)
  [
    SFCBus::DayTypes::Weekday,
    SFCBus::DayTypes::Saturday,
    SFCBus::DayTypes::Holiday
  ][num]
end

.parse_is_rotary(str) ⇒ Object

[View source]

61
62
63
# File 'lib/sfcbus/parser.rb', line 61

def self.parse_is_rotary(str)
  str.include?('r')
end

.parse_location(str) ⇒ Object

[View source]

53
54
55
56
57
58
59
# File 'lib/sfcbus/parser.rb', line 53

def self.parse_location(str)
  {
    'sfc'  => SFCBus::Locations::SFC,
    'sho'  => SFCBus::Locations::Shonandai,
    'tuji' => SFCBus::Locations::Tsujido
  }[str]
end

.parse_updated_at(str) ⇒ Object

[View source]

30
31
32
33
# File 'lib/sfcbus/parser.rb', line 30

def self.parse_updated_at(str)
  year, month, day = str.split('.').map(&:to_i)
  Date.new(year, month, day)
end