sncf 0.1.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0ebb101165159e3c8179e93f18a4891e222b655
4
- data.tar.gz: 96b4e851249d3686a9dc2ae3a1c269512e29c3bc
3
+ metadata.gz: 543bad488723801d92911a47e70904a3d63b0fa1
4
+ data.tar.gz: 68db2da4c6a30470aa09128fcc27c348a18ae659
5
5
  SHA512:
6
- metadata.gz: a142907f567191d2d6828043feb4ec8f9cf172821368b4bf156c6b671d9d600413946c326063aea9f4642f3ec94dc3f88571feb40f3782447ba6edbc183543a2
7
- data.tar.gz: c2f5c562650e5a118f769dde5a5d7a2955518ce1ff736c2913f25ee89b888682903689b7abb91bafc65c238287645ae74c2c33047e0524c53bc8544f55ef5256
6
+ metadata.gz: 0290f41f58c8a1c97e19bff538e6436f66f8371dd407e32c0df22eff59755ed6759d67b65c6d445cd1ad42ba6ed3df87e938cfa724a716332ec145cd82d2f9c5
7
+ data.tar.gz: 3bc2960d5ffc40813b9ceb4aa68de1b18f6087f67ad5292aeea1900f3c26b98db0b7df941ebcc931da914514e36fbf7f01017ca639597a63d33e2269a2a90af5
@@ -42,13 +42,23 @@ module Sncf
42
42
  end
43
43
  end
44
44
 
45
+ def fetch_stations(place)
46
+ raise ArgumentError, "The fetch_place argument should be a string, example: 'Paris Nord'." if place.to_s == ''
47
+
48
+ stations_response = fetch('coverage/sncf/places', {
49
+ q: place
50
+ })
51
+
52
+ Sncf::Parsers::Stations.new(stations_response).get_stations_list
53
+ end
54
+
45
55
  protected
46
56
 
47
57
  def construct_formated_url(path, additional_params)
48
58
  if additional_params.nil?
49
59
  "#{BASE_URL}/#{VERSION_PATH}/#{path}"
50
60
  else
51
- "#{BASE_URL}/#{VERSION_PATH}/#{path}?#{additional_params.to_query}"
61
+ "#{BASE_URL}/#{VERSION_PATH}/#{path}?#{URI.encode_www_form(additional_params)}"
52
62
  end
53
63
  end
54
64
  end
@@ -0,0 +1,36 @@
1
+ module Sncf
2
+ module Models
3
+ MODEL_NAMES = %w[Station AdministrativeRegion]
4
+ MODEL_ATTRIBUTES = {
5
+ 'Station' => [:id, :coord, :quality, :name, :label, :timezone, :administrative_regions],
6
+ 'AdministrativeRegion' => [:insee, :level, :coord, :name, :label, :id, :zip_code]
7
+ }
8
+
9
+ def self.generate_models
10
+ Sncf::Models::MODEL_NAMES.each do |klass_name|
11
+ klass_vars = Sncf::Models::MODEL_ATTRIBUTES[klass_name.to_s]
12
+ klass = generate_class klass_vars
13
+
14
+ const_set klass_name, klass
15
+ end
16
+ end
17
+
18
+ def self.generate_class(klass_vars)
19
+ Class.new do
20
+ klass_vars.each do |field|
21
+ define_method field.intern do
22
+ instance_variable_get("@#{field}")
23
+ end
24
+ define_method "#{field}=".intern do |arg|
25
+ instance_variable_set("@#{field}", arg)
26
+ end
27
+ end
28
+ define_method :initialize do |args|
29
+ klass_vars.each do |field|
30
+ instance_variable_set("@#{field}", args[field])
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,15 @@
1
+ module Sncf
2
+ module Parsers
3
+ class Default
4
+ def initialize(api_response)
5
+ @api_response = api_response
6
+ end
7
+
8
+ protected
9
+
10
+ def create_model(model_name, attributes)
11
+ Object.const_get("Sncf::Models::#{model_name}").new attributes
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ module Sncf
2
+ module Parsers
3
+ class Stations < Sncf::Parsers::Default
4
+ def get_stations_list
5
+ stations, administrative_regions = [], []
6
+
7
+ @api_response.content['places'].each do |place|
8
+ if place['embedded_type'] == 'stop_area'
9
+ place['stop_area']['administrative_regions'].each do |administrative_region|
10
+ administrative_regions << create_model('AdministrativeRegion', {
11
+ id: administrative_region['id'],
12
+ insee: administrative_region['insee'],
13
+ level: administrative_region['level'],
14
+ coord: administrative_region['coord'],
15
+ name: administrative_region['name'],
16
+ label: administrative_region['label'],
17
+ zip_code: administrative_region['zip_code']
18
+ })
19
+ end
20
+
21
+ stations << create_model('Station', {
22
+ id: place['id'],
23
+ coord: place['stop_area']['coord'],
24
+ quality: place['quality'],
25
+ name: place['stop_area']['name'],
26
+ label: place['stop_area']['label'],
27
+ timezone: place['stop_area']['timezone'],
28
+ administrative_regions: administrative_regions
29
+ })
30
+ end
31
+ end
32
+
33
+ stations
34
+ end
35
+ end
36
+ end
37
+ end
data/lib/sncf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sncf
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/sncf.rb CHANGED
@@ -1,6 +1,11 @@
1
1
  require 'sncf/version'
2
2
  require 'sncf/api_client'
3
3
  require 'sncf/api_response'
4
+ require "sncf/models/generator"
5
+ require "sncf/parsers/default"
6
+ require "sncf/parsers/stations"
4
7
 
5
8
  module Sncf
6
9
  end
10
+
11
+ Sncf::Models.generate_models
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sncf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Delhaye
@@ -155,6 +155,9 @@ files:
155
155
  - lib/sncf.rb
156
156
  - lib/sncf/api_client.rb
157
157
  - lib/sncf/api_response.rb
158
+ - lib/sncf/models/generator.rb
159
+ - lib/sncf/parsers/default.rb
160
+ - lib/sncf/parsers/stations.rb
158
161
  - lib/sncf/version.rb
159
162
  - sncf.gemspec
160
163
  homepage: https://github.com/rdlh/sncf