Skip to content

Instantly share code, notes, and snippets.

@jmmastey
Created December 2, 2014 16:28
Show Gist options
  • Save jmmastey/6b7bd54313203abaf69c to your computer and use it in GitHub Desktop.
Save jmmastey/6b7bd54313203abaf69c to your computer and use it in GitHub Desktop.
refactor attempt
class TripOptimizer
attr_accessor :from, :to, :meeting_start, :meeting_end, :meeting_length
def initialize(options = {})
@from = options[:from]
@to = options[:to]
@meeting_start = options[:meeting_start]
@meeting_length = options[:meeting_length]
@meeting_end = meeting_start + meeting_length.hours
end
def best_departure
all_departures && all_departures.min_by(&:origin_date_time)
end
alias_method :departure, :best_departure
def best_return
all_returns && all_returns.max_by(&:destination_date_time)
end
alias_method :return, :best_return
def departures
flightstats = schedule.get_flights_arriving_before(meeting_start, from, to)
flights_from(flightstats)
end
alias_method :all_departures, :departures
def returns
flightstats = schedule.get_flights_departing_after(meeting_end, from, to)
flights_from(flightstats)
end
alias_method :all_returns, :returns
private
def flights_from(stats)
stats.map { |f| Flight.new(mapper.flight_stats_to_flight_h(f)) }
end
def mapper
@mapper ||= FlightMapper.new
end
def schedule
@schedule ||= FlightStats::Schedule.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment