gtfs-orm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/gtfs-orm.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "gtfs/orm/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gtfs-orm"
8
+ spec.version = GTFS::ORM::VERSION
9
+ spec.authors = ["Ryan Closner"]
10
+ spec.email = ["ryan@ryanclosner.com"]
11
+ spec.summary = %q{GTFS -> Ruby Object Mapper}
12
+ spec.description = %q{GTFS -> Ruby Object Mapper}
13
+ spec.homepage = "https://github.com/rclosner/gtfs-orm"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ runtime_dependencies = {
22
+ "virtus" => "~> 1.0.1"
23
+ }
24
+
25
+ runtime_dependencies.each {|lib, version| spec.add_runtime_dependency(lib, version) }
26
+
27
+ development_dependencies = {
28
+ "bundler" => "~> 1.5",
29
+ "rake" => "~> 10.1.1"
30
+ }
31
+
32
+ development_dependencies.each {|lib, version| spec.add_development_dependency(lib, version) }
33
+ end
@@ -0,0 +1,21 @@
1
+ module GTFS
2
+ module ORM
3
+ class Agency < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "agency.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :agency_id, String
12
+ attribute :agency_name, String
13
+ attribute :agency_url, String
14
+ attribute :agency_timezone, String
15
+ attribute :agency_lang, String
16
+ attribute :agency_phone, String
17
+ attribute :agency_fare_url, String
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module GTFS
2
+ module ORM
3
+ class Calendar < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "calendar.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :service_id, String
12
+ attribute :monday, Integer
13
+ attribute :tuesday, Integer
14
+ attribute :wednesday, Integer
15
+ attribute :thursday, Integer
16
+ attribute :friday, Integer
17
+ attribute :saturday, Integer
18
+ attribute :sunday, Integer
19
+ attribute :start_date, Date
20
+ attribute :end_date, Date
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module GTFS
2
+ module ORM
3
+ class CalendarDate < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "calendar_dates.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :service_id, String
12
+ attribute :date, Date
13
+ attribute :exception_type, Integer
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module GTFS
2
+ module ORM
3
+ class FareAttribute < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "fare_attributes.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :fare_id, String
12
+ attribute :price, Float
13
+ attribute :currency_type, String
14
+ attribute :payment_method, Integer
15
+ attribute :trqnsfers, Integer
16
+ attribute :transfer_duration, Integer
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ module GTFS
2
+ module ORM
3
+ class FareRule < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "fare_rules.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :fare_id, String
12
+ attribute :route_id, String
13
+ attribute :origin_id, String
14
+ attribute :destination_id, String
15
+ attribute :contains_id, String
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module GTFS
2
+ module ORM
3
+ class FeedInfo < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "feed_info.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :feed_publisher_name, String
12
+ attribute :feed_publisher_url, String
13
+ attribute :feed_lang, String
14
+ attribute :feed_start_date, Date
15
+ attribute :feed_end_date, Date
16
+ attribute :feed_version, String
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ module GTFS
2
+ module ORM
3
+ class Frequency < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "frequencies.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :trip_id, String
12
+ attribute :start_time, Time
13
+ attribute :end_time, Time
14
+ attribute :headway_seconds, Integer
15
+ attribute :exact_times, Integer
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,142 @@
1
+ module GTFS
2
+ module ORM
3
+ class Resource
4
+
5
+ # INCLUSIONS
6
+
7
+ include Virtus.model
8
+
9
+ # EXCEPTIONS
10
+
11
+ class NonExistentResourceError < Exception; end
12
+
13
+ # CLASS METHODS
14
+
15
+ def self.page(page)
16
+ Scope.new(page)
17
+ end
18
+
19
+ def self.first
20
+ Scope.new(collection).first
21
+ end
22
+
23
+ def self.last
24
+ Scope.new(collection).last
25
+ end
26
+
27
+ def self.limit(max)
28
+ Scope.new(collection).limit(max)
29
+ end
30
+
31
+ def self.all
32
+ Scope.new(collection).all
33
+ end
34
+
35
+ def self.where(conditions)
36
+ Scope.new(collection).where(conditions)
37
+ end
38
+
39
+ private
40
+
41
+ def self.base_path
42
+ ORM.path
43
+ end
44
+
45
+ def self.file_path
46
+ File.join(base_path, self::FILE_NAME)
47
+ end
48
+
49
+ def self.collection
50
+ unless @collection
51
+ raise NonExistentResourceError unless File.exist?(file_path)
52
+ @collection = []
53
+ CSV.read(file_path, headers: true).map do |row|
54
+ @collection << new( Hash[row.headers.zip(row.fields)] )
55
+ end
56
+ end
57
+ @collection
58
+ end
59
+
60
+ class Scope
61
+
62
+ # CONSTANTS
63
+
64
+ DEFAULT_LIMIT = 20.freeze
65
+ DEFAULT_PAGE = 1.freeze
66
+ DEFAULT_CONDITIONS = {}.freeze
67
+
68
+ # INITIALIZER
69
+
70
+ def initialize(collection)
71
+ @collection = collection
72
+ @limit = DEFAULT_LIMIT
73
+ @page = DEFAULT_PAGE
74
+ @conditions = DEFAULT_CONDITIONS
75
+ end
76
+
77
+ # INSTANCE METHODS
78
+
79
+ def limit(limit = nil)
80
+ if limit
81
+ @limit = limit
82
+ return self
83
+ else
84
+ @limit
85
+ end
86
+ end
87
+
88
+ def where(conditions = nil)
89
+ if conditions
90
+ @conditions = conditions
91
+ return self
92
+ else
93
+ @conditions
94
+ end
95
+ end
96
+
97
+ def page(page=nil)
98
+ if page
99
+ @page = page
100
+ return self
101
+ else
102
+ @page
103
+ end
104
+ end
105
+
106
+ def first
107
+ all.first
108
+ end
109
+
110
+ def last
111
+ all.last
112
+ end
113
+
114
+ def all
115
+ scoped_collection
116
+ end
117
+
118
+ private
119
+
120
+ attr_reader :collection, :conditions
121
+
122
+ def scoped_collection
123
+ arr = collection.select do |item|
124
+ match = true
125
+ if conditions
126
+ conditions.each_pair do |k,v|
127
+ if item.respond_to?(k)
128
+ match = false unless item.send(k) == v
129
+ end
130
+ end
131
+ end
132
+ match
133
+ end
134
+
135
+ arr[ ((page - 1) * limit)...(page * limit)] || []
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,23 @@
1
+ module GTFS
2
+ module ORM
3
+ class Route < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "routes.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :route_id, String
12
+ attribute :agency_id, String
13
+ attribute :route_short_name, String
14
+ attribute :route_long_name, String
15
+ attribute :route_desc, String
16
+ attribute :route_type, Integer
17
+ attribute :route_url, String
18
+ attribute :route_color, String
19
+ attribute :route_text_color, String
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ module GTFS
2
+ module ORM
3
+ class Shape < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "shapes.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :shape_id, String
12
+ attribute :shape_pt_lat, Float
13
+ attribute :shape_pt_lon, Float
14
+ attribute :shape_pt_sequence, Integer
15
+ attribute :shape_dist_traveled, Float
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ module GTFS
2
+ module ORM
3
+ class Stop < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "stops.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :stop_id, String
12
+ attribute :stop_code, String
13
+ attribute :stop_name, String
14
+ attribute :stop_desc, String
15
+ attribute :stop_lat, Float
16
+ attribute :stop_lon, Float
17
+ attribute :zone_id, String
18
+ attribute :stop_url, String
19
+ attribute :location_type, Integer
20
+ attribute :parent_station, String
21
+ attribute :stop_timezone, String
22
+ attribute :wheelchair_boarding, Integer
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ module GTFS
2
+ module ORM
3
+ class StopTime < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "stop_times.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :trip_id, Integer
12
+ attribute :arrival_time, Time
13
+ attribute :departure_time, Time
14
+ attribute :stop_id, String
15
+ attribute :stop_sequence, Integer
16
+ attribute :stop_headsign, String
17
+ attribute :pickup_type, Integer
18
+ attribute :drop_off_type, Integer
19
+ attribute :shape_dist_traveled, String
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ module GTFS
2
+ module ORM
3
+ class Transfer < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "transfers.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :from_stop_id, String
12
+ attribute :to_stop_id, String
13
+ attribute :transfer_type, Integer
14
+ attribute :min_transfer_time, Integer
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module GTFS
2
+ module ORM
3
+ class Trip < Resource
4
+
5
+ # CONSTANTS
6
+
7
+ FILE_NAME = "trips.txt".freeze
8
+
9
+ # ATTRIBUTES
10
+
11
+ attribute :route_id, String
12
+ attribute :service_id, Integer
13
+ attribute :trip_id, String
14
+ attribute :trip_headsign, String
15
+ attribute :trip_short_name, String
16
+ attribute :direction_id, Integer
17
+ attribute :block_id, Integer
18
+ attribute :shape_id, Integer
19
+ attribute :wheelchair_accessible, Boolean
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module GTFS
2
+ module ORM
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/gtfs/orm.rb ADDED
@@ -0,0 +1,43 @@
1
+ require "virtus"
2
+ require "csv"
3
+
4
+ module GTFS
5
+ module ORM
6
+
7
+ # EXCEPTIONS
8
+
9
+ class NoPathError < Exception; end
10
+
11
+ # CONSTANTS
12
+
13
+ DEFAULT_PATH = './data'.freeze
14
+
15
+ # CLASS METHODS
16
+
17
+ def self.path=(path)
18
+ @path
19
+ end
20
+
21
+ def self.path
22
+ @path ||= DEFAULT_PATH
23
+ end
24
+
25
+ end
26
+ end
27
+
28
+ require "gtfs/orm/version"
29
+ require "gtfs/orm/resource"
30
+
31
+ require "gtfs/orm/agency"
32
+ require "gtfs/orm/calendar"
33
+ require "gtfs/orm/calendar_date"
34
+ require "gtfs/orm/fare_attribute"
35
+ require "gtfs/orm/fare_rule"
36
+ require "gtfs/orm/feed_info"
37
+ require "gtfs/orm/frequency"
38
+ require "gtfs/orm/route"
39
+ require "gtfs/orm/shape"
40
+ require "gtfs/orm/stop"
41
+ require "gtfs/orm/stop_time"
42
+ require "gtfs/orm/transfer"
43
+ require "gtfs/orm/trip"
data/lib/gtfs.rb ADDED
@@ -0,0 +1,4 @@
1
+ module GTFS
2
+ end
3
+
4
+ require 'gtfs/orm'
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gtfs-orm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Closner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 10.1.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 10.1.1
55
+ description: GTFS -> Ruby Object Mapper
56
+ email:
57
+ - ryan@ryanclosner.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - data/agency.txt
68
+ - data/calendar.txt
69
+ - data/feed_info.txt
70
+ - data/frequencies.txt
71
+ - data/org_onebusaway_transit_data.log
72
+ - data/org_onebusaway_transit_data.properties
73
+ - data/org_onebusaway_transit_data.script
74
+ - data/routes.txt
75
+ - data/shapes.txt
76
+ - data/stop_times.txt
77
+ - data/stops.txt
78
+ - data/transfers.txt
79
+ - data/trips.txt
80
+ - gtfs-orm.gemspec
81
+ - lib/gtfs.rb
82
+ - lib/gtfs/orm.rb
83
+ - lib/gtfs/orm/agency.rb
84
+ - lib/gtfs/orm/calendar.rb
85
+ - lib/gtfs/orm/calendar_date.rb
86
+ - lib/gtfs/orm/fare_attribute.rb
87
+ - lib/gtfs/orm/fare_rule.rb
88
+ - lib/gtfs/orm/feed_info.rb
89
+ - lib/gtfs/orm/frequency.rb
90
+ - lib/gtfs/orm/resource.rb
91
+ - lib/gtfs/orm/route.rb
92
+ - lib/gtfs/orm/shape.rb
93
+ - lib/gtfs/orm/stop.rb
94
+ - lib/gtfs/orm/stop_time.rb
95
+ - lib/gtfs/orm/transfer.rb
96
+ - lib/gtfs/orm/trip.rb
97
+ - lib/gtfs/orm/version.rb
98
+ homepage: https://github.com/rclosner/gtfs-orm
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.0.14
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: GTFS -> Ruby Object Mapper
122
+ test_files: []