Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from bus-detective/scheduled-departures
Scheduled departures
- Loading branch information
Showing
6 changed files
with
64 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,44 @@ | ||
class DepartureFetcher | ||
include ActiveModel::SerializerSupport | ||
|
||
attr_reader :agency, :stop, :time | ||
def initialize(agency, stop, time, params = {}) | ||
attr_reader :agency, :stop, :start_time, :end_time | ||
def initialize(agency:, stop:, start_time:, end_time:) | ||
@agency = agency | ||
@time = time.in_time_zone | ||
@stop = stop | ||
@start_time = start_time | ||
@end_time = end_time | ||
end | ||
|
||
def departures | ||
@departures ||= stop_times.map { |stop_time| | ||
Departure.new(stop_time: stop_time, stop_time_update: nil) | ||
}.sort_by(&:time) | ||
}.sort_by(&:time).select { |d| active?(d) } | ||
end | ||
|
||
def stop_times | ||
@stop_times ||= begin | ||
agency.calculated_stop_times | ||
.where(stop: stop) | ||
.between(start_time, end_time) | ||
.between(query_start_time, query_end_time) | ||
.preload(:stop, :trip, :route) | ||
end | ||
end | ||
|
||
def valid? | ||
@agency.present? && @stop.present? | ||
def query_start_time | ||
start_time | ||
end | ||
|
||
protected | ||
def query_end_time | ||
end_time | ||
end | ||
|
||
def start_time | ||
@time - 10.minutes | ||
def valid? | ||
@agency.present? && @stop.present? | ||
end | ||
|
||
def end_time | ||
@time + 1.hour | ||
private | ||
|
||
def active?(departure) | ||
true | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters