mbta-rt 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +74 -0
  6. data/Rakefile +6 -0
  7. data/lib/mbta.rb +15 -0
  8. data/lib/mbta/config.rb +5 -0
  9. data/lib/mbta/connection.rb +17 -0
  10. data/lib/mbta/predictions_request.rb +12 -0
  11. data/lib/mbta/request.rb +16 -0
  12. data/lib/mbta/routes_request.rb +11 -0
  13. data/lib/mbta/schedule_request.rb +12 -0
  14. data/lib/mbta/stops_request.rb +13 -0
  15. data/lib/mbta/vehicles_request.rb +12 -0
  16. data/lib/mbta/version.rb +3 -0
  17. data/mbta.gemspec +30 -0
  18. data/test/cassettes/Mbta_PredictionsRequest/returns_json_from_the_by_route_method.yml +44 -0
  19. data/test/cassettes/Mbta_StopsRequest/_by_location/returns_an_error_message_json_when_missing_a_lat.yml +44 -0
  20. data/test/cassettes/Mbta_StopsRequest/_by_location/returns_an_error_message_json_when_missing_a_long.yml +44 -0
  21. data/test/cassettes/Mbta_StopsRequest/_by_location/returns_valid_json_when_passed_a_lat_and_long.yml +58 -0
  22. data/test/cassettes/Mbta_StopsRequest/_by_location_returns_valid_json_when_passed_a_lat_and_long.yml +58 -0
  23. data/test/cassettes/Mbta_StopsRequest/_by_route/returns_an_error_message_json_when_invalid_id.yml +44 -0
  24. data/test/cassettes/Mbta_StopsRequest/_by_route/returns_an_error_message_json_when_missing_the_id.yml +44 -0
  25. data/test/cassettes/Mbta_StopsRequest/_by_route/returns_valid_json_when_passed_a_proper_id.yml +193 -0
  26. data/test/cassettes/Mbta_StopsRequest/_by_route_returns_valid_json.yml +193 -0
  27. data/test/cassettes/Mbta_StopsRequest/_by_route_returns_valid_json_when_passed_a_proper_id.yml +193 -0
  28. data/test/predictions_request_test.rb +9 -0
  29. data/test/stops_request_test.rb +45 -0
  30. data/test/test_helper.rb +21 -0
  31. data/test/vehicles_request_test.rb +32 -0
  32. metadata +214 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f083321e45d765cb822b1b088fd89e3e0a8afd59
