Module: BarclaysBikes

Defined in:
lib/barclays_bikes.rb,
lib/barclays_bikes/version.rb

Constant Summary collapse

URL =
'https://web.barclayscyclehire.tfl.gov.uk/maps'
VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.load_bike_dataObject

Scrape the bike availability data from the Barclays Bike website and return a hash mapping station names to bike and space counts.



9
10
11
12
13
14
15
16
17
# File 'lib/barclays_bikes.rb', line 9

def self.load_bike_data
  data = open(URL) { |io| io.read }
  stations = Hash[data.scan(/station={([^}]+)}/).map do |match|
    [js_val(match.first, :name), {
      bikes: js_val(match.first, :nbBikes).to_i,
      spaces: js_val(match.first, :nbEmptyDocks).to_i,
    }]
  end]
end

Display a summary of bike and space availability for a given query.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/barclays_bikes.rb', line 20

def self.print_bike_info(query)
  bike_data = load_bike_data
  query = query.split.map { |term| Regexp.escape(term) }.join("[^\w]+")
  query = Regexp.new(query, Regexp::IGNORECASE)
  matches = bike_data.select { |station_name| station_name =~ query }

  matches.each_pair do |name, info|
    puts "\n\033[1m#{name}\033[0m:"
    puts "  #{colorize("#{info[:bikes]} bikes available", info[:bikes])}"
    puts "  #{colorize("#{info[:spaces]} spaces available", info[:spaces])}"
  end
  puts matches.empty? ? "No matches found" : ""
end