bart 0.0.1

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.
@@ -0,0 +1,26 @@
1
+ require 'nokogiri'
2
+
3
+ module Bart
4
+ class Estimate
5
+
6
+ attr_reader :minutes, :platform, :direction, :length
7
+
8
+ def initialize(xml)
9
+ document = Nokogiri::XML.parse(xml)
10
+
11
+ @minutes = document.css('minutes').text.to_i
12
+ @platform = document.css('platform').text.to_i
13
+ @direction = document.css('direction').text
14
+ @length = document.css('length').text.to_i
15
+ end
16
+
17
+ def seconds
18
+ @minutes * 60
19
+ end
20
+
21
+ def time
22
+ Time.now + seconds
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ require 'nokogiri'
2
+ require 'bart/station'
3
+
4
+ module Bart
5
+ class Etd
6
+
7
+ attr_reader :destination, :estimates
8
+
9
+ def initialize(xml)
10
+ document = Nokogiri::XML.parse(xml)
11
+ abbr = document.css('abbreviation').text
12
+ estimates = document.css('estimate')
13
+
14
+ @destination = Station.new(abbr)
15
+ @estimates = estimates.inject([]) { |memo, i| memo << Estimate.new(i.to_s) }
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,73 @@
1
+ # http://api.bart.gov/docs/etd/etd.aspx uses abbr and abbreviation
2
+ # interchangeably. I'm settling on abbr to be both clear and concise.
3
+
4
+ require 'nokogiri'
5
+ require 'bart/etd'
6
+ require 'bart/station/list'
7
+
8
+ # CLEANUP
9
+ require 'net/http'
10
+ require 'bart/estimate'
11
+
12
+ module Bart
13
+
14
+ class Station
15
+
16
+ attr_reader :abbr
17
+ attr_accessor :departures
18
+
19
+ # DEBUG
20
+ attr_reader :document
21
+
22
+ # OPTIMIZE If we have one station object, we don't need to initialize
23
+ # new ones over and over again. We'll leave it alone for now, for
24
+ # simplicity.
25
+ def initialize(abbr)
26
+ @abbr = abbr.downcase
27
+ end
28
+
29
+ def self.from_xml(xml)
30
+ document = Nokogiri::XML.parse(xml)
31
+ abbr = document.css('abbr').text
32
+ departures = document.css('etd')
33
+
34
+ station = new(abbr)
35
+ station.departures = departures.inject([]) do |memo, i|
36
+ memo << Etd.new(i.to_s)
37
+ end
38
+
39
+ station
40
+ end
41
+
42
+ def name
43
+ ID_TO_NAME[abbr]
44
+ end
45
+
46
+ # fetch
47
+ def load_departures
48
+ params = {
49
+ :cmd => 'etd',
50
+ :orig => @abbr,
51
+ :key => 'MW9S-E7SL-26DU-VV8V'
52
+ }
53
+
54
+ query_string = '?' + params.map { |key, value| [key, value] * '=' } * '&'
55
+ ssan_etd = Net::HTTP::Get.new('/api/etd.aspx' + query_string )
56
+
57
+ response = Net::HTTP.start('api.bart.gov') do |http|
58
+ http.request(ssan_etd)
59
+ end
60
+
61
+ store_departures(response.body)
62
+ end
63
+
64
+ def store_departures(xml)
65
+ document = Nokogiri::XML.parse(xml)
66
+ @document = document
67
+ @departures = document.css('etd').inject([]) do |memo, i|
68
+ memo << Etd.new(i.to_s)
69
+ end
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,54 @@
1
+
2
+ module Bart
3
+ class Station
4
+
5
+ LIST = [
6
+ { :id => "12th", :name => "12th St. Oakland City Center" },
7
+ { :id => "16th", :name => "16th St. Mission (SF)" },
8
+ { :id => "19th", :name => "19th St. Oakland" },
9
+ { :id => "24th", :name => "24th St. Mission (SF)" },
10
+ { :id => "ashb", :name => "Ashby (Berkeley)" },
11
+ { :id => "balb", :name => "Balboa Park (SF)" },
12
+ { :id => "bayf", :name => "Bay Fair (San Leandro)" },
13
+ { :id => "cast", :name => "Castro Valley" },
14
+ { :id => "civc", :name => "Civic Center (SF)" },
15
+ { :id => "cols", :name => "Coliseum/Oakland Airport" },
16
+ { :id => "colm", :name => "Colma" },
17
+ { :id => "conc", :name => "Concord" },
18
+ { :id => "daly", :name => "Daly City" },
19
+ { :id => "dbrk", :name => "Downtown Berkeley" },
20
+ { :id => "dubl", :name => "Dublin/Pleasanton" },
21
+ { :id => "deln", :name => "El Cerrito del Norte" },
22
+ { :id => "plza", :name => "El Cerrito Plaza" },
23
+ { :id => "embr", :name => "Embarcadero (SF)" },
24
+ { :id => "frmt", :name => "Fremont" },
25
+ { :id => "ftvl", :name => "Fruitvale (Oakland)" },
26
+ { :id => "glen", :name => "Glen Park (SF)" },
27
+ { :id => "hayw", :name => "Hayward" },
28
+ { :id => "lafy", :name => "Lafayette" },
29
+ { :id => "lake", :name => "Lake Merritt (Oakland)" },
30
+ { :id => "mcar", :name => "MacArthur (Oakland)" },
31
+ { :id => "mlbr", :name => "Millbrae" },
32
+ { :id => "mont", :name => "Montgomery St. (SF)" },
33
+ { :id => "nbrk", :name => "North Berkeley" },
34
+ { :id => "ncon", :name => "North Concord/Martinez" },
35
+ { :id => "orin", :name => "Orinda" },
36
+ { :id => "pitt", :name => "Pittsburg/Bay Point" },
37
+ { :id => "phil", :name => "Pleasant Hill" },
38
+ { :id => "powl", :name => "Powell St. (SF)" },
39
+ { :id => "rich", :name => "Richmond" },
40
+ { :id => "rock", :name => "Rockridge (Oakland)" },
41
+ { :id => "sbrn", :name => "San Bruno" },
42
+ { :id => "sfia", :name => "San Francisco Int'l Airport" },
43
+ { :id => "sanl", :name => "San Leandro" },
44
+ { :id => "shay", :name => "South Hayward" },
45
+ { :id => "ssan", :name => "South San Francisco" },
46
+ { :id => "ucty", :name => "Union City" },
47
+ { :id => "wcrk", :name => "Walnut Creek" },
48
+ { :id => "woak", :name => "West Oakland" }
49
+ ]
50
+
51
+ ID_TO_NAME = LIST.inject({}) { |memo, i| memo[i[:id]] = i[:name]; memo }
52
+
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bart
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Josh Lubaway
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-13 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description:
34
+ email: josh.lubaway@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - lib/bart/estimate.rb
43
+ - lib/bart/etd.rb
44
+ - lib/bart/station/list.rb
45
+ - lib/bart/station.rb
46
+ has_rdoc: true
47
+ homepage: https://github.com/jish/bart
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A ruby wrapper around the BART API.
78
+ test_files: []
79
+