barclays_bikes 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Harry Marr
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # barclays_bikes - A Ruby Library for Retreiving Barclays Bike Availability
2
+
3
+ This may be used as a library or as a command line tool.
4
+
5
+ ### Command Line Usage
6
+
7
+ Run the `bbikes` command in a shell, optionally providing a query to filter
8
+ the station that are displayed.
9
+
10
+ ```console
11
+ $ bbikes hatton wall
12
+
13
+ Hatton Wall, Holborn:
14
+ 14 bikes available
15
+ 11 spaces available
16
+
17
+ ```
18
+
19
+ ### Using from Ruby
20
+
21
+ Simply require `barclays_bikes`, then use `BarclaysBikes.load_bike_data` to
22
+ fetch the availability information.
23
+
24
+ ```ruby
25
+ require 'barclays_bikes'
26
+
27
+ bike_data = BarclaysBikes.load_bike_data
28
+ bike_data['Hatton Wall, Holborn']
29
+ # => {:bikes=>13, :spaces=>12}
30
+ ```
31
+
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'barclays_bikes'
4
+
5
+ if (ARGV & %w(-h --help)).empty?
6
+ query = ARGV.join(' ')
7
+ BarclaysBikes.print_bike_info(query)
8
+ else
9
+ puts "usage: bbikes <query>"
10
+ end
11
+
12
+ # vim: set ft=ruby
13
+
@@ -0,0 +1,50 @@
1
+ require 'open-uri'
2
+ require "barclays_bikes/version"
3
+
4
+ module BarclaysBikes
5
+ URL = 'https://web.barclayscyclehire.tfl.gov.uk/maps'
6
+
7
+ # Scrape the bike availability data from the Barclays Bike website and return
8
+ # a hash mapping station names to bike and space counts.
9
+ def self.load_bike_data
10
+ data = open(URL) { |io| io.read }
11
+ stations = Hash[data.scan(/station={([^}]+)}/).map do |match|
12
+ [js_val(match.first, :name), {
13
+ bikes: js_val(match.first, :nbBikes).to_i,
14
+ spaces: js_val(match.first, :nbEmptyDocks).to_i,
15
+ }]
16
+ end]
17
+ end
18
+
19
+ # Display a summary of bike and space availability for a given query.
20
+ def self.print_bike_info(query)
21
+ bike_data = load_bike_data
22
+ query = query.split.map { |term| Regexp.escape(term) }.join("[^\w]+")
23
+ query = Regexp.new(query, Regexp::IGNORECASE)
24
+ matches = bike_data.select { |station_name| station_name =~ query }
25
+
26
+ matches.each_pair do |name, info|
27
+ puts "\n\033[1m#{name}\033[0m:"
28
+ puts " #{colorize("#{info[:bikes]} bikes available", info[:bikes])}"
29
+ puts " #{colorize("#{info[:spaces]} spaces available", info[:spaces])}"
30
+ end
31
+ puts matches.empty? ? "No matches found" : ""
32
+ end
33
+
34
+ private
35
+
36
+ # Extract a value from a Javascript (not JSON) object literal.
37
+ def self.js_val(js, prop)
38
+ /#{prop}:\s*"([^"]+)"/.match(js)[1]
39
+ end
40
+
41
+ # Colorize the text appropriately for the given number. If the number is 5 or
42
+ # greater, the text will be green. It'll be red for 0, and yellow for
43
+ # anything inbetween.
44
+ def self.colorize(text, n)
45
+ color_code = n > 0 ? 32 : 31
46
+ color_code = 33 if (1...5).include? n
47
+ "\033[#{color_code}m#{text}\033[0m"
48
+ end
49
+ end
50
+
@@ -0,0 +1,3 @@
1
+ module BarclaysBikes
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barclays_bikes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Harry Marr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - harry@hmarr.com
17
+ executables:
18
+ - bbikes
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - LICENSE
23
+ - README.md
24
+ - bin/bbikes
25
+ - lib/barclays_bikes.rb
26
+ - lib/barclays_bikes/version.rb
27
+ homepage: https://github.com/hmarr/barclays_bikes
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.15
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: A Ruby library for retreiving Barclays Bike availability
51
+ test_files: []