4
+ data.tar.gz: 22c35f18765697e7e79c660e46c97da407dd5611
5
+ SHA512:
6
+ metadata.gz: ac640cd12ba2cd7ee4291794ecbdafcfef86d979938cd321c490b400ede831c7605322ca12d28605cfcab61ee6c210bdc06c54bfe3df53b47e0aa4084057ad51
7
+ data.tar.gz: 25cd133dcedaacdcf008216cfd21364b3e8798cbd1a61f04f5aec059130ab5240504bd11048db93943fde7412d9349a5775763534e0f0149c2f48e1cfdbf08f6
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mbta.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 phereford
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.
@@ -0,0 +1,74 @@
1
+ # Mbta
2
+ The mbta released a realtime api about a year ago. I have some
3
+ interest in building a hyperlocal app around public transportation
4
+ and have this created this api wrapper.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'mbta-rt'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install mbta
21
+
22
+ ## Usage
23
+ This is very much a WIP. The API is stable and won't be changing much.
24
+
25
+ In a configuration file add the following:
26
+ ```
27
+ require 'mbta'
28
+ Mbta.api_key = '<INSERT API_KEY>'
29
+ Mbta.format = 'json' # acceptable inputs are json, jsonp, and xml.
30
+ ```
31
+
32
+ Once you are properly setup, then it is just a matter of knowing
33
+ what data you want to get after.
34
+ ```
35
+ Mbta::RoutesRequest.all #=> returns a jsonified string of all requests
36
+ ```
37
+
38
+ I have built a wrapper around each of the endpoints for their v2
39
+ realtime api. Here is what the footprint looks like for each request:
40
+ ```
41
+ Mbta::RoutesRequest.all
42
+ Mbta::RoutesReqest.by_stop(stop_id)
43
+
44
+ Mbta::PredictionsRequest.by_stop(stop_id, options_hash)
45
+ Mbta::PredictionsRequest.by_route(route_id, options_hash)
46
+ Mbta::PredictionsRequest.by_trip(trip_id, options_hash)
47
+
48
+ Mbta::ScheduleRequest.by_stop(stop_id, options_hash)
49
+ Mbta::ScheduleRequest.by_route(route_id, options_hash)
50
+ Mbta::ScheduleRequest.by_trip(trip_id, options_hash)
51
+
52
+ Mbta::StopsRequest.by_route(route_id)
53
+ Mbta::StopsRequest.by_location(lat, long)
54
+
55
+ Mbta::VehiclesRequest.by_route(route_id, options_hash)
56
+ Mbta::VehiclesRequest.by_trip(tripe_id, options_hash)
57
+ ```
58
+
59
+ I will need to write up some documentation about what each
60
+ options_hash can take, but peeking at the documentation of
61
+ their [endpoint](http://realtime.mbta.com/Portal/Content/Documents/MBTA-realtime_APIDocumentation_v2_0_1_2014-09-08.pdf) may help guide you.
62
+
63
+ ## Left ToDo
64
+ 1) Encapsulate errors from the various endpoints into real Error objects
65
+ 2) Instead of just returning raw json, consider returning Ruby objects. Maybe a boolean staging return json or ruby objects.
66
+ 3) Build out ruby models with attrs for the various pieces of the json response.
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it ( https://github.com/[my-github-username]/mbta/fork )
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc 'Open an irb session preloaded with this library'
4
+ task :console do
5
+ sh 'irb -rubygems -I lib -r mbta.rb'
6
+ end
@@ -0,0 +1,15 @@
1
+ require 'mbta/version'
2
+ require 'mbta/config'
3
+ require 'mbta/connection'
4
+ require 'mbta/request'
5
+ require 'mbta/routes_request'
6
+ require 'mbta/stops_request'
7
+ require 'mbta/schedule_request'
8
+ require 'mbta/vehicles_request'
9
+ require 'mbta/predictions_request'
10
+
11
+ module Mbta
12
+ class << self
13
+ attr_accessor :api_key, :format
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Mbta
2
+ module Config
3
+ BASE_URL = 'http://realtime.mbta.com/developer/api/v2/'
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Mbta
5
+ class Connection
6
+ def self.connection
7
+ @connection = begin
8
+ Faraday.new(:url => Mbta::Config::BASE_URL, :params => { :api_key => Mbta.api_key, :format => Mbta.format }) do |c|
9
+ c.request :url_encoded
10
+ c.request :json
11
+ c.response :json
12
+ c.adapter Faraday.default_adapter
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Mbta
2
+ class PredictionsRequest < Request
3
+ class << self
4
+ [:stop, :route, :trip].each do |mode|
5
+ define_method("by_#{mode}") do |mode_id, options: {}|
6
+ hash = {mode => mode_id}.merge!(options)
7
+ get("predictionsby#{mode}", hash)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module Mbta
2
+ class Request < Connection
3
+ def self.get(path, options={})
4
+ request(:get, path, options)
5
+ end
6
+
7
+ private
8
+ def self.request(request_type, path, options)
9
+ response = connection.send(request_type, path) do |request|
10
+ options.each{ |k,v| request.params[k.to_s] = v }
11
+ end
12
+
13
+ response.body.to_json
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module Mbta
2
+ class RoutesRequest < Request
3
+ def self.all
4
+ get('routes')
5
+ end
6
+
7
+ def self.by_stop(stop_id)
8
+ get('routesbystop', { stop: stop_id })
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Mbta
2
+ class ScheduleRequest < Request
3
+ class << self
4
+ [:stop, :route, :trip].each do |mode|
5
+ define_method("by_#{mode}") do |mode_id, options: {}|
6
+ hash = {mode => mode_id}.merge!(options)
7
+ get("scheduleby#{mode}", hash)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Mbta
2
+ class StopsRequest < Request
3
+ class << self
4
+ def by_route(route_id)
5
+ get('stopsbyroute', { route: route_id })
6
+ end
7
+
8
+ def by_location(lat, long)
9
+ get('stopsbylocation', { lat: lat, lon: long })
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Mbta
2
+ class VehiclesRequest < Request
3
+ class << self
4
+ [:route, :trip].each do |mode|
5
+ define_method("by_#{mode}") do |mode_id, options: {}|
6
+ hash = { mode => mode_id }.merge!(options)
7
+ get("vehiclesby#{mode}", hash)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Mbta
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mbta/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mbta-rt'
8
+ spec.version = Mbta::VERSION
9
+ spec.authors = ['phereford']
10
+ spec.email = ['phereford@gmail.com']
11
+ spec.summary = %q{Ruby API Wrapper around MBTA realtime v2 api.}
12
+ spec.homepage = 'http://www.github.com/phereford/mbta'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'faraday', '~> 0.9.1'
21
+ spec.add_dependency 'faraday_middleware', '~> 0.9.1'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'minitest', '~> 5.7.0'
25
+ spec.add_development_dependency 'minitest-vcr', '~> 1.3.0'
26
+ spec.add_development_dependency 'pry-byebug', '~> 3.1.0'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'vcr', '~> 2.9.3'
29
+ spec.add_development_dependency 'webmock', '~> 1.21.0'
30
+ end
@@ -0,0 +1,44 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://realtime.mbta.com/developer/api/v2/predictionsbyroute?api_key=wX9NwuHnZU2ToO7GmGR9uw&format=json&route=52
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.1
12
+ Accept-Encoding:
13
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
14
+ Accept:
15
+ - '*/*'
16
+ response:
17
+ status:
18
+ code: 404
19
+ message: 'No data for '
20
+ headers:
21
+ Cache-Control:
22
+ - no-cache
23
+ Pragma:
24
+ - no-cache
25
+ Content-Length:
26
+ - '50'
27
+ Content-Type:
28
+ - application/json
29
+ Expires:
30
+ - '-1'
31
+ Server:
32
+ - Microsoft-IIS/7.5
33
+ X-Aspnet-Version:
34
+ - 4.0.30319
35
+ X-Powered-By:
36
+ - ASP.NET
37
+ Date:
38
+ - Sun, 05 Jul 2015 21:44:35 GMT
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{ "error": { "message": "No data for route 52" }}'
42
+ http_version:
43
+ recorded_at: Sun, 05 Jul 2015 21:44:36 GMT
44
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,44 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://realtime.mbta.com/developer/api/v2/stopsbylocation?api_key=wX9NwuHnZU2ToO7GmGR9uw&format=json&lat=&lon=-71.191223
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.1
12
+ Accept-Encoding:
13
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
14
+ Accept:
15
+ - '*/*'
16
+ response:
17
+ status:
18
+ code: 400
19
+ message: Invalid request parameter
20
+ headers:
21
+ Cache-Control:
22
+ - no-cache
23
+ Pragma:
24
+ - no-cache
25
+ Content-Length:
26
+ - '66'
27
+ Content-Type:
28
+ - application/json
29
+ Expires:
30
+ - '-1'
31
+ Server:
32
+ - Microsoft-IIS/7.5
33
+ X-Aspnet-Version:
34
+ - 4.0.30319
35
+ X-Powered-By:
36
+ - ASP.NET
37
+ Date:
38
+ - Sun, 05 Jul 2015 21:56:36 GMT
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{ "error": { "message": "Missing required query parameter: lat" }}'
42
+ http_version:
43
+ recorded_at: Sun, 05 Jul 2015 21:56:36 GMT
44
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,44 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://realtime.mbta.com/developer/api/v2/stopsbylocation?api_key=wX9NwuHnZU2ToO7GmGR9uw&format=json&lat=40&lon=
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.1
12
+ Accept-Encoding:
13
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
14
+ Accept:
15
+ - '*/*'
16
+ response:
17
+ status:
18
+ code: 400
19
+ message: Invalid request parameter
20
+ headers:
21
+ Cache-Control:
22
+ - no-cache
23
+ Pragma:
24
+ - no-cache
25
+ Content-Length:
26
+ - '66'
27
+ Content-Type:
28
+ - application/json
29
+ Expires:
30
+ - '-1'
31
+ Server:
32
+ - Microsoft-IIS/7.5
33
+ X-Aspnet-Version:
34
+ - 4.0.30319
35
+ X-Powered-By:
36
+ - ASP.NET
37
+ Date:
38
+ - Sun, 05 Jul 2015 21:58:46 GMT
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{ "error": { "message": "Missing required query parameter: lon" }}'
42
+ http_version:
43
+ recorded_at: Sun, 05 Jul 2015 21:58:47 GMT
44
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,58 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://realtime.mbta.com/developer/api/v2/stopsbylocation?api_key=wX9NwuHnZU2ToO7GmGR9uw&format=json&lat=42.363114&lon=-71.191223
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.1
12
+ Accept-Encoding:
13
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
14
+ Accept:
15
+ - '*/*'
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Cache-Control:
22
+ - no-cache
23
+ Pragma:
24
+ - no-cache
25
+ Content-Type:
26
+ - application/json; charset=utf-8
27
+ Expires:
28
+ - '-1'
29
+ Server:
30
+ - Microsoft-IIS/7.5
31
+ X-Aspnet-Version:
32
+ - 4.0.30319
33
+ X-Powered-By:
34
+ - ASP.NET
35
+ Date:
36
+ - Sun, 05 Jul 2015 21:56:35 GMT
37
+ Content-Length:
38
+ - '2699'
39
+ body:
40
+ encoding: UTF-8
41
+ string: '{"stop":[{"stop_id":"8181","stop_name":"Watertown St @ Fifth St","parent_station":"","parent_station_name":"","stop_lat":"42.363114","stop_lon":"-71.191223","distance":"0"},{"stop_id":"8176","stop_name":"Watertown
42
+ St @ Fifth Ave","parent_station":"","parent_station_name":"","stop_lat":"42.362933","stop_lon":"-71.191427","distance":"0.0162406731396914"},{"stop_id":"8175","stop_name":"Watertown
43
+ St @ Morse St","parent_station":"","parent_station_name":"","stop_lat":"42.362644","stop_lon":"-71.19201","distance":"0.0515512116253376"},{"stop_id":"8182","stop_name":"Watertown
44
+ St opp Morse St","parent_station":"","parent_station_name":"","stop_lat":"42.362688","stop_lon":"-71.192143","distance":"0.0555802248418331"},{"stop_id":"8180","stop_name":"Watertown
45
+ St opp Aldrich Rd","parent_station":"","parent_station_name":"","stop_lat":"42.364094","stop_lon":"-71.189091","distance":"0.127980828285217"},{"stop_id":"8174","stop_name":"Watertown
46
+ St @ Pond St","parent_station":"","parent_station_name":"","stop_lat":"42.361881","stop_lon":"-71.194156","distance":"0.172004327178001"},{"stop_id":"8183","stop_name":"Watertown
47
+ St opp Pond St","parent_station":"","parent_station_name":"","stop_lat":"42.36191","stop_lon":"-71.194485","distance":"0.186133340001106"},{"stop_id":"8177","stop_name":"Watertown
48
+ St opp California St","parent_station":"","parent_station_name":"","stop_lat":"42.364349","stop_lon":"-71.187436","distance":"0.211104184389114"},{"stop_id":"8173","stop_name":"Watertown
49
+ St @ Washburn St","parent_station":"","parent_station_name":"","stop_lat":"42.361668","stop_lon":"-71.195503","distance":"0.240127876400948"},{"stop_id":"8284","stop_name":"Watertown
50
+ St @ Galen St","parent_station":"","parent_station_name":"","stop_lat":"42.364331","stop_lon":"-71.186377","distance":"0.261084020137787"},{"stop_id":"8179","stop_name":"Watertown
51
+ St @ Galen St","parent_station":"","parent_station_name":"","stop_lat":"42.364483","stop_lon":"-71.186401","distance":"0.263572841882706"},{"stop_id":"900","stop_name":"Watertown
52
+ Yard","parent_station":"","parent_station_name":"","stop_lat":"42.364307","stop_lon":"-71.185703","distance":"0.29365000128746"},{"stop_id":"8185","stop_name":"Watertown
53
+ St opp Pearl St","parent_station":"","parent_station_name":"","stop_lat":"42.361534","stop_lon":"-71.19681","distance":"0.305113464593887"},{"stop_id":"8172","stop_name":"Watertown
54
+ St @ Pearl St","parent_station":"","parent_station_name":"","stop_lat":"42.361395","stop_lon":"-71.197011","distance":"0.31836062669754"},{"stop_id":"8178","stop_name":"Watertown
55
+ Sq Terminal","parent_station":"","parent_station_name":"","stop_lat":"42.365436","stop_lon":"-71.185502","distance":"0.333127826452255"}]}'
56
+ http_version:
57
+ recorded_at: Sun, 05 Jul 2015 21:56:36 GMT
58
+ recorded_with: VCR 2.9.3