one_bus_away 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb4cdf6f62d4648d4903644aa16332432a8a4da1
4
- data.tar.gz: 08ba7dd760a9689cd393d0b64a34aa7493933a8b
3
+ metadata.gz: 9d2d9bd435b8006453f7f7b435523828fdddf382
4
+ data.tar.gz: d4297a3e713d857573a3f1b4f15f0a923f8e2493
5
5
  SHA512:
6
- metadata.gz: 73641a3964d0fd91e848e8c0074a6eb10ee6fc46b6074a146d3ca1ff6cddc6a8f222dc8aea812bb2ad2b631869bc953ac5f65384fb554eaa1b4cad4e27ea431a
7
- data.tar.gz: c984fba70ef2f9efcde525ffae6f16ef8ee304c73c57b0e149272b158e2bf2499b67012772faf7b27abeab1299117806eb61ea68b46066741ac2d4f129b064ce
6
+ metadata.gz: 98e4044599cef53cdce3d783db2eeba736d7e18a6b0acf2c352d7a4342997b3041cf2e5841523d630b192b86169fd92cbccb53cc86cc79f9ec9fba19d0fe8dd1
7
+ data.tar.gz: 433adecd694466dd9519b1e09d330465a86739d46168700cd27b55890e1177dd8c58e14b8930c0fea9f75b72256ab67175547116350da64f93d1003dd7a5af00
data/.gitignore CHANGED
@@ -13,3 +13,6 @@
13
13
  *.a
14
14
  mkmf.log
15
15
  .DS_Store
16
+ *.gem
17
+ *.quicktask
18
+ /spec/vcr/
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --require spec_helper
3
+ --format documentation
data/Guardfile CHANGED
@@ -1,43 +1,7 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- ## Uncomment and set this to only include directories you want to watch
5
- # directories %w(app lib config test spec features)
6
-
7
- ## Uncomment to clear the screen before every task
8
- # clearing :on
9
-
10
- ## Guard internally checks for changes in the Guardfile and exits.
11
- ## If you want Guard to automatically start up again, run guard in a
12
- ## shell loop, e.g.:
13
- ##
14
- ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
- ##
16
- ## Note: if you are using the `directories` clause above and you are not
17
- ## watching the project directory ('.'), then you will want to move
18
- ## the Guardfile to a watched dir and symlink it back, e.g.
19
- #
20
- # $ mkdir config
21
- # $ mv Guardfile config/
22
- # $ ln -s config/Guardfile .
23
- #
24
- # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
-
26
- # Note: The cmd option is now required due to the increasing number of ways
27
- # rspec may be run, below are examples of the most common uses.
28
- # * bundler: 'bundle exec rspec'
29
- # * bundler binstubs: 'bin/rspec'
30
- # * spring: 'bin/rspec' (This will use spring if running and you have
31
- # installed the spring binstubs per the docs)
32
- # * zeus: 'zeus rspec' (requires the server to be started separately)
33
- # * 'just' rspec: 'rspec'
34
-
35
- guard :rspec, cmd: "bundle exec rspec" do
36
- require "guard/rspec/dsl"
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ require 'guard/rspec/dsl'
37
3
  dsl = Guard::RSpec::Dsl.new(self)
38
4
 
39
- # Feel free to open issues for suggestions and improvements
40
-
41
5
  # RSpec files
42
6
  rspec = dsl.rspec
43
7
  watch(rspec.spec_helper) { rspec.spec_dir }
@@ -51,5 +15,4 @@ guard :rspec, cmd: "bundle exec rspec" do
51
15
  # Application Files
52
16
  watch(%r{^lib/(.+)\.rb})
53
17
  watch(%r{^bin/})
54
-
55
18
  end
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # OneBusAway
2
2
 
3
- This little 'gem' is for easily querying the next avialable bus at a given stop.
3
+ This little 'gem' is for easily querying the next available bus at a given stop.
4
+
5
+ ## Pre-reqs
6
+
7
+ Please have the ruby development packaged installed.
4
8
 
5
9
  ## Installation
6
10
 
@@ -11,28 +15,32 @@ gem 'one_bus_away'
11
15
  ```
12
16
 
13
17
  And then execute:
14
-
18
+ ```
15
19
  $ bundle
16
-
20
+ ```
17
21
  Or install it yourself as:
18
-
22
+ ```
19
23
  $ gem install one_bus_away
20
-
24
+ ```
25
+
21
26
  Then you will need to create the file ~/.one_bus_away which contains just your API key which can be requested here: http://pugetsound.onebusaway.org/p/OneBusAwayApiService.action
22
27
 
28
+ ```
23
29
  $ cat ~/.one_bus_away
24
30
  6xxxxxf7-6ec4-45adsf22-bf33-sdfsdfs
