Class: SFCBus::Client

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

Instance Method Summary collapse

Instance Method Details

#fetchObject



8
9
10
11
12
13
14
15
# File 'lib/sfcbus/client.rb', line 8

def fetch
  http = Net::HTTP.new('hack.sfc.keioac.jp')
  res  = http.get('/sfcbusapi/index.php')
  res.value
  parsed = SFCBus::Parser.parse(res.body)
  @updated_at = parsed[:updated_at]
  @timetable  = parsed[:timetable]
end

#next_list(opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sfcbus/client.rb', line 39

def next_list(opts = {})
  if opts[:from].nil? and opts[:to].nil?
    route = [SFCBus::Locations::SFC, SFCBus::Locations::Shonandai]
    opts[:from], opts[:to] = SFCBus::Utils.at_sfc? ? route : route.reverse
  end
  opts[:day_type] ||= SFCBus::Utils.today_day_type
  opts[:limit]    ||= 5

  now = Time.now
  select(opts).select { |b|
    (now.hour < b.hour) or (now.hour == b.hour and now.min < b.min)
  }.take(opts[:limit])
end

#select(opts = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sfcbus/client.rb', line 27

def select(opts = {})
  SFCBus::Validator.validate(opts)

  list = timetable
  list.select! { |bus| bus.from     == opts[:from]      } unless opts[:from].nil? 
  list.select! { |bus| bus.to       == opts[:to]        } unless opts[:to].nil? 
  list.select! { |bus| bus.type     == opts[:type]      } unless opts[:type].nil? 
  list.select! { |bus| bus.day_type == opts[:day_type]  } unless opts[:day_type].nil? 
  list.select! { |bus| bus.rotary?  == opts[:is_rotary] } unless opts[:is_rotary].nil? 
  list
end

#timetableObject



17
18
19
20
# File 'lib/sfcbus/client.rb', line 17

def timetable
  fetch if @timetable.nil?
  @timetable
end

#updated_atObject



22
23
24
25
# File 'lib/sfcbus/client.rb', line 22

def updated_at
  fetch if @updated_at.nil?
  @updated_at
end