Module: OCTranspo::Stop

Extended by:
Stop
Included in:
Stop
Defined in:
lib/octranspo/stop.rb

Instance Method Summary collapse

Instance Method Details

#allObject



7
8
9
# File 'lib/octranspo/stop.rb', line 7

def all
  @stops ||= YAML.load_file(storage)
end

#find_all_by_name(name) ⇒ Object



38
39
40
41
42
# File 'lib/octranspo/stop.rb', line 38

def find_all_by_name (name)
  all.select do |stop|
    stop[:name] == name
  end
end

#find_all_by_number(number) ⇒ Object



25
26
27
28
29
30
# File 'lib/octranspo/stop.rb', line 25

def find_all_by_number (number)
  number = number.to_s
  all.select do |stop|
    stop[:number] == number
  end
end

#find_all_matching(pattern) ⇒ Object



44
45
46
47
48
# File 'lib/octranspo/stop.rb', line 44

def find_all_matching (pattern)
  all.select do |stop|
    pattern.match(stop[:name])
  end
end

#find_by_name(name) ⇒ Object



32
33
34
35
36
# File 'lib/octranspo/stop.rb', line 32

def find_by_name (name)
  all.detect do |stop|
    stop[:name] == name
  end
end

#find_by_number(number) ⇒ Object



18
19
20
21
22
23
# File 'lib/octranspo/stop.rb', line 18

def find_by_number (number)
  number = number.to_s
  all.detect do |stop|
    stop[:number] == number
  end
end

#find_by_stop_id(stop_id) ⇒ Object



11
12
13
14
15
16
# File 'lib/octranspo/stop.rb', line 11

def find_by_stop_id (stop_id)
  stop_id = stop_id.to_s
  all.detect do |stop|
    stop[:stop_id] == stop_id
  end
end

#storageObject



65
66
67
# File 'lib/octranspo/stop.rb', line 65

def storage
  "#{File.dirname(__FILE__)}/stops.yml"
end

#updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/octranspo/stop.rb', line 50

def update
  url = "http://www.octranspo1.com/google/retrieve_stops?zoom=15"
  doc = Hpricot.XML(open(url).read)
  new_stops = (doc / :marker).map do |marker|
    attributes = {}.merge(marker.attributes).symbolize_keys
    attributes[:stop_id] = attributes.delete(:stopid)
    attributes[:number]  = attributes.delete(:id).rjust(4, "0")
    attributes[:number]  = nil if attributes[:number] == "0000"
    attributes        
  end
  File.open(storage, 'w') { |f| f << YAML.dump(new_stops) }
  @stops = nil
  return all
end