25
-
31
+ ```
26
32
 
27
33
  ## Usage
28
34
 
29
35
 
30
- one-bus-away arrivals-and-departures-for-stop [stop] [route] [arrival time from now]
36
+ one-bus-away arrivals-and-departures-for-stop [stop]
31
37
 
32
- $ one-bus-away arrivals-and-departures-for-stop 13721 "D Line" 22
38
+ ```
39
+ $ one-bus-away arrivals-and-departures-for-stop 13721
33
40
  The D Line arrives in 4 minutes
34
41
  The D Line arrives in 19 minutes
35
-
42
+ ```
43
+
36
44
  ## Contributing
37
45
 
38
46
  1. Fork it ( https://github.com/ellisandy/one-bus-away-cli/fork )
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
2
4
 
5
+ task :test do
6
+ sh 'rspec'
7
+ sh 'yard stats --list-undoc'
8
+ sh 'ruby -W0 -S rubocop'
9
+ end
data/bin/one_bus_away ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'commander/import'
5
+ require_relative '../lib/one_bus_away'
6
+
7
+ program :version, '0.0.1'
8
+ program :description, 'Simple querying of One Bus Away'
9
+
10
+ if File.exist? ENV['HOME'] + '/.one_bus_away'
11
+ @one_bus_away = OneBusAway.new
12
+ else
13
+ fail 'please install your one-bus-away API key into ~/.one_bus_away'
14
+ end
15
+
16
+ command 'current-time' do |c|
17
+ c.syntax = 'one-bus-away current-time'
18
+ c.summary = 'Get the current system time from One Bus Away'
19
+ c.description = 'Makes the current_time API call against the One Bus Away'\
20
+ ' API. Simple, Straightforward, Unneeded'
21
+ c.example '', 'one-bus-away current-time'
22
+ c.action do
23
+ @one_bus_away.current_time
24
+ p @one_bus_away.data.entry.time
25
+ end
26
+ end
27
+
28
+ command 'arrivals-and-departures-for-stop' do |c|
29
+ c.syntax = 'one-bus-away arrivals-and-departures-for-stop
30
+ [stop] [route] [arrival time from now]'
31
+ c.summary = 'Next Arrival Time for a specific Stop'
32
+ c.description = 'By default, you can get the next available arrival time.'
33
+ c.example 'description', 'one-bus-away arrivals-and-departures-for-stop' \
34
+ ' [stop] [route] [arrival time from now]'
35
+ c.action do |args|
36
+ @one_bus_away.arrivals_and_departures_for_stop(
37
+ args[0]
38
+ )
39
+
40
+ busses = @one_bus_away.filter_by_route
41
+ if busses.empty?
42
+ puts 'No buses in that time constrain'
43
+ else
44
+ busses.each do |bus|
45
+ puts "The #{bus.keys.first} arrives in #{bus.values.first} minutes"
46
+ end
47
+ end
48
+ end
49
+ end
data/lib/one_bus_away.rb CHANGED
@@ -1,74 +1,63 @@
1
- require_relative "one_bus_away/version"
2
- require 'rest-client'
3
- require 'utilities'
4
- require 'contracts'
1
+ require_relative 'one_bus_away/version'
2
+ require_relative 'one_bus_away/utilities'
3
+ require_relative 'one_bus_away/client'
4
+ require 'geocoder'
5
5
 
6
+ # Main class
7
+ # defines the way to interact with everything
6
8
  class OneBusAway
7
- include Contracts::Core
8
- C = Contracts
9
+ attr_accessor :api_method, :parameters
10
+ attr_reader :client, :data, :http_response
9
11
 
10
- Contract String => String
11
- def initialize(api_key)
12
- @api_key = api_key
12
+ def initialize(options = {})
13
+ @api_method = options[:api_method]
14
+ @parameters = options[:parameters]
13
15
  end
14
16
 
15
- Contract String
17
+ # Returns the current time from the One Bus Away System
16
18
  def current_time
17
- response = RestClient.get("http://api.pugetsound.onebusaway.org/api/where/current-time.json?key=#{@api_key}")
18
- json = JSON.parse(response)
19
- time = json["data"]["entry"]["time"]
20
- return time.to_s
19
+ @client = OneBusAway::Client.new(
20
+ api_method: ['current-time']
21
+ )
22
+ call_api
21
23
  end
22
24
 
23
- # Contract C::Num, C::Or[C::Num, String], C::Num => C::ArrayOf[C::Any]
24
- def arrivals_and_departures_for_stop(stop, route, time_to_look_for)
25
- if valid_stop?(stop) && valid_route?(route)
26
- response = RestClient.get("http://api.pugetsound.onebusaway.org/api/where/arrivals-and-departures-for-stop/1_#{stop}.json?key=#{@api_key}&minutesAfter=#{time_to_look_for}")
27
- json = JSON.parse(response)
28
- arrivalsAndDepartures = json["data"]["entry"]["arrivalsAndDepartures"]
29
-
30
- outputs = []
31
-
32
- arrivalsAndDepartures.each do |bus|
33
- if bus["routeShortName"] == route
34
- outputs.sort
35
- outputs.push(Utilities.convert_time(bus["scheduledDepartureTime"].to_s))
36
- end
37
- end
38
-
39
- return outputs
40
- else
41
- fail "Either your route or stop is invalid."
42
- end
43
-
25
+ # get arrivales and departunes for a stop with one arguement
26
+ def arrivals_and_departures_for_stop(stop)
27
+ @client = OneBusAway::Client.new(
28
+ api_method: ['arrivals-and-departures-for-stop', "1_#{stop}"]
29
+ )
30
+ call_api
31
+ end
44
32
 
33
+ # Assigns the http data to the @data instance variable
34
+ def assign_data
35
+ @data = client.http_response.data
45
36
  end
46
-
47
- # Contract C::Num => C::Bool
48
- def valid_stop?(stop_number)
49
- response = RestClient.get("http://api.pugetsound.onebusaway.org/api/where/stop-ids-for-agency/1.json?key=#{@api_key}")
50
- json = JSON.parse(response)
51
-
52
- if json["code"] == 200
53
- json["data"]["list"].include? "1_#{stop_number}"
54
- else
55
- fail "OneBusAway API HTTP response error #{json['code']}"
56
- end
37
+
38
+ # Short hand for building the URL, calling the API, and assigning the data
39
+ # to @data
40
+ def call_api
41
+ @client.build_url
42
+ @client.get
43
+ assign_data
57
44
  end
58
45
 
59
- # Contract C::Or[C::Num, String] => C::Bool
60
- def valid_route?(route_name)
61
- response = RestClient.get("http://api.pugetsound.onebusaway.org/api/where/routes-for-agency/1.json?key=#{@api_key}")
62
- json = JSON.parse(response)
63
-
64
- if json["code"] == 200
65
- array = []
66
- json["data"]["list"].map { |x| array.push x["shortName"] }
67
-
68
- array.include? route_name.to_s
69
- else
70
- fail "OneBusAway API HTTP response error #{json['code']}"
46
+ # filter the route by a specific route
47
+ def filter_by_route
48
+ array = []
49
+ data.entry.arrivalsAndDepartures.each do |bus|
50
+ time = OneBusAway::Utilities.convert_time bus.scheduledDepartureTime
51
+ array << { bus.routeShortName => time }
71
52
  end
53
+ array
72
54
  end
73
55
 
56
+ # Uses the google API to git the long and lat of a given location
57
+ def get_location(loc)
58
+ geo = Geocoder.search("#{loc} seattle")
59
+ lat = geo[0].latitude
60
+ lon = geo[0].longitude
61
+ [lon, lat]
62
+ end
74
63
  end
@@ -0,0 +1,75 @@
1
+ require 'rest-client'
2
+ require 'recursive-open-struct'
3
+
4
+ class OneBusAway
5
+ # Class for establishing a connection to one bus away
6
+ class Client
7
+ attr_accessor :api_method, :api_key, :parameters
8
+ attr_reader :base_url, :url, :http_response
9
+
10
+ def initialize(options = {})
11
+ @api_method = options[:api_method]
12
+ @api_key = options[:api_key] || apply_local_api_key
13
+ @parameters = options[:parameters]
14
+ @base_url = 'api.pugetsound.onebusaway.org'
15
+ end
16
+
17
+ # Verifies that @api_method and @api_key are set
18
+ def valid?
19
+ if api_method && api_key
20
+ true
21
+ else
22
+ false
23
+ end
24
+ end
25
+
26
+ # Provided that @url is set, HTTP get @url
27
+ def get
28
+ if @url
29
+ response = RestClient.get(@url)
30
+ json = JSON.parse(response)
31
+ @http_response = RecursiveOpenStruct.new(
32
+ json, recurse_over_arrays: true
33
+ )
34
+ else
35
+ fail 'url is not properly built'
36
+ end
37
+ end
38
+
39
+ # Builds a valid url, then sets this string to @url
40
+ def build_url
41
+ @url = URI::HTTP.build(
42
+ host: @base_url,
43
+ path: build_path,
44
+ query: build_query
45
+ ).to_s if valid?
46
+ end
47
+
48
+ # Builds the path for utilization in #build_url
49
+ def build_path
50
+ path = %w(api where)
51
+ path.concat api_method
52
+ path = path.join('/')
53
+ @path = "/#{path}.json"
54
+ end
55
+
56
+ # Build query for utilization in #build_url
57
+ def build_query
58
+ query = { key: @api_key }
59
+ query.merge! @parameters if @parameters
60
+ query.map { |k, v| "#{k}=#{v}" }.join('&')
61
+ end
62
+
63
+ # Applies ~/.one_bus_away to @api if the file exists
64
+ # otherwise, it fails.
65
+ def apply_local_api_key
66
+ if File.exist? ENV['HOME'] + '/.one_bus_away'
67
+ file = File.read(ENV['HOME'] + '/.one_bus_away')
68
+ @api_key = file.chomp
69
+ else
70
+ fail 'no API key provided. Please ensure you have your api key'\
71
+ 'installed in here: ~/.one_bus_away'
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,25 @@
1
+ require 'date'
2
+ require 'active_support'
3
+ require 'action_view'
4
+
5
+ include ActionView::Helpers::DateHelper
6
+
7
+ class OneBusAway
8
+ # Useful cross class methods for sting modifications
9
+ class Utilities
10
+ # Converts time into a consumable format
11
+ def self.convert_time(time)
12
+ obj = OneBusAway::Utilities.new
13
+ stripped_time = obj.strip_time(time)
14
+ converted_date = DateTime.strptime(stripped_time, '%s')
15
+ distance_of_time_in_words_to_now(converted_date.to_time)
16
+ end
17
+
18
+ # Removes the last three characters from a string
19
+ def strip_time(time)
20
+ time = time.to_s
21
+ n = time.size
22
+ time[0..n - 4]
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,5 @@
1
+ # Defining the version for this gem
1
2
  class OneBusAway
2
- VERSION = "0.0.2"
3
+ # Version for one_bus_away gem
4
+ VERSION = '0.0.4'
3
5
  end
data/one_bus_away.gemspec CHANGED
@@ -4,26 +4,39 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'one_bus_away/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "one_bus_away"
7
+ spec.name = 'one_bus_away'
8
8
  spec.version = OneBusAway::VERSION
9
- spec.authors = ["Jack Ellis"]
10
- spec.email = ["jack@mnmlst.cc"]
11
- spec.summary = "Simple Gem to query One Bus Away API"
12
- spec.description = "This is a simple gem to query bus stops, delay times, and additional features. This does require that you apply for an API key from One Bus Away -- http://pugetsound.onebusaway.org/p/OneBusAwayApiService.action"
13
- spec.homepage = "https://github.com/ellisandy/one-bus-away-cli"
14
- spec.license = "MIT"
9
+ spec.authors = ['Jack Ellis']
10
+ spec.email = ['jack@mnmlst.cc']
11
+ spec.summary = 'Simple Gem to query One Bus Away API'
12
+ spec.description = 'This is a simple gem to query bus stops,'\
13
+ 'delay times, and additional features. This'\
14
+ 'does require that you apply for an API key'\
15
+ 'from One Bus Away --'\
16
+ 'http://pugetsound.onebusaway.org/p/OneBusAwayApiService.action'
17
+ spec.homepage = 'https://github.com/ellisandy/one-bus-away-cli'
18
+ spec.license = 'MIT'
15
19
 
16
20
  spec.files = `git ls-files -z`.split("\x0")
17
21
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
23
+ spec.require_paths = ['lib']
20
24
 
21
- spec.add_development_dependency "bundler", "~> 1.7"
22
- spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "rspec", "~> 3.2"
24
- spec.add_development_dependency "contracts"
25
- spec.add_development_dependency "guard-rspec", "~> 4.5"
26
- spec.add_development_dependency "terminal-notifier-guard"
27
- spec.add_dependency('commander')
28
- spec.add_dependency('rest-client')
25
+ spec.add_development_dependency 'bundler', '~> 1.7'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.2'
28
+ spec.add_development_dependency 'rspec-mocks', '~> 3.2'
29
+ spec.add_development_dependency 'webmock', '~> 1.22'
30
+ spec.add_development_dependency 'vcr', '~> 2.9'
31
+ spec.add_development_dependency 'contracts', '~> 0.12'
32
+ spec.add_development_dependency 'guard-rspec', '~> 4.5'
33
+ spec.add_development_dependency 'terminal-notifier-guard', '~> 1.6'
34
+ spec.add_development_dependency 'fuubar', '~> 2.0'
35
+ spec.add_development_dependency 'simplecov', '~> 0.10'
36
+ spec.add_dependency 'commander', '~> 4.3'
37
+ spec.add_dependency 'rest-client', '~> 1.8'
38
+ spec.add_dependency 'recursive-open-struct', '~> 0.6'
39
+ spec.add_dependency 'activesupport', '~> 4.2'
40
+ spec.add_dependency 'actionview', '~> 4.2'
41
+ spec.add_dependency 'geocoder', '~> 1.2'
29
42
  end