Skip to content

Instantly share code, notes, and snippets.

@xcobar

xcobar/cli.rb Secret

Last active March 25, 2019 00:51
Show Gist options
  • Save xcobar/92deb10cdbbb2f9e2664e5ab27ff602a to your computer and use it in GitHub Desktop.
Save xcobar/92deb10cdbbb2f9e2664e5ab27ff602a to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'readline'
require './loco'
@banner = File.read("./banner.txt")
puts @banner
@rows = []
def table_header
@table = Terminal::Table.new headings: ['', 'Search Result 1', 'Search Result 2', 'Search Result 3'], rows: @rows
puts @table
end
def reset_array
@rows = []
end
def show_ids
reset_array
@rows << ['ID'] + Loco.new.display_ids
table_header
end
def show_train_names
reset_array
@rows << ['Train Names', Loco.new.train_names(0), Loco.new.train_names(1), Loco.new.train_names(2)]
table_header
end
def show_start_finish
reset_array
table_row = [
'Starcv gt/Finish',
Loco.new.start_finish_stations,
Loco.new.start_finish_stations(1),
Loco.new.start_finish_stations(2)
]
@rows << table_row
table_header
end
def show_departure_arrival_date
reset_array
table_row = [
'Departure/Arrival(Date)', Loco.new.depature_arrival_date_time('date'),
Loco.new.depature_arrival_date_time('date', 1),
Loco.new.depature_arrival_date_time('date', 2)
]
@rows << table_row
table_header
end
def show_departure_arrival_time
reset_array
table_row = [
'Departure/Arrival(Time)',
Loco.new.depature_arrival_date_time('time'),
Loco.new.depature_arrival_date_time('time', 1),
Loco.new.depature_arrival_date_time('time', 2)
]
@rows << table_row
table_header
end
def show_connecation_duration
reset_array
table_row = [
'Duration(each connection)',
Loco.new.duration_of_each_connection,
Loco.new.duration_of_each_connection(1),
Loco.new.duration_of_each_connection(2)
]
@rows << table_row
table_header
end
def show_journey_duration
reset_array
table_row = [
'Duration(whole journey)',
Loco.new.duration_of_whole_journey,
Loco.new.duration_of_whole_journey(1),
Loco.new.duration_of_whole_journey(2)
]
@rows << table_row
table_header
end
def show_number_of_changes
reset_array
table_row = [
'Number of changes',
Loco.new.number_of_changes,
Loco.new.number_of_changes(1),
Loco.new.number_of_changes(2)
]
@rows << table_row
table_header
end
def show_time_between_changes
reset_array
table_row = [
'Time between changes',
Loco.new.time_between_changes,
Loco.new.time_between_changes(1),
Loco.new.time_between_changes(2)
]
@rows << table_row
table_header
end
def exit_cli
sleep 0.4
puts 'Enjoy the rest of your day today! Bye...'
sleep 0.4
exit
end
while line = Readline.readline('> ', true)
case line
when 'st', 'show-table'
Loco.new.show_table
when 'si', 'show-ids'
show_ids
when 'stn', 'show-train-names'
show_train_names
when 'stf', 'show-start-finish'
show_start_finish
when 'sdad', 'show-departure-arrival-time-date' # fix
show_departure_arrival_date
when 'sdat', 'show-departure-arrival-time' # fix
show_departure_arrival_time
when 'scd', 'show-connection-duration'
show_connecation_duration
when 'sjd', 'show-journey-duration'
show_journey_duration
when 'snc', 'show-number-changes'
show_number_of_changes
when 'stbc', 'show-time-beween-changes'
show_time_between_changes
when 'clear'
system('clear')
puts @banner
when 'bye', 'exit'
exit_cli
else
puts 'Please only use commands listed.'
end
end
# frozen_string_literal: true
require 'nokogiri'
require 'time'
require 'terminal-table'
class String
def cut(position)
(self[0, position]).to_s
end
end
class Loco
def root(path, file_name = 'search.xml')
File.open(file_name) do |file|
Nokogiri::XML(file).xpath('//' + path)
end
end
def display_ids
id_list = []
root('ID').each do |id|
id_list << id.text
end
id_list
end
def train_names(result = 0)
train_names = []
root('Connections')[result].xpath('./Connection/TrainName').each do |train_name|
train_names << train_name.text
end
train_names.join(', ')
end
def start_finish_stations(result = 0)
start = []
finish = []
root('Connections')[result].xpath('./Connection/Start').each do |start_city|
start << start_city.text
end
root('Connections')[result].xpath('Connection/Finish').each do |finish_city|
finish << finish_city.text
end
start.zip(finish).map! do |start_finish|
start_finish.map! do |word|
word.cut(9)
end
end.map! { |word| word.join('/') }.join(', ')
end
def depature_arrival_date_time(what, result = 0)
@what = \
case what
when 'date'
'%d.%b.%y'
when 'time'
'%H:%M'
else
raise "Please choose between 'date' and 'time'"
end
departure = []
arrival = []
root('Connections')[result].xpath('./Connection/DepartureTime').each do |depature_time|
departure << depature_time.text
end
root('Connections')[result].xpath('Connection/ArrivalTime').each do |arrival_time|
arrival << arrival_time.text
end
departure.zip(arrival).map do |depart_arrive|
depart_arrive.map do |time|
Time.iso8601(time)
Time.at(Time.iso8601(time)).strftime(@what)
end
end.map { |time| time.join('-') }.join(', ')
end
def duration_of_whole_journey(result = 0)
start_time = root('Connections')[result].xpath('./Connection/DepartureTime').first
finish_time = root('Connections')[result].xpath('./Connection').last.xpath('./ArrivalTime')
total_duration = Time.iso8601(finish_time.text) - Time.iso8601(start_time.text)
Time.at(total_duration).strftime('%H:%M')
end
def number_of_changes(result = 0)
root('Connections')[result].xpath('./Connection').count
end
def fare_name_and_price(result = 0)
@fare_name = []
@fare_price = []
root('SearchResults/SearchResult')[result].xpath('./Connections/Connection/Fares').each do |row|
row.xpath('./Fare/Name').each do |fare|
@fare_name << fare.text
end
row.xpath('./Fare/Price/Value').each do |value|
@fare_price << value.text
end
end
@connections_fare_hash = []
@fare_name.zip(@fare_price).each_slice(2) do |slice|
@connections_fare_hash << slice
end
@connections_fare_hash
end
def cheapest_route
prices = []
number_of_search_results = root('SearchResults/SearchResult').count
(0...number_of_search_results).each do |search_result|
list_of_fares = []
root('SearchResults/SearchResult')[search_result].xpath('./Connections/Connection/Fares/Fare/Price/Value').each do |price_value|
list_of_fares << price_value.text.to_i
end
prices << list_of_fares
end
# list = [363, 318, 190] This is total of all connections in search result
list = []
prices.map do |array_of_prices|
array_of_prices.inject(:+)
end.each_with_index do |total_fare, ndx|
hush = {}
hush[ndx] = total_fare
list << hush
end
key_for_cheapest_routes = list.min_by(&:values).keys[0]
root('SearchResults/SearchResult')[key_for_cheapest_routes].xpath('./ID').text
end
def quickest_route
root('SearchResults').each do |result_set|
res_hash = {}
result_set.xpath('./SearchResult').each_with_index do |search_result, ndx|
dep_time = Time.iso8601(search_result.xpath('./Connections/Connection').first.xpath('./DepartureTime').text)
arr_time = Time.iso8601(search_result.xpath('./Connections/Connection').last.xpath('./ArrivalTime').text)
time_difference = arr_time - dep_time
res_hash[ndx] = time_difference
end
# Index of quickest search result
@quickest_search_result_id = res_hash.min_by { |_key, value| value }[0]
end
root('SearchResults/SearchResult/ID')[@quickest_search_result_id].text
end
def time_between_changes(result = 0)
depature_times = []
arrival_times = []
root('SearchResults/SearchResult')[result].xpath('./Connections').each do |connection|
connection.xpath('./Connection/DepartureTime').each do |departure_time|
depature_times << departure_time.text
end
connection.xpath('./Connection/ArrivalTime').each do |arrival_time|
arrival_times << arrival_time.text
end
end
depature_times.zip(arrival_times).each_cons(2).map do |(_, arrival), (departure, _)|
time_difference = Time.iso8601(departure) - Time.iso8601(arrival)
Time.at(time_difference).utc.strftime('%H:%M')
end
end
def duration_of_each_connection(result = 0)
departure = []
arrival = []
array_for_connection_duration = []
total_time_in_seconds = []
root('SearchResults/SearchResult')[result].xpath('./Connections').each do |connection|
connection.xpath('./Connection/DepartureTime').each do |departure_time|
departure << departure_time.text
end
connection.xpath('./Connection/ArrivalTime').each do |arrival_time|
arrival << arrival_time.text
end
end
arrival.zip(departure).flatten.each do |time|
array_for_connection_duration << Time.iso8601(time)
end
array_for_connection_duration.each_slice(2) { |a, b| total_time_in_seconds << [a - b] }
total_time_in_seconds.map! do |duration|
Time.at(duration.first).utc.strftime('%H:%M')
end
end
def show_table
rows = []
rows << ['ID'] + display_ids
rows << ['Train Names', train_names(0), train_names(1), train_names(2)]
rows << ['Start/Finish', start_finish_stations, start_finish_stations(1), start_finish_stations(2)]
rows << ['Departure/Arrival(Date)', depature_arrival_date_time('date'), depature_arrival_date_time('date', 1), depature_arrival_date_time('date', 2)]
rows << ['Departure/Arrival(Time)', depature_arrival_date_time('time'), depature_arrival_date_time('time', 1), depature_arrival_date_time('time', 2)]
rows << ['Duration(each connection)', duration_of_each_connection, duration_of_each_connection(1), duration_of_each_connection(2)]
rows << ['Duration(whole journey)', duration_of_whole_journey, duration_of_whole_journey(1), duration_of_whole_journey(2)]
rows << ['Number of changes', number_of_changes, number_of_changes(1), number_of_changes(2)]
rows << ['Time between changes', time_between_changes, time_between_changes(1), time_between_changes(2)]
# Make separate fare type/price data, Fill this data in with #fare_name_and_price
table = Terminal::Table.new headings: ['', 'Search Result 1', 'Search Result 2', 'Search Result 3'], rows: rows
puts table
puts
puts "ID of SearchResult with cheapest route: #{cheapest_route}"
puts "ID of SearchResult with quickest route: #{quickest_route}"
puts
end
end
# Loco.new.show_table
<?xml version="1.0" encoding="UTF-8"?>
<SearchResults>
<SearchResult>
<ID>F4S1DS</ID>
<Connections>
<Connection>
<Start>London St Pancras International</Start>
<Finish>Paris Nord</Finish>
<DepartureTime>2015-07-11T09:23:00+01:00</DepartureTime>
<ArrivalTime>2015-07-11T12:41:00+02:00</ArrivalTime>
<TrainName>Eurostar</TrainName>
<Fares>
<Fare>
<Name>Standard Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>79.00</Value>
</Price>
</Fare>
<Fare>
<Name>Standard Premier</Name>
<Price>
<Currency>GBP</Currency>
<Value>159.00</Value>
</Price>
</Fare>
</Fares>
</Connection>
<Connection>
<Start>Paris Lyon</Start>
<Finish>Barcelona Sants</Finish>
<DepartureTime>2015-07-11T13:56:00+02:00</DepartureTime>
<ArrivalTime>2015-07-11T20:17:00+02:00</ArrivalTime>
<TrainName>TGV 2N2</TrainName>
<Fares>
<Fare>
<Name>Standard Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>50.00</Value>
</Price>
</Fare>
<Fare>
<Name>First Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>75.00</Value>
</Price>
</Fare>
</Fares>
</Connection>
</Connections>
</SearchResult>
<SearchResult>
<ID>L2FSF4</ID>
<Connections>
<Connection>
<Start>London St Pancras International</Start>
<Finish>Paris Nord</Finish>
<DepartureTime>2015-07-11T11:31:00+01:00</DepartureTime>
<ArrivalTime>2015-07-11T14:48:00+02:00</ArrivalTime>
<TrainName>Eurostar</TrainName>
<Fares>
<Fare>
<Name>Standard Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>69.00</Value>
</Price>
</Fare>
<Fare>
<Name>Standard Premier</Name>
<Price>
<Currency>GBP</Currency>
<Value>179.00</Value>
</Price>
</Fare>
</Fares>
</Connection>
<Connection>
<Start>Paris Lyon</Start>
<Finish>Barcelona Sants</Finish>
<DepartureTime>2015-07-11T15:32:00+02:00</DepartureTime>
<ArrivalTime>2015-07-11T22:16:00+02:00</ArrivalTime>
<TrainName>TGV 2N2</TrainName>
<Fares>
<Fare>
<Name>Standard Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>25.50</Value>
</Price>
</Fare>
<Fare>
<Name>First Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>45.50</Value>
</Price>
</Fare>
</Fares>
</Connection>
</Connections>
</SearchResult>
<SearchResult>
<ID>D42GVV</ID>
<Connections>
<Connection>
<Start>London St Pancras International</Start>
<Finish>Paris Nord</Finish>
<DepartureTime>2015-07-11T15:48:00+01:00</DepartureTime>
<ArrivalTime>2015-07-11T19:03:00+02:00</ArrivalTime>
<TrainName>Eurostar</TrainName>
<Fares>
<Fare>
<Name>Standard Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>39.00</Value>
</Price>
</Fare>
<Fare>
<Name>Standard Premier</Name>
<Price>
<Currency>GBP</Currency>
<Value>59.00</Value>
</Price>
</Fare>
</Fares>
</Connection>
<Connection>
<Start>Paris Lyon</Start>
<Finish>Perpignan</Finish>
<DepartureTime>2015-07-11T22:25:00+02:00</DepartureTime>
<ArrivalTime>2015-07-12T06:48:00+02:00</ArrivalTime>
<TrainName>Intercités de Nuit</TrainName>
<Fares>
<Fare>
<Name>Standard Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>15.20</Value>
</Price>
</Fare>
<Fare>
<Name>First Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>27.00</Value>
</Price>
</Fare>
</Fares>
</Connection>
<Connection>
<Start>Perpignan</Start>
<Finish>Barcelona Sants</Finish>
<DepartureTime>2015-07-12T09:57:00+02:00</DepartureTime>
<ArrivalTime>2015-07-12T11:19:00+02:00</ArrivalTime>
<TrainName>AVE Class 100</TrainName>
<Fares>
<Fare>
<Name>Standard Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>20.00</Value>
</Price>
</Fare>
<Fare>
<Name>First Class</Name>
<Price>
<Currency>GBP</Currency>
<Value>30.00</Value>
</Price>
</Fare>
</Fares>
</Connection>
</Connections>
</SearchResult>
</SearchResults>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment