freerider 0.0.4
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.
- checksums.yaml +7 -0
- data/.gitignore +34 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +94 -0
- data/LICENSE.txt +22 -0
- data/README.md +79 -0
- data/Rakefile +10 -0
- data/bin/freerider +63 -0
- data/build +15 -0
- data/freerider.gemspec +37 -0
- data/lib/formatter.rb +55 -0
- data/lib/freerider.rb +42 -0
- data/lib/freerider/version.rb +3 -0
- data/lib/geolocutor.rb +37 -0
- data/spec/formatter_spec.rb +37 -0
- data/spec/freerider_spec.rb +50 -0
- data/spec/geolocutor_spec.rb +58 -0
- data/spec/spec_helper.rb +4 -0
- metadata +222 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3061fdf8fe5483f8247586cc3f6dafaa42ddf734
|
4
|
+
data.tar.gz: a0a40bc31bc9d7578de11fdaebbe8c0a62760251
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4efcef6d3dc651253124bc22479e594a24506fbaf4fdfae68e76374349ea9111ed7695da774961be6a6886f7a9b4e3fca1cde07e43fcb8a273aca3e212749c0a
|
7
|
+
data.tar.gz: d29eaaac9fbb47b89813608d2dfab6cf96ee5921ede3ffa5156f0e70eb34d832e8cf351a4023ab68fd36c98c66708e6869f229b1de5239d631ca313f56fde96f
|
data/.gitignore
ADDED
@@ -0,0 +1,34 @@
|
|
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
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
# Gemfile.lock
|
30
|
+
# .ruby-version
|
31
|
+
# .ruby-gemset
|
32
|
+
|
33
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
+
.rvmrc
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
freerider (0.0.3)
|
5
|
+
caruby2go
|
6
|
+
geocoder
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
caruby2go (0.0.2)
|
12
|
+
thor (~> 0.19.1)
|
13
|
+
celluloid (0.16.0)
|
14
|
+
timers (~> 4.0.0)
|
15
|
+
coderay (1.1.0)
|
16
|
+
diff-lcs (1.2.5)
|
17
|
+
ffi (1.9.6)
|
18
|
+
formatador (0.2.5)
|
19
|
+
geocoder (1.2.8)
|
20
|
+
guard (2.11.1)
|
21
|
+
formatador (>= 0.2.4)
|
22
|
+
listen (~> 2.7)
|
23
|
+
lumberjack (~> 1.0)
|
24
|
+
nenv (~> 0.1)
|
25
|
+
notiffany (~> 0.0)
|
26
|
+
pry (>= 0.9.12)
|
27
|
+
shellany (~> 0.0)
|
28
|
+
thor (>= 0.18.1)
|
29
|
+
guard-compat (1.2.1)
|
30
|
+
guard-rspec (4.5.0)
|
31
|
+
guard (~> 2.1)
|
32
|
+
guard-compat (~> 1.1)
|
33
|
+
rspec (>= 2.99.0, < 4.0)
|
34
|
+
hitimes (1.2.2)
|
35
|
+
listen (2.8.5)
|
36
|
+
celluloid (>= 0.15.2)
|
37
|
+
rb-fsevent (>= 0.9.3)
|
38
|
+
rb-inotify (>= 0.9)
|
39
|
+
lumberjack (1.0.9)
|
40
|
+
method_source (0.8.2)
|
41
|
+
nenv (0.2.0)
|
42
|
+
notiffany (0.0.4)
|
43
|
+
nenv (~> 0.1)
|
44
|
+
shellany (~> 0.0)
|
45
|
+
pry (0.10.1)
|
46
|
+
coderay (~> 1.1.0)
|
47
|
+
method_source (~> 0.8.1)
|
48
|
+
slop (~> 3.4)
|
49
|
+
pry-nav (0.2.4)
|
50
|
+
pry (>= 0.9.10, < 0.11.0)
|
51
|
+
pry-remote (0.1.8)
|
52
|
+
pry (~> 0.9)
|
53
|
+
slop (~> 3.0)
|
54
|
+
rake (10.4.2)
|
55
|
+
rb-fsevent (0.9.4)
|
56
|
+
rb-inotify (0.9.5)
|
57
|
+
ffi (>= 0.5.0)
|
58
|
+
rspec (3.2.0)
|
59
|
+
rspec-core (~> 3.2.0)
|
60
|
+
rspec-expectations (~> 3.2.0)
|
61
|
+
rspec-mocks (~> 3.2.0)
|
62
|
+
rspec-core (3.2.0)
|
63
|
+
rspec-support (~> 3.2.0)
|
64
|
+
rspec-expectations (3.2.0)
|
65
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
66
|
+
rspec-support (~> 3.2.0)
|
67
|
+
rspec-mocks (3.2.0)
|
68
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
69
|
+
rspec-support (~> 3.2.0)
|
70
|
+
rspec-nc (0.2.0)
|
71
|
+
rspec (>= 2.9)
|
72
|
+
terminal-notifier (>= 1.4)
|
73
|
+
rspec-support (3.2.1)
|
74
|
+
shellany (0.0.1)
|
75
|
+
slop (3.6.0)
|
76
|
+
terminal-notifier (1.6.2)
|
77
|
+
thor (0.19.1)
|
78
|
+
timers (4.0.1)
|
79
|
+
hitimes
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
bundler (~> 1.7)
|
86
|
+
freerider!
|
87
|
+
guard
|
88
|
+
guard-rspec
|
89
|
+
pry
|
90
|
+
pry-nav
|
91
|
+
pry-remote
|
92
|
+
rake (~> 10.0)
|
93
|
+
rspec
|
94
|
+
rspec-nc
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Eric Ebbesen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Freerider
|
2
|
+
|
3
|
+
freerider identifies car2go vehicles in your area which you can refuel for free minutes!
|
4
|
+
|
5
|
+
|
6
|
+
### What?
|
7
|
+
freerider is a gem that uses [the car2go api](https://code.google.com/p/car2go/wiki/index_v2_1) to locate local car2go vehicles that are less than a specified percent fueled.
|
8
|
+
|
9
|
+
You need to [get your own consumer key from car2go](https://www.car2go.com/en/austin/car2go-apps/).
|
10
|
+
|
11
|
+
### Why?
|
12
|
+
car2go offers 20 minutes of free time when you refuel a vehicle that is less than 25 percent fueled. See the Parking and refueling section of [this page](https://www.car2go.com/en/minneapolis/how-does-car2go-work/) for details.
|
13
|
+
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'freerider'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install freerider
|
30
|
+
|
31
|
+
## Packaging
|
32
|
+
`rake build`
|
33
|
+
|
34
|
+
or run
|
35
|
+
|
36
|
+
`./build`
|
37
|
+
|
38
|
+
to build _and_ install the gem
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
After packaging you can run
|
43
|
+
|
44
|
+
$ CONSUMER_KEY=<your_car2go_consumer_key> FROM_LOCATION='Tivoli, Danmark' bin/freerider [-l <location>] [-f <fuel_threshold>] [-r <radius>]
|
45
|
+
or
|
46
|
+
|
47
|
+
$ export CONSUMER_KEY=<your_car2go_consumer_key>
|
48
|
+
$ bin/freerider [-l <location>] [-f <fuel_threshold>] [-r <radius>]
|
49
|
+
|
50
|
+
For example
|
51
|
+
|
52
|
+
$ export CONSUMER_KEY=conkey bin/freerider v -l kobenhavn -f 20 -r 3
|
53
|
+
will use your consumer key and retrieve all vehicles in Copenhagen that are 20 or less fueled
|
54
|
+
|
55
|
+
`location` will default to `twincities` (represent!).
|
56
|
+
`fuel_threshold` will default to `25`.
|
57
|
+
|
58
|
+
## Dependencies
|
59
|
+
I work/test using Ruby 2.2.x but any version 2.0.0 or greater will work. Ruby 1.9.3 is right out.
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it ( https://github.com/eebbesen/freerider/fork )
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create a new Pull Request
|
68
|
+
|
69
|
+
|
70
|
+
## Helpful tools
|
71
|
+
### Geocoder gem
|
72
|
+
#### Get geocode for address or business name
|
73
|
+
|
74
|
+
Geocoder.coordinates("Fasika, Saint Paul, MN")
|
75
|
+
|
76
|
+
#### Distance calculation
|
77
|
+
|
78
|
+
geo_distance = Geocoder::Calculations.distance_between([-93.09774, 44.93669],[-93.17795, 44.94289])
|
79
|
+
Geocoder::Calculations.to_miles(geo_distance)
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
# Default directory to look in is `/specs`
|
5
|
+
# Run with `rake spec`
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
7
|
+
task.rspec_opts = ['--color', '--format', 'progress']
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: :spec
|
data/bin/freerider
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
require_relative '../lib/freerider'
|
5
|
+
require_relative '../lib/formatter'
|
6
|
+
require_relative '../lib/geolocutor'
|
7
|
+
|
8
|
+
##
|
9
|
+
# Command-line interface for Freerider
|
10
|
+
class FreeriderScript < Thor
|
11
|
+
SUMMARY_BORDER = '=========================='
|
12
|
+
RECORD_DELIMITER = SUMMARY_BORDER + SUMMARY_BORDER
|
13
|
+
|
14
|
+
desc 'vehicles', "Get vehicles: location defaults to 'twincities', fuel_threshold defaults to '25'"
|
15
|
+
option :location, default: 'twincities', aliases: :l
|
16
|
+
option :fuel_threshold, type: :numeric, default: 25, aliases: :f
|
17
|
+
option :radius, type: :numeric, aliases: :r
|
18
|
+
def vehicles
|
19
|
+
require_key
|
20
|
+
vehicles = Freerider.new((options[:location] || ENV['FROM_LOCATION']), ENV['CONSUMER_KEY'])
|
21
|
+
display(vehicles.find_vehicles(options[:fuel_threshold], options[:radius]))
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def display(vehicles)
|
27
|
+
vehicles.each do |vehicle|
|
28
|
+
puts RECORD_DELIMITER
|
29
|
+
puts Formatter.format_vehicle(vehicle.to_json)
|
30
|
+
end
|
31
|
+
summarize(vehicles)
|
32
|
+
end
|
33
|
+
|
34
|
+
def summarize(vehicles)
|
35
|
+
puts SUMMARY_BORDER
|
36
|
+
|
37
|
+
notify_suffix = "with a fuel level below #{options[:fuel_threshold]} percent and within radius #{options[:radius]}"
|
38
|
+
if vehicles.size > 0
|
39
|
+
cars = if vehicles.size > 1
|
40
|
+
'cars'
|
41
|
+
else
|
42
|
+
'car'
|
43
|
+
end
|
44
|
+
|
45
|
+
print "#{vehicles.size} #{cars} found #{notify_suffix}"
|
46
|
+
else
|
47
|
+
print "No cars found #{notify_suffix}"
|
48
|
+
end
|
49
|
+
|
50
|
+
puts "\n#{SUMMARY_BORDER}"
|
51
|
+
end
|
52
|
+
|
53
|
+
# this is set using an environment variable
|
54
|
+
def require_key
|
55
|
+
return if ENV['CONSUMER_KEY']
|
56
|
+
puts 'Set environment variable CONSUMER_KEY to your Car2Go consumer key.'
|
57
|
+
puts 'For example:'
|
58
|
+
puts ' $ CONSUMER_KEY=mykey bin/freerider v'
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
FreeriderScript.start(ARGV)
|
data/build
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
build_return = `gem build freerider.gemspec`
|
5
|
+
|
6
|
+
gemfile = ''
|
7
|
+
lines = build_return.split "\n"
|
8
|
+
puts lines
|
9
|
+
lines.each do |line|
|
10
|
+
gemfile = line.split(' ')[1] if line.start_with? ' File: '
|
11
|
+
end
|
12
|
+
|
13
|
+
puts `gem install freerider`
|
14
|
+
|
15
|
+
FileUtils.mv(gemfile, './pkg/')
|
data/freerider.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'freerider/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'freerider'
|
8
|
+
spec.version = Freerider::VERSION
|
9
|
+
spec.authors = ['Eric Ebbesen']
|
10
|
+
spec.email = ['git.eebbesen@gmail.com']
|
11
|
+
spec.summary = 'Locate local car2go vehicles that need to be fueled.'
|
12
|
+
spec.description = 'car2go offers 20 minutes of free time when you refuel a vehicle that is less than 25 percent fueled. freerider identifies car2go vehicles in your area which you can refuel for free minutes!'
|
13
|
+
spec.homepage = 'https://github.com/eebbesen/freerider'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.0'
|
22
|
+
|
23
|
+
spec.add_dependency 'caruby2go'
|
24
|
+
spec.add_dependency 'geocoder'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
|
29
|
+
# rspec
|
30
|
+
spec.add_development_dependency 'rspec'
|
31
|
+
spec.add_development_dependency 'rspec-nc'
|
32
|
+
spec.add_development_dependency 'guard'
|
33
|
+
spec.add_development_dependency 'guard-rspec'
|
34
|
+
spec.add_development_dependency 'pry'
|
35
|
+
spec.add_development_dependency 'pry-remote'
|
36
|
+
spec.add_development_dependency 'pry-nav'
|
37
|
+
end
|
data/lib/formatter.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
##
|
2
|
+
# Addresses don't always have
|
3
|
+
# * a city
|
4
|
+
# * a zip code
|
5
|
+
# * a numeric component of the street address
|
6
|
+
class Formatter
|
7
|
+
def self.format_vehicle(vehicle)
|
8
|
+
address = format_address vehicle
|
9
|
+
maps_uri = format_maps_uri vehicle
|
10
|
+
stats = format_status vehicle
|
11
|
+
|
12
|
+
"#{address}\n#{stats}\n#{maps_uri}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.format_address(vehicle)
|
16
|
+
address_parts = (JSON.parse vehicle)['address'].split ','
|
17
|
+
return address_parts[0] if address_parts.size == 1
|
18
|
+
|
19
|
+
street_address = address_parts[0].split ' '
|
20
|
+
|
21
|
+
# Prepend with number if address has number part
|
22
|
+
display_address = if (street_address.last =~ /\d/).nil?
|
23
|
+
street_address.join ' '
|
24
|
+
else
|
25
|
+
number = street_address.pop
|
26
|
+
street_address = street_address.join ' '
|
27
|
+
"#{number} #{street_address}"
|
28
|
+
end
|
29
|
+
|
30
|
+
# Add city if address has city part
|
31
|
+
if address_parts[1]
|
32
|
+
ciudad = address_parts[1].split ' '
|
33
|
+
ciudad.shift unless (ciudad.first =~ /\d/).nil? # zipcode
|
34
|
+
display_address += ", #{ciudad.join ' '}"
|
35
|
+
end
|
36
|
+
|
37
|
+
display_address
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.format_maps_uri(vehicle)
|
41
|
+
geocode = (JSON.parse vehicle)['coordinates']
|
42
|
+
latitude = geocode[0]
|
43
|
+
longitude = geocode[1]
|
44
|
+
# accuracy = geocode[2]
|
45
|
+
|
46
|
+
"https://www.google.com/maps/preview?q=#{longitude},#{latitude}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.format_status(vehicle)
|
50
|
+
fuel_level = (JSON.parse vehicle)['fuel']
|
51
|
+
license_plate = (JSON.parse vehicle)['name']
|
52
|
+
|
53
|
+
"<<FUEL: #{fuel_level}>> #{license_plate}"
|
54
|
+
end
|
55
|
+
end
|
data/lib/freerider.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'freerider/version'
|
2
|
+
require 'caruby2go'
|
3
|
+
|
4
|
+
##
|
5
|
+
# Process values from car2go API calls
|
6
|
+
class Freerider
|
7
|
+
TC = 'twincities'
|
8
|
+
|
9
|
+
def initialize(location,
|
10
|
+
consumer_key = ENV['CONSUMER_KEY'],
|
11
|
+
from_location = ENV['FROM_LOCATION'])
|
12
|
+
@car2go = Caruby2go.new(consumer_key, default_if_empty(location, TC))
|
13
|
+
@geolocutor = Geolocutor.new(from_location)
|
14
|
+
end
|
15
|
+
|
16
|
+
def find_vehicles(upper_fuel_threshold = 25, radius = 100)
|
17
|
+
upper_fuel_threshold = default_if_empty(upper_fuel_threshold, 25)
|
18
|
+
radius = default_if_empty(radius, 100)
|
19
|
+
(@car2go.vehicles.select do |vehicle|
|
20
|
+
within_fuel_range(vehicle, upper_fuel_threshold) && within_radius(vehicle, radius)
|
21
|
+
end
|
22
|
+
).compact
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def within_fuel_range(vehicle, upper_fuel_threshold)
|
28
|
+
vehicle['fuel'] < upper_fuel_threshold
|
29
|
+
end
|
30
|
+
|
31
|
+
def within_radius(vehicle, radius)
|
32
|
+
@geolocutor.distance_from_here(vehicle) <= radius
|
33
|
+
end
|
34
|
+
|
35
|
+
def default_if_empty(value, default)
|
36
|
+
if !value || (value.respond_to?('empty?') && value.empty?)
|
37
|
+
default
|
38
|
+
else
|
39
|
+
value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/geolocutor.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'geocoder'
|
2
|
+
|
3
|
+
##
|
4
|
+
# Wrap Geocoder gem to calculate relative distances
|
5
|
+
class Geolocutor
|
6
|
+
include Geocoder
|
7
|
+
|
8
|
+
attr_reader :coordinates
|
9
|
+
|
10
|
+
def initialize(location, metric = false)
|
11
|
+
@coordinates = coordinatize(location)
|
12
|
+
@metric = metric
|
13
|
+
end
|
14
|
+
|
15
|
+
def distance_from_here(there)
|
16
|
+
there = coordinatize(extract_coordinates(there))
|
17
|
+
distance = Geocoder::Calculations.distance_between(@coordinates, there)
|
18
|
+
|
19
|
+
return Geocoder::Calculations.to_kilometers(distance) if @metric
|
20
|
+
Geocoder::Calculations.to_miles(distance)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def coordinatize(location)
|
26
|
+
fail(ArgumentError, 'You must supply coordinates, and address or a location name') if location.nil?
|
27
|
+
Geocoder.coordinates(location)
|
28
|
+
end
|
29
|
+
|
30
|
+
# car2go reverses the lat/long
|
31
|
+
# and also includes an accuracy value which must be removed
|
32
|
+
def extract_coordinates(vehicle_json)
|
33
|
+
coordinates = vehicle_json['coordinates']
|
34
|
+
coordinates.pop
|
35
|
+
coordinates.reverse
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Formatter do
|
4
|
+
let(:vehicle) { '{"address":"Grand Ave 1600, 55105 St Paul","coordinates":[-93.16789,44.93999,0],"engineType":"CE","exterior":"GOOD","fuel":26,"interior":"GOOD","name":"AB0000","smartPhoneRequired":false,"vin":"AAAAA0AA0AA000000"}' }
|
5
|
+
let(:vehicle_2) { '{"address":"10th & Park park spot","coordinates":[-93.26529,44.97035,0],"engineType":"CE","exterior":"GOOD","fuel":18,"interior":"GOOD","name":"AB1111","smartPhoneRequired":false,"vin":"BAAAA0AA0AA000000"}' }
|
6
|
+
let(:vehicle_3) { '{"address":"Dr Justus Ohage Blvd, 55107 St Paul","coordinates":[-93.09774,44.93669,0],"engineType":"CE","exterior":"GOOD","fuel":100,"interior":"GOOD","name":"AB2222","smartPhoneRequired":false,"vin":"BAAAA0AA0AA000001"}' }
|
7
|
+
let(:vehicle_4) { '{"address":"Abbot Parkspot","coordinates":[-93.27623,44.96604,0],"engineType":"CE","exterior":"BAD","fuel":1,"interior":"GOOD","name":"AB3333","smartPhoneRequired":false,"vin":"BAAAA0AA0AA000003"}' }
|
8
|
+
|
9
|
+
describe '#format_vehicle' do
|
10
|
+
it 'should return a formatted representation of a vehicle' do
|
11
|
+
expect(Formatter.format_vehicle(vehicle)).to eq "1600 Grand Ave, St Paul\n<<FUEL: 26>> AB0000\nhttps://www.google.com/maps/preview?q=44.93999,-93.16789"
|
12
|
+
expect(Formatter.format_vehicle(vehicle_2)).to eq "10th & Park park spot\n<<FUEL: 18>> AB1111\nhttps://www.google.com/maps/preview?q=44.97035,-93.26529"
|
13
|
+
expect(Formatter.format_vehicle(vehicle_3)).to eq "Dr Justus Ohage Blvd, St Paul\n<<FUEL: 100>> AB2222\nhttps://www.google.com/maps/preview?q=44.93669,-93.09774"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#format_address' do
|
18
|
+
it "should return a formatted representation of the vehicle's street address location" do
|
19
|
+
expect(Formatter.format_address(vehicle)).to eq '1600 Grand Ave, St Paul'
|
20
|
+
expect(Formatter.format_address(vehicle_2)).to eq '10th & Park park spot'
|
21
|
+
expect(Formatter.format_address(vehicle_3)).to eq 'Dr Justus Ohage Blvd, St Paul'
|
22
|
+
expect(Formatter.format_address(vehicle_4)).to eq 'Abbot Parkspot'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#format_maps_uri' do
|
27
|
+
it "should return a google maps uri of the vehicle's street address location" do
|
28
|
+
expect(Formatter.format_maps_uri(vehicle)).to eq 'https://www.google.com/maps/preview?q=44.93999,-93.16789'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#format_status' do
|
33
|
+
it ' should display the fuel leve and license plate of the vehicle' do
|
34
|
+
expect(Formatter.format_status(vehicle)).to eq '<<FUEL: 26>> AB0000'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
# Read up on this to address deprecation warnings
|
3
|
+
# http://stackoverflow.com/questions/20275510/how-to-avoid-deprecation-warning-for-stub-chain-in-rspec-3-0
|
4
|
+
|
5
|
+
VEHICLES_JSON = '[{"address": "Grand Ave 1600, 55105 St Paul", "coordinates": [-93.16789, 44.93999, 0], "engineType": "CE", "exterior": "GOOD", "fuel": 26, "interior": "GOOD", "name": "AB1111", "smartPhoneRequired": false, "vin": "AAAAA0AA0AA000000" }, { "address": "West Kellogg Boulevard 15, 55102 St Paul", "coordinates": [-93.093654, 44.943895, 0], "engineType": "CE", "exterior": "GOOD", "fuel": 25, "interior": "GOOD", "name": "AB6860", "smartPhoneRequired": false, "vin": "AAAAA1AA1AA111111" }, { "address": "South 5th Street 350, 55415 Minneapolis", "coordinates": [-93.265769, 44.976851, 0], "engineType": "CE", "exterior": "GOOD", "fuel": 24, "interior": "GOOD", "name": "AB0000", "smartPhoneRequired": false, "vin": "AAAAA2AA2AA222222"}]'
|
6
|
+
|
7
|
+
##
|
8
|
+
# Test stub for Caruby2go
|
9
|
+
class Caruby2go
|
10
|
+
attr_accessor :key
|
11
|
+
attr_accessor :location
|
12
|
+
|
13
|
+
def initialize(key, location)
|
14
|
+
@key = key
|
15
|
+
@location = location
|
16
|
+
end
|
17
|
+
|
18
|
+
def vehicles
|
19
|
+
JSON.parse VEHICLES_JSON
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Add accessor for Caruby2go instance to Freerider
|
25
|
+
class Freerider
|
26
|
+
attr_reader :car2go
|
27
|
+
end
|
28
|
+
|
29
|
+
describe Freerider do
|
30
|
+
subject { Freerider.new('twincities', 'fake_key', 'Grand Ave 1600, 55105 St Paul') }
|
31
|
+
|
32
|
+
describe '#find_vehicles' do
|
33
|
+
it 'should return json for all vehicles below 25 by default' do
|
34
|
+
vehicles = subject.find_vehicles
|
35
|
+
expect(vehicles.size).to eq 1
|
36
|
+
expect(subject.car2go.location).to eq 'twincities'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should return json for all vehicles below specified threshold' do
|
40
|
+
vehicles = subject.find_vehicles(26)
|
41
|
+
expect(vehicles.size).to eq 2
|
42
|
+
expect(subject.car2go.location).to eq 'twincities'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should return json for all vehicles within the given radius' do
|
46
|
+
vehicles = subject.find_vehicles(100, 1)
|
47
|
+
expect(vehicles.size).to eq 1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Geolocutor do
|
4
|
+
let(:vehicle) { '{"address":"Grand Ave 1600, 55105 St Paul","coordinates":[-93.16789,44.93999,0],"engineType":"CE","exterior":"GOOD","fuel":26,"interior":"GOOD","name":"AB0000","smartPhoneRequired":false,"vin":"AAAAA0AA0AA000000"}' }
|
5
|
+
let(:vehicle_2) { '{"address":"10th & Park park spot","coordinates":[-93.26529,44.97035,0],"engineType":"CE","exterior":"GOOD","fuel":18,"interior":"GOOD","name":"AB1111","smartPhoneRequired":false,"vin":"BAAAA0AA0AA000000"}' }
|
6
|
+
let(:vehicle_3) { '{"address":"Dr Justus Ohage Blvd, 55107 St Paul","coordinates":[-93.09774,44.93669,0],"engineType":"CE","exterior":"GOOD","fuel":100,"interior":"GOOD","name":"AB2222","smartPhoneRequired":false,"vin":"BAAAA0AA0AA000001"}' }
|
7
|
+
let(:vehicle_4) { '{"address":"Abbot Parkspot","coordinates":[-93.27623,44.96604,0],"engineType":"CE","exterior":"BAD","fuel":1,"interior":"GOOD","name":"AB3333","smartPhoneRequired":false,"vin":"BAAAA0AA0AA000003"}' }
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
it 'should create coordinates from address' do
|
11
|
+
geolocutor = Geolocutor.new('1600 Grand Ave, Saint Paul, MN')
|
12
|
+
(geolocutor.coordinates).should eql([44.9397631, -93.1687294])
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should preserve passed-in coordinates' do
|
16
|
+
geolocutor = Geolocutor.new [44.93999, -93.16789]
|
17
|
+
(geolocutor.coordinates).should eql([44.9397631, -93.1687294])
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should raise an error if no location passed in' do
|
21
|
+
expect { Geolocutor.new nil }.to raise_error(ArgumentError, 'You must supply coordinates, and address or a location name')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#extract_coordinates' do
|
26
|
+
it 'should grab coordinates from vehicle JSON' do
|
27
|
+
geolocutor = Geolocutor.new('1600 Grand Ave, Saint Paul, MN')
|
28
|
+
coordinates = geolocutor.send(:extract_coordinates, JSON.parse(vehicle))
|
29
|
+
coordinates.should eql([44.93999, -93.16789])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#distance_from_here' do
|
34
|
+
before do
|
35
|
+
@geolocutor = Geolocutor.new('1600 Grand Ave, Saint Paul, MN')
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should return zero when the from and to are the same' do
|
39
|
+
distance = @geolocutor.distance_from_here(JSON.parse(vehicle))
|
40
|
+
distance.should eq 0
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when looking for the disance in miles' do
|
44
|
+
it 'should return the proper value' do
|
45
|
+
distance = @geolocutor.distance_from_here(JSON.parse(vehicle_2))
|
46
|
+
distance.should eql 3.215852550959139
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when looking for the disance in kilometers' do
|
51
|
+
it 'should return the proper value' do
|
52
|
+
geolocutor = Geolocutor.new('1600 Grand Ave, Saint Paul, MN', true)
|
53
|
+
distance = geolocutor.distance_from_here(JSON.parse(vehicle_2))
|
54
|
+
distance.should eq 8.329019877940437
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: freerider
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric Ebbesen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: caruby2go
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: geocoder
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-nc
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: guard
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: guard-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: pry-remote
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: pry-nav
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
description: car2go offers 20 minutes of free time when you refuel a vehicle that
|
168
|
+
is less than 25 percent fueled. freerider identifies car2go vehicles in your area
|
169
|
+
which you can refuel for free minutes!
|
170
|
+
email:
|
171
|
+
- git.eebbesen@gmail.com
|
172
|
+
executables:
|
173
|
+
- freerider
|
174
|
+
extensions: []
|
175
|
+
extra_rdoc_files: []
|
176
|
+
files:
|
177
|
+
- ".gitignore"
|
178
|
+
- Gemfile
|
179
|
+
- Gemfile.lock
|
180
|
+
- LICENSE.txt
|
181
|
+
- README.md
|
182
|
+
- Rakefile
|
183
|
+
- bin/freerider
|
184
|
+
- build
|
185
|
+
- freerider.gemspec
|
186
|
+
- lib/formatter.rb
|
187
|
+
- lib/freerider.rb
|
188
|
+
- lib/freerider/version.rb
|
189
|
+
- lib/geolocutor.rb
|
190
|
+
- spec/formatter_spec.rb
|
191
|
+
- spec/freerider_spec.rb
|
192
|
+
- spec/geolocutor_spec.rb
|
193
|
+
- spec/spec_helper.rb
|
194
|
+
homepage: https://github.com/eebbesen/freerider
|
195
|
+
licenses:
|
196
|
+
- MIT
|
197
|
+
metadata: {}
|
198
|
+
post_install_message:
|
199
|
+
rdoc_options: []
|
200
|
+
require_paths:
|
201
|
+
- lib
|
202
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
203
|
+
requirements:
|
204
|
+
- - ">="
|
205
|
+
- !ruby/object:Gem::Version
|
206
|
+
version: '2.0'
|
207
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
208
|
+
requirements:
|
209
|
+
- - ">="
|
210
|
+
- !ruby/object:Gem::Version
|
211
|
+
version: '0'
|
212
|
+
requirements: []
|
213
|
+
rubyforge_project:
|
214
|
+
rubygems_version: 2.4.5
|
215
|
+
signing_key:
|
216
|
+
specification_version: 4
|
217
|
+
summary: Locate local car2go vehicles that need to be fueled.
|
218
|
+
test_files:
|
219
|
+
- spec/formatter_spec.rb
|
220
|
+
- spec/freerider_spec.rb
|
221
|
+
- spec/geolocutor_spec.rb
|
222
|
+
- spec/spec_helper.rb
|