Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: working branch of features from @ianwdunlop #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions atco.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Gem::Specification.new do |s|
s.version = "0.0.2"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["David Rice"]
s.authors = ["David Rice", "Ian Dunlop"]
s.date = %q{2010-02-22}
s.description = %q{Simple and opinionated library for parsing ATCO-CIF files with Ruby.}
s.email = %q{[email protected]}
s.email = [%q{[email protected]}, %q{[email protected]}]
s.extra_rdoc_files = [
"README.mdown"
]
Expand All @@ -22,7 +22,12 @@ Gem::Specification.new do |s|
"lib/atco.rb",
"lib/atco/journey.rb",
"lib/atco/location.rb",
"lib/atco/stop.rb"
"lib/atco/stop.rb",
"lib/atco/z_location.rb",
"lib/atco/journey_times.rb",
"lib/atco/journey_route.rb",
"lib/atco/header.rb",
"lib/atco/operator.rb"
]
s.homepage = %q{http://github.com/davidjrice/atco}
s.rdoc_options = ["--charset=UTF-8"]
Expand Down
174 changes: 116 additions & 58 deletions lib/atco.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,91 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'open3'
require 'tempfile'
require 'atco/location'
require 'atco/journey'
require 'atco/stop'

require 'atco/journey_route'
require 'atco/journey_times'
require 'atco/z_location'
require 'atco/operator'
require 'atco/header'
require 'iconv'

module Atco
VERSION = '0.0.1'

class << self

@path = nil
@@methods = {
:bank_holiday => 'QH',
:operator => 'QP',
:additional_location_info => 'QB',
:location => 'QL',
:destination => 'QT',
:intermediate => 'QI',
:origin => 'QO',
:journey_header => 'QS'
}
class Parser

def parse(file)
@path = File.expand_path(file)
data = File.readlines(@path)
path = File.expand_path(file)
data = File.readlines(path)

objects = []
current_journey = nil
current_location = nil
locations = []
journeys = {}
# journeys = []
journey = nil
header = nil

z_journey = nil
operator = nil
journeys = nil
# locations = []
operators = []
gmpte_info=[]
first_line = data.first
header = Header.new(parse_header first_line)
data.each do |line|
if line == data.first
header = parse_header(line)
next
end
@@methods.each do |method,identifier|
object = self.send("parse_#{method}", line)
if object[:record_identity] && object[:record_identity] == identifier
current_journey = object if object[:record_identity] && object[:record_identity] == @@methods[:journey_header]
if object[:record_identity] && ( object[:record_identity] == @@methods[:location] || object[:record_identity] == @@methods[:additional_location_info] )
if object[:record_identity] == @@methods[:location]
current_location = object
else
locations << Location.new(current_location, object)
end
end

if current_journey
if journeys[current_journey[:unique_journey_identifier]]
journeys[current_journey[:unique_journey_identifier]].stops << Stop.new(object)
else
journeys[current_journey[:unique_journey_identifier]] = Journey.new(object)
end
end
objects << object
end
case line[0,2]
when 'QS'
journey = Journey.new(parse_journey_header line)
z_journey.journeys << journey
when 'QO'
journey.stops << Stop.new(parse_origin line)
when 'QP'
#operator
operator = Operator.new(parse_operator line)
operators << operator
when 'QQ'
#additional operator info
additional_operator_info = parse_additonal_operator_info line
operator.address = additional_operator_info[:address]
when 'QB'
additional_info = parse_additional_location_info(line)
location.easting = additonal_info[:grid_reference_easting]
location.northing = additonal_info[:grid_reference_northing]
location.short_code = additional_info[:location]
when 'QL'
location = Location.new(parse_location line)
locations << location
when 'QA'
#alternative location
when 'QH'
when 'QI'
journey.stops << Stop.new(parse_intermediate line)
when 'QT'
journey.stops << Stop.new(parse_destination line)
when 'QO'
when 'QR'
#repetition records
when 'ZD'
#this is a gmpte specific thing
#it's a new journey so start again
#the assumption is that the journey record is defined before the stops etc
# record_ended = false
z_locations=[]
z_journey = JourneyTimes.new(parse_journey_times line)
z_journey.z_locations=z_locations
z_journey.journey_identifiers = []
journeys = []
z_journey.journeys = journeys
gmpte_info << z_journey
when 'ZA'
#this is a gmpte specific thing
z_journey.z_locations << ZLocation.new(parse_stop_location_name line)
when 'ZS'
#this is a gmpte specific thing
z_journey.journey_route = JourneyRoute.new(parse_journey_route line)
end
end
return {:header => header, :locations => locations, :journeys => journeys}

return {:header => header, :locations => locations, :operators => operators, :journeys => journeys, :gmpte_info=>gmpte_info}
end

def parse_header(string)
Expand Down Expand Up @@ -94,7 +115,14 @@ def parse_operator(string)
:operator_legal_name => parse_value(string[31,48])
}
end


def parse_additonal_operator_info
{
:record_identity => string[0,2],
:address => string[77,3]
}
end

def parse_additional_location_info(string)
{
:record_identity => string[0,2],
Expand All @@ -104,7 +132,7 @@ def parse_additional_location_info(string)
:grid_reference_northing => parse_value(string[23,8])
}
end

def parse_location(string)
{
:record_identity => string[0,2],
Expand All @@ -114,7 +142,7 @@ def parse_location(string)
:gazetteer_code => string[63,1]
}
end

def parse_destination(string)
{
:record_identity => string[0,2],
Expand All @@ -125,7 +153,7 @@ def parse_destination(string)
:fare_stage_indicator => string[23,2]
}
end

def parse_intermediate(string)
{
:record_identity => string[0,2],
Expand All @@ -138,7 +166,7 @@ def parse_intermediate(string)
:fare_stage_indicator => string[28,2]
}
end

def parse_origin(string)
{
:record_identity => string[0,2],
Expand All @@ -149,7 +177,7 @@ def parse_origin(string)
:fare_stage_indicator => string[23,2]
}
end

#
def parse_journey_header(string)
{
:record_identity => string[0,2],
Expand All @@ -174,9 +202,39 @@ def parse_journey_header(string)
:route_direction => string[64,1]
}
end

#
def parse_value(value)
return value.strip if value
return value.strip if value
end

#GMPTE has this data
def parse_journey_times(string)
{
:record_identity => string[0,2],
:start_time => string[2,8],
:end_time => string[10,8],
:days_of_the_week_text => parse_value(string[18,48])
}
end

#GMPTE has this data
def parse_journey_route(string)
{
:record_identity => string[0,2],
:provider => string[2,8],
:route_name => parse_value(string[10,4]),
:route_text => parse_value(string[14,50])
}
end

#GMPTE has this data
def parse_stop_location_name(string)
{
:record_identity => string[0,2],
:type => string[2,1],
:identifier => parse_value(string[3,12]),
:name => parse_value(string[14,48])
}
end
end

Expand Down
18 changes: 18 additions & 0 deletions lib/atco/bank_hoilday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Atco

class BankHoliday
:record_identity => string[0,2],
:transaction_type => string[2,1],
:date_of_bank_holiday
attr_accessor :record_identity, :transaction_type, :date_of_bank_holiday
attr_writer :record_identity, :transaction_type, :date_of_bank_holiday

def initialize(data)
@record_identity = data[:record_identity]
@transaction_type = data[:transaction_type]
@date_of_bank_holiday = data[:date_of_bank_holiday]
end

end

end
17 changes: 17 additions & 0 deletions lib/atco/journey_route.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Atco

class JourneyRoute

attr_accessor :record_identity, :provider, :route_name, :route_text
attr_writer :record_identity, :provider, :route_name, :route_text

def initialize(data)
@record_identity = data[:record_identity]
@provider = data[:provider]
@route_name = data[:route_name]
@route_text = data[:route_text]
end

end

end
16 changes: 16 additions & 0 deletions lib/atco/journey_times.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Atco

class JourneyTimes

attr_accessor :journeys, :journey_identifiers, :record_identity, :start_time, :end_time, :days_of_the_week_text, :z_locations, :journey_route
attr_writer :journeys, :journey_identifiers, :record_identity, :start_time, :end_time, :days_of_the_week_text, :z_locations, :journey_route

def initialize(data)
@record_identity = data[:record_identity]
@start_time = data[:start_time]
@days_of_the_week_text = data[:days_of_the_week_text]
@z_locations = data[:z_locations]
@journey_route = data[:journey_route]
end
end
end
7 changes: 3 additions & 4 deletions lib/atco/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ module Atco

class Location

attr_accessor :name, :identifier, :easting, :northing, :gazeteer_code
attr_accessor :name, :identifier, :easting, :northing, :gazeteer_code, :short_code
attr_writer :name, :identifier, :easting, :northing, :gazeteer_code, :short_code

def initialize(location_header, additional_location_information)
def initialize(location_header)
@name = location_header[:full_location]
@identifier = location_header[:record_identity]
@easting = additional_location_information[:grid_reference_easting]
@northing = additional_location_information[:grid_reference_northing]
@gazeteer_code = location_header[:gazetteer_code]
end

Expand Down
18 changes: 18 additions & 0 deletions lib/atco/operator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Atco

class Operator

attr_accessor :record_identity, :transaction_type, :operator, :operator_short_form, :operator_legal_name, :address
attr_writer :record_identity, :transaction_type, :operator, :operator_short_form, :operator_legal_name, :address

def initialize(data)
@record_identity = data[:record_identity]
@transaction_type = data[:transaction_type]
@operator = data[:operator]
@operator_short_form = data[:operator_short_form]
@operator_legal_name = data[:operator_legal_name]
end

end

end
3 changes: 2 additions & 1 deletion lib/atco/stop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Atco

class Stop

attr_accessor :bay_number, :location, :timing_point_indicator, :fare_stage_indicator, :published_departure_time, :record_identity
attr_accessor :bay_number, :location, :timing_point_indicator, :fare_stage_indicator, :published_arrival_time, :published_departure_time, :record_identity

def origin?; @record_identity == "QO"; end
def intermediate?; @record_identity == "QI"; end
Expand All @@ -14,6 +14,7 @@ def initialize(data)
@timing_point_indicator = data[:timing_point_indicator]
@fare_stage_indicator = data[:fare_stage_indicator]
@published_departure_time = data[:published_departure_time]
@published_arrival_time = data[:published_arrival_time]
@record_identity = data[:record_identity]
end

Expand Down
Loading