onestop-id-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2d5191c9603fa00e9600e4a20cb387edfcea70e
4
+ data.tar.gz: 4b2ef31690266190a6021b24fbae001473551e03
5
+ SHA512:
6
+ metadata.gz: 08cc15f088c52c6ead6c0d63632e8e373281e32ea53fda91a321bc63648efaafd397c4b85adbc2c0d99a4b7e3b01c9bdac325e9ec3f8e15cb93def9fcadd0f6f
7
+ data.tar.gz: 06ad685c168879320fbc66cc3518863134332953bdbba76570a7b625566a749d25ea5a3412621d970c9359c4d27d81a7e19a57ab622ab34c5d0d7c1e47eeb700
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Mapzen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ [![Circle CI](https://circleci.com/gh/transitland/onestop-id-ruby-client.svg?style=svg)](https://circleci.com/gh/transitland/onestop-id-ruby-client)
2
+
3
+ # onestop-id-(ruby)-client
4
+
5
+ TODO: Write a gem description
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'onestop-id-client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install onestop-id-client
22
+
23
+ ## Usage
24
+
25
+ - `ENV['ONESTOP_ID_REGISTRY_REMOTE_URL']`
26
+ - `ENV['ONESTOP_ID_REGISTRY_LOCAL_PATH']`
@@ -0,0 +1,42 @@
1
+ require_relative '../registry'
2
+
3
+ require 'json'
4
+
5
+ module OnestopIdClient
6
+ module Entities
7
+ class BaseEntity
8
+ def initialize(directory, onestop_id: nil, json_blob: nil)
9
+ if onestop_id
10
+ begin
11
+ json_file = File.open(File.join(directory, "#{onestop_id}.json"), 'r')
12
+ parsed_json = JSON.parse(json_file.read)
13
+ rescue Errno::ENOENT
14
+ raise StandardError.new("no JSON file found with a Onestop ID of #{onestop_id}")
15
+ end
16
+ elsif json_blob
17
+ parsed_json = JSON.parse(json_blob)
18
+ else
19
+ raise ArgumentError.new('provide a Onestop ID or a JSON blob')
20
+ end
21
+
22
+ map_from_json_properties_to_object_variables(parsed_json)
23
+
24
+ @parsed_json = parsed_json
25
+
26
+ self
27
+ end
28
+
29
+ def self.all(directory, force_reload: false)
30
+ if force_reload || !defined?(@entity_objects)
31
+ @entity_objects = []
32
+ files = Dir.glob(File.join(directory, '*.json'))
33
+ files.each do |file_path|
34
+ json_file = File.open(file_path, 'r')
35
+ @entity_objects << new(json_blob: json_file.read)
36
+ end
37
+ end
38
+ @entity_objects
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,50 @@
1
+ require_relative 'base_entity'
2
+ require_relative 'operator_in_feed'
3
+
4
+ module OnestopIdClient
5
+ module Entities
6
+ class Feed < BaseEntity
7
+ DIRECTORY = File.join(OnestopIdClient::Registry::LOCAL_PATH, 'feeds')
8
+
9
+ attr_accessor :onestop_id, :url, :feed_format, :tags, :operators_in_feed
10
+
11
+ def initialize(onestop_id: nil, json_blob: nil)
12
+ super(DIRECTORY, onestop_id: onestop_id, json_blob: json_blob)
13
+ @operators_in_feed = create_operators_in_feed(@parsed_json['operatorsInFeed'])
14
+ self
15
+ end
16
+
17
+ def self.find_by(gtfs_data_exchange_id: nil)
18
+ if gtfs_data_exchange_id
19
+ all.find { |feed| feed.tags['gtfs_data_exchange_id'] == gtfs_data_exchange_id }
20
+ else
21
+ raise ArgumentError.new('must specify a GTFS Data Exchange ID')
22
+ end
23
+ end
24
+
25
+ def self.all(force_reload: false)
26
+ super(DIRECTORY, force_reload: force_reload)
27
+ end
28
+
29
+ private
30
+
31
+ def create_operators_in_feed(parsed_json_array)
32
+ array = parsed_json_array.map do |parsed_json_object|
33
+ OperatorInFeed.new(feed: self, operator_onestop_id: parsed_json_object['onestopId'], gtfs_agency_id: parsed_json_object['gtfsAgencyId'])
34
+ end
35
+ array
36
+ end
37
+
38
+ def map_from_json_properties_to_object_variables(parsed_json)
39
+ [
40
+ ['@onestop_id', 'onestopId'],
41
+ ['@url', 'url'],
42
+ ['@feed_format', 'feedFormat'],
43
+ ['@tags', 'tags'],
44
+ ].each do |mapping|
45
+ instance_variable_set(mapping[0], parsed_json[mapping[1]])
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'base_entity'
2
+
3
+ module OnestopIdClient
4
+ module Entities
5
+ class Operator < BaseEntity
6
+ DIRECTORY = File.join(OnestopIdClient::Registry::LOCAL_PATH, 'operators')
7
+
8
+ attr_accessor :onestop_id, :name, :tags, :identifiers, :geometry
9
+
10
+ def initialize(onestop_id: nil, json_blob: nil)
11
+ super(DIRECTORY, onestop_id: onestop_id, json_blob: json_blob)
12
+ self
13
+ end
14
+
15
+ def self.find_by(us_ntd_id: nil)
16
+ if us_ntd_id
17
+ all.find { |operator| operator.tags['us_national_transit_database_id'] == us_ntd_id }
18
+ else
19
+ raise ArgumentError.new('must specify a US NTD ID')
20
+ end
21
+ end
22
+
23
+ def self.all(force_reload: false)
24
+ super(DIRECTORY, force_reload: force_reload)
25
+ end
26
+
27
+ private
28
+
29
+ def map_from_json_properties_to_object_variables(parsed_json)
30
+ [
31
+ ['@onestop_id', 'onestopId'],
32
+ ['@name', 'name'],
33
+ ['@tags', 'tags'],
34
+ ['@identifiers', 'identifiers'],
35
+ ['@geometry', 'geometry']
36
+ ].each do |mapping|
37
+ instance_variable_set(mapping[0], parsed_json[mapping[1]])
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'base_entity'
2
+ require_relative 'feed'
3
+ require_relative 'operator'
4
+
5
+ module OnestopIdClient
6
+ module Entities
7
+ class OperatorInFeed
8
+ attr_accessor :gtfs_agency_id, :operator_onestop_id, :feed_onestop_id, :operator, :feed
9
+
10
+ def initialize(gtfs_agency_id: nil, operator_onestop_id: nil, feed_onestop_id: nil, operator: nil, feed: nil)
11
+ if operator
12
+ @operator = operator
13
+ elsif operator_onestop_id
14
+ @operator = Operator.new(onestop_id: operator_onestop_id)
15
+ else
16
+ raise ArgumentError.new('an operator object or Onestop ID must be specified')
17
+ end
18
+
19
+ if feed
20
+ @feed = feed
21
+ elsif feed_onestop_id
22
+ @feed = Feed.new(onestop_id: feed_onestop_id)
23
+ else
24
+ raise ArgumentError.new('a feed object or Onestop ID must be specified')
25
+ end
26
+
27
+ if gtfs_agency_id
28
+ @gtfs_agency_id = gtfs_agency_id
29
+ elsif feed
30
+ # TODO: handle this?
31
+ end
32
+
33
+ self
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'base_entity'
2
+
3
+ module OnestopIdClient
4
+ module Entities
5
+ class Route < BaseEntity
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'base_entity'
2
+
3
+ module OnestopIdClient
4
+ module Entities
5
+ class Stop < BaseEntity
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,99 @@
1
+ module OnestopIdClient
2
+ class OnestopId
3
+ ENTITY_TO_PREFIX = {
4
+ 'stop' => 's',
5
+ 'operator' => 'o',
6
+ 'feed' => 'f',
7
+ 'route' => 'r'
8
+ }
9
+ PREFIX_TO_ENTITY = ENTITY_TO_PREFIX.invert
10
+ COMPONENT_SEPARATOR = '-'
11
+
12
+ attr_accessor :entity_prefix, :geohash, :name
13
+
14
+ def initialize(string: nil, entity_prefix: nil, geohash: nil, name: nil)
15
+ errors = []
16
+
17
+ if string && string.length > 0
18
+ is_a_valid_onestop_id, errors = OnestopId.validate_onestop_id_string(string)
19
+ if is_a_valid_onestop_id
20
+ @entity_prefix = string.split(COMPONENT_SEPARATOR)[0]
21
+ @geohash = string.split(COMPONENT_SEPARATOR)[1]
22
+ @name = string.split(COMPONENT_SEPARATOR)[2]
23
+ self
24
+ else
25
+ raise ArgumentError.new(errors.join(', '))
26
+ end
27
+ elsif entity_prefix && geohash && name
28
+ if OnestopId.valid_component?(:entity_prefix, entity_prefix)
29
+ @entity_prefix = entity_prefix
30
+ else
31
+ errors << 'invalid entity prefix'
32
+ end
33
+ if OnestopId.valid_component?(:geohash, geohash)
34
+ @geohash = geohash
35
+ else
36
+ errors << 'invalid geohash'
37
+ end
38
+ if OnestopId.valid_component?(:name, name)
39
+ @name = name
40
+ else
41
+ errors << 'invalid name'
42
+ end
43
+ else
44
+ errors << 'either a string or entity/geohash/name must be specified'
45
+ end
46
+
47
+ if errors.length > 0
48
+ raise ArgumentError.new(errors.join(', '))
49
+ else
50
+ self
51
+ end
52
+ end
53
+
54
+ def self.validate_onestop_id_string(onestop_id, expected_entity_type: nil)
55
+ errors = []
56
+ is_a_valid_onestop_id = true
57
+
58
+ if onestop_id.split(COMPONENT_SEPARATOR).length != 3
59
+ errors << 'must include 3 components separated by hyphens ("-")'
60
+ is_a_valid_onestop_id = false
61
+ end
62
+
63
+ if expected_entity_type && onestop_id.split(COMPONENT_SEPARATOR)[0] != ENTITY_TO_PREFIX[expected_entity_type]
64
+ errors << "must start with \"#{ENTITY_TO_PREFIX[expected_entity_type]}\" as its 1st component"
65
+ is_a_valid_onestop_id = false
66
+ elsif expected_entity_type == nil && !valid_component?(:entity_prefix, onestop_id.split(COMPONENT_SEPARATOR)[0])
67
+ errors << "must start with \"#{ENTITY_TO_PREFIX.values.join(' or ')}\" as its 1st component"
68
+ is_a_valid_onestop_id = false
69
+ end
70
+
71
+ if onestop_id.split(COMPONENT_SEPARATOR)[1].length == 0 || !valid_component?(:geohash, onestop_id.split(COMPONENT_SEPARATOR)[1])
72
+ errors << 'must include a valid geohash as its 2nd component'
73
+ is_a_valid_onestop_id = false
74
+ end
75
+
76
+ if !valid_component?(:name, onestop_id.split(COMPONENT_SEPARATOR)[2])
77
+ errors << 'must include only letters and digits in its abbreviated name (the 3rd component)'
78
+ is_a_valid_onestop_id = false
79
+ end
80
+ return is_a_valid_onestop_id, errors
81
+ end
82
+
83
+ private
84
+
85
+ def self.valid_component?(component, value)
86
+ return false if !value || value.length == 0
87
+ case component
88
+ when :entity_prefix
89
+ ENTITY_TO_PREFIX.values.include?(value)
90
+ when :geohash
91
+ !(value =~ /[^a-z\d]/)
92
+ when :name
93
+ !(value =~ /[^a-zA-Z\d]/)
94
+ else
95
+ false
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,32 @@
1
+ require 'git'
2
+
3
+ module OnestopIdClient
4
+ class Registry
5
+ include Singleton
6
+
7
+ REMOTE_URL = ENV['ONESTOP_ID_REGISTRY_REMOTE_URL'] || 'git@github.com:transitland/onestop-id-registry.git'
8
+ LOCAL_PATH = ENV['ONESTOP_ID_REGISTRY_LOCAL_PATH'] || File.join(__dir__, '..', '..','tmp', 'onestop-id-registry')
9
+
10
+ def self.repo(force_update: false)
11
+ if !defined?(@repo) || force_update
12
+ begin
13
+ @repo = Git.open(LOCAL_PATH)
14
+ @repo.pull if force_update
15
+ rescue ArgumentError => error
16
+ if error.message == 'path does not exist'
17
+ @repo = Git.clone(REMOTE_URL, LOCAL_PATH)
18
+ end
19
+ end
20
+ end
21
+
22
+ # TODO: remove when this is complete: https://github.com/transitland/onestop-id-registry/pull/12
23
+ @repo.branch("remotes/origin/extract-ruby").checkout
24
+
25
+ @repo
26
+ end
27
+
28
+ def self.json_files_for_entity(entity)
29
+ Dir[File.join(LOCAL_PATH, entity, '**', '*.json')]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module OnestopIdClient
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ Gem.find_files("onestop_id_client/**/*.rb").each { |path| require path }
2
+
3
+ module OnestopIdClient
4
+ end
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onestop-id-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Drew Dara-Abrams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-07 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'
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: git
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.20'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.20'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.10'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.10'
125
+ - !ruby/object:Gem::Dependency
126
+ name: byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.5'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-byebug
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.0'
153
+ description: ''
154
+ email:
155
+ - drew@mapzen.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - LICENSE.txt
161
+ - README.md
162
+ - lib/onestop_id_client.rb
163
+ - lib/onestop_id_client/entities/base_entity.rb
164
+ - lib/onestop_id_client/entities/feed.rb
165
+ - lib/onestop_id_client/entities/operator.rb
166
+ - lib/onestop_id_client/entities/operator_in_feed.rb
167
+ - lib/onestop_id_client/entities/route.rb
168
+ - lib/onestop_id_client/entities/stop.rb
169
+ - lib/onestop_id_client/onestop_id.rb
170
+ - lib/onestop_id_client/registry.rb
171
+ - lib/onestop_id_client/version.rb
172
+ homepage: https://github.com/transitland/onestop-id-ruby-client
173
+ licenses:
174
+ - MIT
175
+ metadata: {}
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - "~>"
183
+ - !ruby/object:Gem::Version
184
+ version: '2.0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.2.2
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: Read and write public-transit identities to the Onestop ID Registry/
196
+ test_files: []
197
+ has_rdoc: