one_bus_away 0.0.1 → 0.0.2

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: 628ea7954f796b3dce9a2dc744ee7f5c5e9a5261
4
- data.tar.gz: a62af7684ca5bd5aebbec632807718f4c652dec6
3
+ metadata.gz: cb4cdf6f62d4648d4903644aa16332432a8a4da1
4
+ data.tar.gz: 08ba7dd760a9689cd393d0b64a34aa7493933a8b
5
5
  SHA512:
6
- metadata.gz: 019da129cd7e004b56b46802b6d4bd1190366c7034adf2e6ad84b19fd708bb1907e72c16c85a1f951e812877f71afcc5d493e9fd5229a8a2d1c300022e97ab0d
7
- data.tar.gz: 56c543723160f605d9349708e83da35f14b0a289c75ecb9693fa835c6b8fc5d8269b4eacb5e33cfe21f9f53be9803d3c9171e216b196b578a876ff116147fe1c
6
+ metadata.gz: 73641a3964d0fd91e848e8c0074a6eb10ee6fc46b6074a146d3ca1ff6cddc6a8f222dc8aea812bb2ad2b631869bc953ac5f65384fb554eaa1b4cad4e27ea431a
7
+ data.tar.gz: c984fba70ef2f9efcde525ffae6f16ef8ee304c73c57b0e149272b158e2bf2499b67012772faf7b27abeab1299117806eb61ea68b46066741ac2d4f129b064ce
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ .DS_Store
data/README.md CHANGED
@@ -17,13 +17,22 @@ And then execute:
17
17
  Or install it yourself as:
18
18
 
19
19
  $ gem install one_bus_away
20
+
21
+ 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
+
23
+ $ cat ~/.one_bus_away
24
+ 6xxxxxf7-6ec4-45adsf22-bf33-sdfsdfs
25
+
20
26
 
21
27
  ## Usage
22
28
 
23
29
 
24
30
  one-bus-away arrivals-and-departures-for-stop [stop] [route] [arrival time from now]
25
31
 
26
- one-bus-away arrivals-and-departures-for-stop 29215 "44" 15
32
+ $ one-bus-away arrivals-and-departures-for-stop 13721 "D Line" 22
33
+ The D Line arrives in 4 minutes
34
+ The D Line arrives in 19 minutes
35
+
27
36
  ## Contributing
28
37
 
29
38
  1. Fork it ( https://github.com/ellisandy/one-bus-away-cli/fork )
data/bin/one-bus-away CHANGED
@@ -2,19 +2,22 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'commander/import'
5
- require 'one_bus_away'
5
+ #require 'one_bus_away'
6
+ require_relative '../lib/one_bus_away'
6
7
 
7
8
  program :version, '0.0.1'
8
9
  program :description, 'Simple querying of One Bus Away'
9
10
 
11
+ # puts $LOAD_PATH
12
+ @one_bus_away = OneBusAway.new(File.read(ENV['HOME']+"/.one_bus_away"))
13
+
10
14
  command "current-time" do |c|
11
15
  c.syntax = 'one-bus-away current-time'
12
16
  c.summary = 'Get the current system time from One Bus Away'
13
17
  c.description = 'Makes the current_time API call against the One Bus Away API. Simple, Straightforward, Unneeded'
14
18
  c.example '','one-bus-away current-time'
15
19
  c.action do |args, options|
16
- puts OneBusAway.current_time
17
- notify 'Something happened'
20
+ puts @one_bus_away.current_time
18
21
  end
19
22
  end
20
23
 
@@ -24,12 +27,13 @@ command "arrivals-and-departures-for-stop" do |c|
24
27
  c.description = 'By default, you can get the next available arrival time.'
25
28
  c.example 'description', 'one-bus-away arrivals-and-departures-for-stop [stop] [route] [arrival time from now]'
26
29
  c.action do |args, options|
27
- # Implement arrivals-and-departures-for-stop [stop] [route]
28
-
29
- arrivals = OneBusAway.arrivals_and_departures_for_stop(args[0], args[1], args[2])
30
-
31
- arrivals.each do |arrival|
32
- puts "The #{args[1]} arrives in #{arrival}"
30
+ arrivals = @one_bus_away.arrivals_and_departures_for_stop(args[0], args[1], args[2])
31
+ unless arrivals.empty?
32
+ arrivals.each do |arrival|
33
+ puts "The #{args[1]} arrives in #{arrival}"
34
+ end
35
+ else
36
+ puts "No buses in that time constrain"
33
37
  end
34
38
  end
35
39
  end
@@ -1,3 +1,3 @@
1
- module OneBusAway
2
- VERSION = "0.0.1"
1
+ class OneBusAway
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/one_bus_away.rb CHANGED
@@ -1,33 +1,74 @@
1
- require "one_bus_away/version"
1
+ require_relative "one_bus_away/version"
2
2
  require 'rest-client'
3
3
  require 'utilities'
4
+ require 'contracts'
4
5
 
5
- module OneBusAway
6
- def self.api_key
7
- return File.read(ENV['HOME']+"/.one_bus_away")
6
+ class OneBusAway
7
+ include Contracts::Core
8
+ C = Contracts
9
+
10
+ Contract String => String
11
+ def initialize(api_key)
12
+ @api_key = api_key
8
13
  end
9
-
10
- def self.current_time
11
- response = RestClient.get("http://api.pugetsound.onebusaway.org/api/where/current-time.json?key=#{self.api_key}")
14
+
15
+ Contract String
16
+ def current_time
17
+ response = RestClient.get("http://api.pugetsound.onebusaway.org/api/where/current-time.json?key=#{@api_key}")
12
18
  json = JSON.parse(response)
13
19
  time = json["data"]["entry"]["time"]
14
- return time
20
+ return time.to_s
21
+ end
22
+
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
+
44
+
15
45
  end
16
46
 
17
- def self.arrivals_and_departures_for_stop(stop, route, time_to_look_for)
18
- #
19
- response = RestClient.get("http://api.pugetsound.onebusaway.org/api/where/arrivals-and-departures-for-stop/1_#{stop}.json?key=#{self.api_key}&minutesAfter=#{time_to_look_for}")
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}")
20
50
  json = JSON.parse(response)
21
- arrivalsAndDepartures = json["data"]["entry"]["arrivalsAndDepartures"]
22
51
 
23
- @arrivalsAndDepartures2 = []
24
-
25
- arrivalsAndDepartures.each do |bus|
26
- if bus["routeShortName"] == route
27
- @arrivalsAndDepartures2.push(Utilities.convert_time(bus["scheduledDepartureTime"].to_s))
28
- end
52
+ if json["code"] == 200
53
+ json["data"]["list"].include? "1_#{stop_number}"
54
+ else
55
+ fail "OneBusAway API HTTP response error #{json['code']}"
29
56
  end
57
+ end
58
+
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)
30
63
 
31
- return @arrivalsAndDepartures2
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']}"
71
+ end
32
72
  end
73
+
33
74
  end
data/lib/utilities.rb CHANGED
@@ -1,22 +1,21 @@
1
1
  require("date")
2
-
3
2
  class Utilities
4
3
  def self.convert_time(time)
5
4
  stripped_time = self.strip_time(time)
6
-
5
+
7
6
  converted_date = DateTime.strptime(stripped_time,"%s")
8
-
7
+
9
8
  new_time = converted_date.to_time#.strftime "%I:%M %P"
10
9
  distance_of_time_in_hours_and_minutes(new_time, Time.now)
11
10
  end
12
-
11
+
13
12
  def self.strip_time(time_to_strip)
14
13
  n = time_to_strip.size
15
14
  stripped_time = time_to_strip[0..n-4]
16
15
  return stripped_time
17
-
16
+
18
17
  end
19
-
18
+
20
19
  def self.distance_of_time_in_hours_and_minutes(from_time, to_time)
21
20
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
22
21
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
@@ -30,4 +29,4 @@ class Utilities
30
29
  words << "#{hours} #{hours > 1 ? 'hours' : 'hour' } and " if hours > 0
31
30
  words << "#{minutes} #{minutes == 1 ? 'minute' : 'minutes' }"
32
31
  end
33
- end
32
+ end
data/one_bus_away.gemspec CHANGED
@@ -21,7 +21,9 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.2"
24
+ spec.add_development_dependency "contracts"
24
25
  spec.add_development_dependency "guard-rspec", "~> 4.5"
26
+ spec.add_development_dependency "terminal-notifier-guard"
25
27
  spec.add_dependency('commander')
26
28
  spec.add_dependency('rest-client')
27
- end
29
+ end
@@ -1,24 +1,34 @@
1
1
  require 'one_bus_away'
2
2
 
3
3
  RSpec.describe OneBusAway do
4
- context "Testing" do
5
- it "runs initial test" do
6
- expect(OneBusAway).not_to eq(nil)
4
+ describe '.new' do
5
+ it 'should raise error without arguements' do
6
+ expect { OneBusAway.new() }.to raise_error
7
+ end
8
+
9
+ it 'should accept API_KEY as arguement' do
10
+ expect { OneBusAway.new("somestring")}.not_to raise_error
7
11
  end
8
12
  end
9
13
 
10
- context "api_key" do
11
- it "does not equal nil" do
12
- expect(OneBusAway.api_key).not_to eq(nil)
14
+ describe '#current_time' do
15
+ let(:one_bus_away) { OneBusAway.new("6a1c72f7-6ec4-4522-bf33-3698b3ad86c2") }
16
+ it 'returns true' do
17
+ expect { one_bus_away.current_time }.not_to raise_error
13
18
  end
14
- it "errors out when no file is found" do
15
- expect(OneBusAway.api_key).to eq(File.read(ENV['HOME']+"/.one_bus_away"))
19
+ end
20
+
21
+ describe '#arrivals_and_departures_for_stop' do
22
+ let(:one_bus_away) { OneBusAway.new("6a1c72f7-6ec4-4522-bf33-3698b3ad86c2") }
23
+ it 'returns true' do
24
+ expect { one_bus_away.arrivals_and_departures_for_stop(18145, "40", 10) }.not_to raise_error
16
25
  end
17
26
  end
18
27
 
19
- context 'get current time' do
20
- it 'returns the current time' do
21
- expect(OneBusAway.current_time.class).to eq(Fixnum)
28
+ describe '#valid_stop?' do
29
+ let(:one_bus_away) { OneBusAway.new("6a1c72f7-6ec4-4522-bf33-3698b3ad86c2") }
30
+ it 'returns true' do
31
+ expect { one_bus_away.valid_stop?(18145) }.not_to raise_error
22
32
  end
23
33
  end
24
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: one_bus_away
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Ellis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-29 00:00:00.000000000 Z
11
+ date: 2015-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: contracts
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: guard-rspec
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '4.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: terminal-notifier-guard
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'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: commander
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -144,3 +172,4 @@ summary: Simple Gem to query One Bus Away API
144
172
  test_files:
145
173
  - spec/one_bus_away_spec.rb
146
174
  - spec/spec_helper.rb
175
+ has_rdoc: