Class: Bort::Util
- Inherits:
-
Object
- Object
- Bort::Util
- Defined in:
- lib/bort.rb
Constant Summary collapse
- VALID_AM_PM =
param validation
['am', 'pm', nil]
- VALID_DATE_STRING =
['today', 'now', nil]
Class Method Summary collapse
- .download(params) ⇒ Object
- .set_key(key) ⇒ Object
- .validate_date(date) ⇒ Object
- .validate_station(station) ⇒ Object
- .validate_time(time) ⇒ Object
Class Method Details
.download(params) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bort.rb', line 59 def self.download(params) params[:key] = @@api_key action = params.delete(:action) queries = params.delete_if{|k, v|v.nil?}.map{|k, v| "#{k}=#{v}" }.join('&') request = Net::HTTP::Get.new("/api/#{action}.aspx?#{queries}") response = Net::HTTP.start('api.bart.gov') do |http| http.request(request) end response.body end |
.set_key(key) ⇒ Object
55 56 57 |
# File 'lib/bort.rb', line 55 def self.set_key(key) @@api_key = key end |
.validate_date(date) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bort.rb', line 91 def self.validate_date(date) return if VALID_DATE_STRING.include?(date) mm, dd, yyyy = date.split('/').map(&:to_i) year = Time.now.year unless /^\d{1,2}\/\d{1,2}\/\d{4}$/ === date && (1..12) === mm && (1..31) === dd && (year-1..year+1) === yyyy raise InvalidDate.new(date) end end |
.validate_station(station) ⇒ Object
106 107 108 |
# File 'lib/bort.rb', line 106 def self.validate_station(station) raise InvalidStation.new(station) unless STATIONS.keys.map(&:to_s).include?(station.to_s.downcase) end |
.validate_time(time) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bort.rb', line 76 def self.validate_time(time) return unless time hm, ap = time.split('+') hh, mm = hm.split(':').map(&:to_i) unless /^\d{1,2}:\d{1,2}\+\w\w$/ === time && VALID_AM_PM.include?(ap.downcase) && (1..12) === hh && (0..59) === mm raise InvalidTime.new(time) end end |