london-bike-hire-cli 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +14 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +11 -0
  8. data/LICENSE +21 -0
  9. data/README.md +144 -0
  10. data/Rakefile +19 -0
  11. data/bin/lbh +5 -0
  12. data/boris-on-a-bike_med.jpg +0 -0
  13. data/lib/london_bike_hire_cli.rb +15 -0
  14. data/lib/london_bike_hire_cli/application.rb +71 -0
  15. data/lib/london_bike_hire_cli/basic_renderer.rb +38 -0
  16. data/lib/london_bike_hire_cli/controller.rb +82 -0
  17. data/lib/london_bike_hire_cli/feed_parser.rb +76 -0
  18. data/lib/london_bike_hire_cli/geocoding_adapter.rb +27 -0
  19. data/lib/london_bike_hire_cli/query_response.rb +22 -0
  20. data/lib/london_bike_hire_cli/spatial_search.rb +20 -0
  21. data/lib/london_bike_hire_cli/station.rb +16 -0
  22. data/lib/london_bike_hire_cli/station_adapter.rb +19 -0
  23. data/lib/london_bike_hire_cli/station_repository.rb +51 -0
  24. data/lib/london_bike_hire_cli/version.rb +3 -0
  25. data/london-bike-hire-cli.gemspec +37 -0
  26. data/spec/controller_spec.rb +186 -0
  27. data/spec/feed_parser_spec.rb +42 -0
  28. data/spec/fixtures/feed_xml.yml +822 -0
  29. data/spec/fixtures/n19ae_geocode.yml +114 -0
  30. data/spec/fixtures/no_results_geocode.yml +50 -0
  31. data/spec/geocoding_adapter_spec.rb +47 -0
  32. data/spec/spatial_search_spec.rb +27 -0
  33. data/spec/spec_helper.rb +83 -0
  34. data/spec/station_adapter_spec.rb +29 -0
  35. data/spec/station_respository_spec.rb +117 -0
  36. data/spec/station_spec.rb +28 -0
  37. data/spec/support/test_datasource.rb +24 -0
  38. data/spec/support/vcr.rb +7 -0
  39. data/test.rb +6 -0
  40. metadata +307 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a31585a5f453c569a80dc06e07b7c2ff6fbda20f
4
+ data.tar.gz: 94dab876d1d9638e36f5d8415246ef0b70dcad0d
5
+ SHA512:
6
+ metadata.gz: d7b7c87317e34fc58ccbb8e867ffa553662245f07e6c26f2680dfbbc0d471a7db7106d099fd9a7b1ca08206bf3d0c934195874ef7bf59fe2f89d11eae9ddf241
7
+ data.tar.gz: d954e6c03befdd669094c432ef7a5c1131104f4d88b8526a329c8f6b98dd6ccae1cfcf1c556ecb4453203a77143a14206b6f3bfc0dd3b2016a0963aa676cd400
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+ /vendor/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.1
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - ruby-head
7
+
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
11
+
12
+ addons:
13
+ code_climate:
14
+ repo_token: a69242268fba4d9eb660619cd6ed8669ca8c9ae4b55311660b8af822e9187178
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in london-bike-hire-cli.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'rspec', cmd: 'rspec --color --format doc' do
2
+ # watch /lib/ files
3
+ watch(%r{^lib/(.+)\.rb$}) do |m|
4
+ "spec/#{m[1]}_spec.rb"
5
+ end
6
+
7
+ # watch /spec/ files
8
+ watch(%r{^spec/(.+)\.rb$}) do |m|
9
+ "spec/#{m[1]}.rb"
10
+ end
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Robert Murray
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ ## London Bike Hire CLI
2
+
3
+ #### A simple command line interface to London's Bike Hire API.
4
+
5
+ [![Build Status](https://travis-ci.org/rob-murray/london-bike-hire-cli.svg?branch=master)](https://travis-ci.org/rob-murray/london-bike-hire-cli)
6
+ [![Code Climate](https://codeclimate.com/github/rob-murray/london-bike-hire-cli.png)](https://codeclimate.com/github/rob-murray/london-bike-hire-cli)
7
+ [![Coverage Status](https://coveralls.io/repos/rob-murray/london-bike-hire-cli/badge.png)](https://coveralls.io/r/rob-murray/london-bike-hire-cli)
8
+ [![Dependency Status](https://gemnasium.com/rob-murray/ferver.svg)](https://gemnasium.com/rob-murray/london-bike-hire-cli)
9
+ [![Gem Version](https://badge.fury.io/rb/london-bike-hire-cli.svg)](http://badge.fury.io/rb/london-bike-hire-cli)
10
+ [![Haz Commitz Status](http://haz-commitz.herokuapp.com/repos/rob-murray/london-bike-hire-cli.svg)](http://haz-commitz.herokuapp.com/repos/rob-murray/london-bike-hire-cli)
11
+
12
+
13
+ ### Description
14
+
15
+ This gem provides a command line interface to find information about London's Bike Hire stations, it allows you to find stations by name or their ID and retrieve information about the status of a station.
16
+
17
+ ![Boris](boris-on-a-bike_med.jpg?raw=true "Boris Johnson on a bike")
18
+
19
+ Here's the spec for **London Bike Hire CLI**:
20
+
21
+ * Find all stations
22
+ * Find nearest station
23
+ * Find by ID
24
+ * Find by by name
25
+ * Display status of stations
26
+
27
+
28
+ ### Getting started
29
+
30
+ Using **london-bike-hire-cli** could not be simpler - just install the **london-bike-hire-cli** gem.
31
+
32
+ ```bash
33
+ $ gem install london-bike-hire-cli
34
+ ```
35
+
36
+
37
+ ### Usage
38
+
39
+ You can run **london-bike-hire-cli** from the command line with just the `lbh` command.
40
+
41
+ ##### Command line help
42
+
43
+ For a list of arguments just use the `-h` switch.
44
+
45
+ ```bash
46
+ $ lbh -h
47
+ ````
48
+
49
+ ##### List all bike stations
50
+
51
+ List all however-many-hundred bike stations there are. With pagination.
52
+
53
+ ```bash
54
+ $ lbh all
55
+ ````
56
+
57
+ ##### Find the nearest bike station
58
+
59
+ Find a the nearest bike station to the specified type below. This will return five stations.
60
+
61
+ Available types:
62
+
63
+ * `search` - Most search terms; such as placename, postcode
64
+ * `id` - An ID of another bike station
65
+
66
+ ```bash
67
+ $ lbh near --{type} {search_value}
68
+
69
+ # Near by search term
70
+ $ lbh near --search N19AE
71
+ $ lbh near --search 'waterloo station'
72
+
73
+ # Near another bike station id
74
+ $ lbh near --id 123
75
+ ```
76
+
77
+ **Note:** Don't forget to wrap search term in quotes if it contains spaces.
78
+
79
+ ##### Show status of a bike station by id
80
+
81
+ Find a Bike station by it's Tfl station ID. This will return one station.
82
+
83
+ ```bash
84
+ $ lbh find --id {id}
85
+ $ lbh find --id 439
86
+ ````
87
+
88
+ ##### Find all bike stations by name
89
+
90
+ Search all bike stations' name attribute for the search value. This will return zero or more results.
91
+
92
+ ```bash
93
+ $ lbh where --{attribute} {search_value}
94
+ $ lbh where --name kings
95
+ ````
96
+
97
+ ##### Results
98
+
99
+ ```bash
100
+ $ lbh find --id 439
101
+ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
102
+ Feed updated: 2014-08-12 08:45:01
103
+ >>> Dock
104
+ Id: 439
105
+ Name: Killick Street, Kings Cross
106
+ Lat: 51.5338
107
+ Long: -0.118677
108
+ Temporary: false
109
+ Bikes: 4
110
+ Docks_free: 18
111
+ Docks_total: 22
112
+ Link to map: https://www.google.co.uk/maps/preview/@51.5338,-0.118677,17z
113
+ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
114
+ ```
115
+
116
+ ### Contributing
117
+
118
+ 1. Fork it ( https://github.com/rob-murray/london-bike-hire-cli/fork )
119
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
120
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
121
+ 4. Push to the branch (`git push origin my-new-feature`)
122
+ 5. Create a new Pull Request
123
+
124
+ #### Development
125
+
126
+ Install dependancies
127
+
128
+ ```bash
129
+ $ bundle install --no-deployment
130
+ ```
131
+
132
+ Run test script as entry point to application
133
+
134
+ ```bash
135
+ $ ruby test.rb find --id 439
136
+ ```
137
+
138
+
139
+ ### License
140
+
141
+ This project is available for use under the MIT software license.
142
+ See LICENSE
143
+
144
+ The above image is copyright 2015 Getty Images - Getty Images for Santander.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+
5
+ task default: :spec
6
+ task test: :spec
7
+
8
+ desc 'Run all specs in spec directory (excluding plugin specs)'
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ desc 'Run application locally from source'
12
+ task :run, [:arg1, :arg2] do |_t, _args|
13
+ require 'rubygems'
14
+ require 'bundler'
15
+ Bundler.setup
16
+ require 'london-bike-hire-cli'
17
+
18
+ LondonBikeHireCli::Application.new.run
19
+ end
data/bin/lbh ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'london_bike_hire_cli'
4
+
5
+ LondonBikeHireCli::Application.new.run
Binary file
@@ -0,0 +1,15 @@
1
+ require_relative 'london_bike_hire_cli/application'
2
+ require_relative 'london_bike_hire_cli/basic_renderer'
3
+ require_relative 'london_bike_hire_cli/controller'
4
+ require_relative 'london_bike_hire_cli/feed_parser'
5
+ require_relative 'london_bike_hire_cli/geocoding_adapter'
6
+ require_relative 'london_bike_hire_cli/query_response'
7
+ require_relative 'london_bike_hire_cli/spatial_search'
8
+ require_relative 'london_bike_hire_cli/station'
9
+ require_relative 'london_bike_hire_cli/station_adapter'
10
+ require_relative 'london_bike_hire_cli/station_repository'
11
+ require_relative 'london_bike_hire_cli/version'
12
+
13
+ module LondonBikeHireCli
14
+ DEFAULT_SEARCH_LIMIT = 5
15
+ end
@@ -0,0 +1,71 @@
1
+ require 'commander'
2
+
3
+ module LondonBikeHireCli
4
+ class Application
5
+ include Commander::Methods
6
+
7
+ def run
8
+ program :name, 'London Bike Hire CLI'
9
+ program :version, LondonBikeHireCli::VERSION
10
+ program :description, 'A command line interface to the London Bike Hire API.'
11
+
12
+ command :find do |c|
13
+ c.option '--id ID', Integer, 'The Bike station ID'
14
+ c.syntax = 'find --id {id}'
15
+ c.description = 'Find by id. The id is the idenitifier given by TFL to each Bike Station.'
16
+ c.action do |_args, options|
17
+ controller.find_by_id(options.id)
18
+ end
19
+ end
20
+
21
+ command :where do |c|
22
+ c.option '--name NAME', String, 'The name attribute to search'
23
+ c.syntax = 'where --name {name}'
24
+ c.description = 'Search all stations by name, include road name or area.'
25
+ c.action do |_args, options|
26
+ enable_paging
27
+
28
+ controller.where(params: { name: options.name })
29
+ end
30
+ end
31
+
32
+ command :near do |c|
33
+ search_opt_message = 'Search term to base search around, can be a placename or postcode.'
34
+ id_opt_message = 'Search for stations near another station by its ID'
35
+ description = 'Search for the nearest stations to a point, either from a search term or bike station id.'
36
+
37
+ c.option '--search SEARCH_TERM', String, search_opt_message
38
+ c.option '--id ID', Integer, id_opt_message
39
+ c.syntax = 'near --search {search_term} --id {id}'
40
+ c.description = description
41
+ c.action do |_args, options|
42
+ enable_paging
43
+
44
+ controller.nearest(params: { search_term: options.search, id: options.id })
45
+ end
46
+ end
47
+
48
+ command :all do |c|
49
+ c.syntax = 'all'
50
+ c.description = 'List all stations.'
51
+ c.action do |_args, _options|
52
+ enable_paging
53
+
54
+ controller.all
55
+ end
56
+ end
57
+
58
+ run!
59
+ end
60
+
61
+ private
62
+
63
+ def controller
64
+ @controller ||= Controller.new(repository: station_repository)
65
+ end
66
+
67
+ def station_repository
68
+ @station_repository ||= StationRepository.new(FeedParser.new)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,38 @@
1
+ module LondonBikeHireCli
2
+ class BasicRenderer
3
+ def initialize(output = STDOUT)
4
+ @output = output
5
+ end
6
+
7
+ def render(context)
8
+ output.print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
9
+ output.print "Feed updated: #{context.time_of_feed}\n"
10
+
11
+ context.each do |station|
12
+ render_station(station)
13
+ end
14
+
15
+ output.print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
16
+ end
17
+
18
+ def render_error(context)
19
+ output.print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
20
+ output.print "Ooops, something went wrong with the query.\n"
21
+ output.print "The query #{context[:query].inspect}\n"
22
+ output.print "resulted in error: #{context[:error]}\n"
23
+ output.print "Try again?\n"
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :output
29
+
30
+ def render_station(station)
31
+ output.print ">>> Dock\n"
32
+ station.each_pair.each do |attr_name, attr_value|
33
+ output.print "#{attr_name.capitalize}: #{attr_value}\n"
34
+ end
35
+ output.print "Link to map: #{station.map_link}\n"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,82 @@
1
+ module LondonBikeHireCli
2
+ class Controller
3
+ attr_accessor :geocoder, :spatial_service
4
+
5
+ def initialize(repository: nil, renderer: default_renderer)
6
+ @repository = repository
7
+ @renderer = renderer
8
+ @geocoder = GeocodingAdapter.new
9
+ end
10
+
11
+ def all
12
+ results = repository.all
13
+ renderer.render(results)
14
+ end
15
+
16
+ def find_by_id(id)
17
+ results = repository.find_by_id(id.to_i)
18
+ renderer.render(results)
19
+
20
+ rescue StationRepository::StationNotFound => e
21
+ renderer.render_error(query: "find_by_id: #{id}", error: e)
22
+ end
23
+
24
+ def where(params: {})
25
+ results = repository.find_by_name(params[:name])
26
+ renderer.render(results)
27
+
28
+ rescue StationRepository::StationNotFound => e
29
+ renderer.render_error(query: "where: #{params}", error: e)
30
+ end
31
+
32
+ def nearest(params: {})
33
+ unless params[:search_term].nil? ^ params[:id].nil?
34
+ error = 'Please use either `search_term` or `id` not both.'
35
+ renderer.render_error(query: "nearest: #{params}", error: error)
36
+ return
37
+ end
38
+
39
+ if search_term = params[:search_term]
40
+ geocoded_point = geocoder.geocode(search_term)
41
+ end
42
+
43
+ if search_term = params[:id]
44
+ begin
45
+ stations = repository.find_by_id(search_term.to_i)
46
+ rescue StationRepository::StationNotFound => e
47
+ renderer.render_error(query: "where: #{params}", error: e)
48
+ return
49
+ end
50
+
51
+ geocoded_point = stations.first.position
52
+ end
53
+
54
+ unless geocoded_point
55
+ error = 'Unable to Geocode location.'
56
+ renderer.render_error(query: "nearest: #{params}", error: error)
57
+ return
58
+ end
59
+
60
+ init_spatial_service(repository.all)
61
+ nearest_ids = spatial_service.nearest(geocoded_point)
62
+
63
+ results = repository.find_by_id(*nearest_ids)
64
+ renderer.render(results)
65
+ end
66
+
67
+ private
68
+
69
+ attr_reader :repository, :renderer
70
+
71
+ def default_renderer
72
+ BasicRenderer.new
73
+ end
74
+
75
+ def init_spatial_service(all_stations)
76
+ @spatial_service ||= begin
77
+ datasource = StationAdapter.new(all_stations).to_triples
78
+ SpatialSearch.new(datasource)
79
+ end
80
+ end
81
+ end
82
+ end