station_master 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32b9f5de932077df843d30f145c23fb5c9887f13
4
- data.tar.gz: 67709ee0e31c63b17c00197e1e0894f51d5c54f4
3
+ metadata.gz: bc125df126dd70a4a1c9fd1a9213e08b7f8e3721
4
+ data.tar.gz: 8d971d2032bd8bd7bb5954912611989575ebcd13
5
5
  SHA512:
6
- metadata.gz: b84730ab2ce3f46adf37bfd9702021a340b7bc41015d9a4e59290e68eb7402708bc2c962f5c5d6f585f0b2a948f675960134fc3a68c86687185477f3151e357f
7
- data.tar.gz: e4d36ac384e938e1935fe0938bf1f14a75fea998d3cff0f11977861fab02c5a0c68acb3d685ac5fe8710f6f51c914127b1b3fa373e93bcee41d625a39aafe36e
6
+ metadata.gz: 600a3941dc4dd1fd23122426d8099ebee652b790c24da24901b7223254ed9cfa19f24d9b744f1fc9bd9b51252409c370c74a92032d02b878dd4c2dd6d1943ecc
7
+ data.tar.gz: c99fa293d78e951f1273adbcfd1a7ec656c81dbe356c08c60a97c767a384ed10eee7972cf4f626c4914e297cf462a3e5a6b7999724f62bf3d3db1a8a31e9718d
data/README.md CHANGED
@@ -51,6 +51,10 @@ require minutes!).
51
51
 
52
52
  ## Changelog
53
53
 
54
+ ### 0.0.9
55
+
56
+ - Refactor code, better code organization.
57
+
54
58
  ### 0.0.8
55
59
 
56
60
  - Bug fix arrivals and departures initializers.
@@ -0,0 +1,27 @@
1
+ module StationMaster
2
+ module Schedule
3
+ class Arrival
4
+ attr_reader :train_code, :train_type, :origin, :platform, :time, :delay
5
+
6
+ def initialize(hash)
7
+ @train_code = hash[:numeroTreno]
8
+ @train_type = hash[:categoria]
9
+ @origin = hash[:origine]
10
+ @platform = (hash[:binarioProgrammatoArrivoDescrizione].to_s || '0').strip
11
+ @time = Time.at((hash[:orarioArrivo] || 0) / 1000)
12
+ @delay = hash[:ritardo]
13
+ end
14
+
15
+ def to_hash
16
+ {
17
+ train_code: train_code,
18
+ train_type: train_type,
19
+ origin: origin,
20
+ platform: platform,
21
+ time: time.strftime('%H:%M'),
22
+ delay: delay
23
+ }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module StationMaster
2
+ module Schedule
3
+ class Departure
4
+ attr_reader :train_code, :train_type, :destination, :platform, :time, :delay
5
+
6
+ def initialize(hash)
7
+ @train_code = hash[:numeroTreno]
8
+ @train_type = hash[:categoria]
9
+ @destination = hash[:destinazione]
10
+ @platform = (hash[:binarioProgrammatoPartenzaDescrizione].to_s || '0').strip
11
+ @time = Time.at((hash[:orarioPartenza] || 0) / 1000)
12
+ @delay = hash[:ritardo]
13
+ end
14
+
15
+ def to_hash
16
+ {
17
+ train_code: train_code,
18
+ train_type: train_type,
19
+ destination: destination,
20
+ platform: platform,
21
+ time: time.strftime('%H:%M'),
22
+ delay: delay
23
+ }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ module StationMaster
2
+ module Schedule
3
+ module Find
4
+ include Ask
5
+
6
+ def find_station_arrivals(station_code, time = Time.now)
7
+ MultiJson::load(remote_call(:arrivals, { station_code: station_code, time: format_time(time) }), symbolize_keys: true).inject([]) do |array, arrival_hash|
8
+ array << Arrival.new(arrival_hash)
9
+ end
10
+ end
11
+
12
+ def find_station_departures(station_code, time = Time.now)
13
+ MultiJson::load(remote_call(:departures, { station_code: station_code, time: format_time(time) }), symbolize_keys: true).inject([]) do |array, arrival_hash|
14
+ array << Departure.new(arrival_hash)
15
+ end
16
+ end
17
+
18
+ protected
19
+ def remote_call(resource, options = {})
20
+ request_url = case resource
21
+ when :arrivals then
22
+ STATION_MASTER_BASE_URL + "arrivi/#{options[:station_code]}/#{options[:time]}"
23
+ when :departures then
24
+ STATION_MASTER_BASE_URL + "partenze/#{options[:station_code]}/#{options[:time]}"
25
+ else
26
+ nil
27
+ end
28
+
29
+ ask!(request_url) if request_url
30
+ end
31
+
32
+ def format_time(time)
33
+ time.strftime('%a %b %d %Y %H:%M:%S GMT%z (CET)')
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,85 +1,10 @@
1
- module StationMaster
2
- class Schedule
3
- class << self
4
- include Ask
5
-
6
- def find_station_arrivals(station_code, time = Time.now)
7
- MultiJson::load(remote_call(:arrivals, { station_code: station_code, time: format_time(time) }), symbolize_keys: true).inject([]) do |array, arrival_hash|
8
- array << Arrival.new(arrival_hash)
9
- end
10
- end
11
-
12
- def find_station_departures(station_code, time = Time.now)
13
- MultiJson::load(remote_call(:departures, { station_code: station_code, time: format_time(time) }), symbolize_keys: true).inject([]) do |array, arrival_hash|
14
- array << Departure.new(arrival_hash)
15
- end
16
- end
17
-
18
- protected
19
- def remote_call(resource, options = {})
20
- request_url = case resource
21
- when :arrivals then
22
- STATION_MASTER_BASE_URL + "arrivi/#{options[:station_code]}/#{options[:time]}"
23
- when :departures then
24
- STATION_MASTER_BASE_URL + "partenze/#{options[:station_code]}/#{options[:time]}"
25
- else
26
- nil
27
- end
28
-
29
- ask!(request_url) if request_url
30
- end
31
-
32
- def format_time(time)
33
- time.strftime('%a %b %d %Y %H:%M:%S GMT%z (CET)')
34
- end
35
- end
1
+ require 'station_master/schedule/find'
2
+ require 'station_master/schedule/arrival'
3
+ require 'station_master/schedule/departure'
36
4
 
37
- class Arrival
38
- attr_reader :train_code, :train_type, :origin, :platform, :time, :delay
39
-
40
- def initialize(hash)
41
- @train_code = hash[:numeroTreno]
42
- @train_type = hash[:categoria]
43
- @origin = hash[:origine]
44
- @platform = (hash[:binarioProgrammatoArrivoDescrizione].to_s || '0').strip
45
- @time = Time.at((hash[:orarioArrivo] || 0) / 1000)
46
- @delay = hash[:ritardo]
47
- end
48
-
49
- def to_hash
50
- {
51
- train_code: train_code,
52
- train_type: train_type,
53
- origin: origin,
54
- platform: platform,
55
- time: time.strftime('%H:%M'),
56
- delay: delay
57
- }
58
- end
59
- end
60
-
61
- class Departure
62
- attr_reader :train_code, :train_type, :destination, :platform, :time, :delay
63
-
64
- def initialize(hash)
65
- @train_code = hash[:numeroTreno]
66
- @train_type = hash[:categoria]
67
- @destination = hash[:destinazione]
68
- @platform = (hash[:binarioProgrammatoPartenzaDescrizione].to_s || '0').strip
69
- @time = Time.at((hash[:orarioPartenza] || 0) / 1000)
70
- @delay = hash[:ritardo]
71
- end
5
+ module StationMaster
6
+ module Schedule
7
+ extend Find
72
8
 
73
- def to_hash
74
- {
75
- train_code: train_code,
76
- train_type: train_type,
77
- destination: destination,
78
- platform: platform,
79
- time: time.strftime('%H:%M'),
80
- delay: delay
81
- }
82
- end
83
- end
84
9
  end
85
10
  end
@@ -0,0 +1,58 @@
1
+ module StationMaster
2
+ class Station
3
+ module Find
4
+ include Ask
5
+
6
+ def all
7
+ stations_array = []
8
+ index = 0
9
+ begin
10
+ response = MultiJson::load(remote_call(:stations, page: index), symbolize_keys: true).inject([]) do |array, station|
11
+ station_code = station[:codiceStazione]
12
+ latitude = station[:lat]
13
+ longitude = station[:lon]
14
+ region_id = find_region_id_by_code(station[:codiceStazione])
15
+ station_details = find_details(station_code, region_id)
16
+ location = (station_details[:localita][:nomeLungo] || '').split.map(&:capitalize).join(' ') if station_details
17
+
18
+ array << Station.new(station_code, latitude, longitude, region_id, location)
19
+ end
20
+ stations_array += response
21
+ index += 1
22
+ end while response.any?
23
+
24
+ stations_array
25
+ end
26
+
27
+ def find_by_city(city)
28
+ remote_call(:search, { city: city }).split(/\n/).map(&:strip).inject({}) { |hash, entry| hash.merge!({ entry.split('|').first.to_sym => entry.split('|').last }) }
29
+ end
30
+
31
+ def find_region_id_by_code(station_code)
32
+ remote_call(:region_id, station_code: station_code)
33
+ end
34
+
35
+ def find_details(station_code, region_id)
36
+ MultiJson::load(remote_call(:station_details, station_code: station_code, region_id: region_id), symbolize_keys: true)
37
+ end
38
+
39
+ protected
40
+ def remote_call(resource, options = {})
41
+ request_url = case resource
42
+ when :stations then
43
+ STATION_MASTER_BASE_URL + "elencoStazioni/#{options[:page] || 0}"
44
+ when :region_id then
45
+ STATION_MASTER_BASE_URL + "regione/#{options[:station_code] || 0}"
46
+ when :station_details
47
+ STATION_MASTER_BASE_URL + "dettaglioStazione/#{options[:station_code] || 0}/#{options[:region_id] || 0}"
48
+ when :search
49
+ STATION_MASTER_BASE_URL + "autocompletaStazione/#{options[:city] || ''}"
50
+ else
51
+ nil
52
+ end
53
+
54
+ ask!(request_url) if request_url
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,61 +1,10 @@
1
+ require 'station_master/station/find'
2
+
1
3
  module StationMaster
2
4
  class Station
3
- attr_reader :station_code, :latitude, :longitude, :region_id, :location
4
-
5
- class << self
6
- include Ask
7
-
8
- def all
9
- stations_array = []
10
- index = 0
11
- begin
12
- response = MultiJson::load(remote_call(:stations, page: index), symbolize_keys: true).inject([]) do |array, station|
13
- station_code = station[:codiceStazione]
14
- latitude = station[:lat]
15
- longitude = station[:lon]
16
- region_id = find_region_id_by_code(station[:codiceStazione])
17
- station_details = find_details(station_code, region_id)
18
- location = (station_details[:localita][:nomeLungo] || '').split.map(&:capitalize).join(' ') if station_details
19
-
20
- array << Station.new(station_code, latitude, longitude, region_id, location)
21
- end
22
- stations_array += response
23
- index += 1
24
- end while response.any?
25
-
26
- stations_array
27
- end
5
+ extend Find
28
6
 
29
- def find_by_city(city)
30
- remote_call(:search, { city: city }).split(/\n/).map(&:strip).inject({}) { |hash, entry| hash.merge!({ entry.split('|').first.to_sym => entry.split('|').last }) }
31
- end
32
-
33
- def find_region_id_by_code(station_code)
34
- remote_call(:region_id, station_code: station_code)
35
- end
36
-
37
- def find_details(station_code, region_id)
38
- MultiJson::load(remote_call(:station_details, station_code: station_code, region_id: region_id), symbolize_keys: true)
39
- end
40
-
41
- protected
42
- def remote_call(resource, options = {})
43
- request_url = case resource
44
- when :stations then
45
- STATION_MASTER_BASE_URL + "elencoStazioni/#{options[:page] || 0}"
46
- when :region_id then
47
- STATION_MASTER_BASE_URL + "regione/#{options[:station_code] || 0}"
48
- when :station_details
49
- STATION_MASTER_BASE_URL + "dettaglioStazione/#{options[:station_code] || 0}/#{options[:region_id] || 0}"
50
- when :search
51
- STATION_MASTER_BASE_URL + "autocompletaStazione/#{options[:city] || ''}"
52
- else
53
- nil
54
- end
55
-
56
- ask!(request_url) if request_url
57
- end
58
- end
7
+ attr_reader :station_code, :latitude, :longitude, :region_id, :location
59
8
 
60
9
  def initialize(station_code, latitude, longitude, region_id, location)
61
10
  @station_code = station_code
@@ -1,3 +1,3 @@
1
1
  module StationMaster
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -8,115 +8,115 @@ describe StationMaster::Schedule do
8
8
  expect(schedule).to respond_to(:find_station_arrivals)
9
9
  expect(schedule.find_station_arrivals('S00219', Time.new(2014,11,10,22,35,26,'+01:00')).map(&:to_hash)).to eq([
10
10
  {
11
+ train_code: 24875,
12
+ train_type: "REG",
11
13
  origin: "SUSA",
12
14
  platform: "14",
13
15
  time: "22:15",
14
- train_code: 24875,
15
- train_type: "REG",
16
16
  delay: 0
17
17
  },
18
18
  {
19
+ train_code: 2530,
20
+ train_type: "REG",
19
21
  origin: "GENOVA BRIGNOLE",
20
22
  platform: "7",
21
23
  time: "22:30",
22
- train_code: 2530,
23
- train_type: "REG",
24
24
  delay: 7
25
25
  },
26
26
  {
27
+ train_code: 24832,
28
+ train_type: "REG",
27
29
  origin: "IVREA",
28
30
  platform: "18",
29
31
  time: "22:32",
30
- train_code: 24832,
31
- train_type: "REG",
32
32
  delay: 0
33
33
  },
34
34
  {
35
+ train_code: 10216,
36
+ train_type: "REG",
35
37
  origin: "CUNEO",
36
38
  platform: "3",
37
39
  time: "22:35",
38
- train_code: 10216,
39
- train_type: "REG",
40
40
  delay: 0
41
41
  },
42
42
  {
43
+ train_code: 9580,
44
+ train_type: "ES*",
43
45
  origin: "ROMA TERMINI",
44
46
  platform: "17",
45
47
  time: "22:40",
46
- train_code: 9580,
47
- train_type: "ES*",
48
48
  delay: 6
49
49
  },
50
50
  {
51
+ train_code: 10029,
52
+ train_type: "REG",
51
53
  origin: "BARDONECCHIA",
52
54
  platform: "15",
53
55
  time: "22:45",
54
- train_code: 10029,
55
- train_type: "REG",
56
56
  delay: 0
57
57
  },
58
58
  {
59
+ train_code: 9746,
60
+ train_type: "ES*",
59
61
  origin: "VENEZIA SANTA LUCIA",
60
62
  platform: "11",
61
63
  time: "22:50",
62
- train_code: 9746,
63
- train_type: "ES*",
64
64
  delay: 17
65
65
  },
66
66
  {
67
+ train_code: 2030,
68
+ train_type: "REG",
67
69
  origin: "MILANO CENTRALE",
68
70
  platform: "13",
69
71
  time: "23:07",
70
- train_code: 2030,
71
- train_type: "REG",
72
72
  delay: 0
73
73
  },
74
74
  {
75
+ train_code: 9654,
76
+ train_type: "ES*",
75
77
  origin: "ROMA TERMINI",
76
78
  platform: "16",
77
79
  time: "23:12",
78
- train_code: 9654,
79
- train_type: "ES*",
80
80
  delay: 8
81
81
  },
82
82
  {
83
+ train_code: 24877,
84
+ train_type: "REG",
83
85
  origin: "SUSA",
84
86
  platform: "4",
85
87
  time: "23:17",
86
- train_code: 24877,
87
- train_type: "REG",
88
88
  delay: 0
89
89
  },
90
90
  {
91
- origin: "SAVONA",
92
- platform: "0",
93
- time: "23:35",
94
91
  train_code: 33800,
95
92
  train_type: "REG",
93
+ origin: "SAVONA",
94
+ platform: "",
95
+ time: "23:35",
96
96
  delay: 0
97
97
  },
98
98
  {
99
+ train_code: 2532,
100
+ train_type: "REG",
99
101
  origin: "GENOVA BRIGNOLE",
100
102
  platform: "5",
101
103
  time: "23:40",
102
- train_code: 2532,
103
- train_type: "REG",
104
104
  delay: 0
105
105
  },
106
106
  {
107
+ train_code: 10031,
108
+ train_type: "REG",
107
109
  origin: "BARDONECCHIA",
108
110
  platform: "15",
109
111
  time: "23:45",
110
- train_code: 10031,
111
- train_type: "REG",
112
112
  delay: 0
113
113
  },
114
114
  {
115
+ train_code: 2032,
116
+ train_type: "REG",
115
117
  origin: "MILANO CENTRALE",
116
118
  platform: "13",
117
119
  time: "00:10",
118
- train_code: 2032,
119
- train_type: "REG",
120
120
  delay: 0
121
121
  }
122
122
  ])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: station_master
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Ordine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-14 00:00:00.000000000 Z
11
+ date: 2014-11-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem provide an interface to an api from www.viaggiatreno.it public
14
14
  site. This project exploits the api that the site uses to build up the front-end
@@ -29,7 +29,11 @@ files:
29
29
  - lib/station_master.rb
30
30
  - lib/station_master/ask.rb
31
31
  - lib/station_master/schedule.rb
32
+ - lib/station_master/schedule/arrival.rb
33
+ - lib/station_master/schedule/departure.rb
34
+ - lib/station_master/schedule/find.rb
32
35
  - lib/station_master/station.rb
36
+ - lib/station_master/station/find.rb
33
37
  - lib/station_master/version.rb
34
38
  - spec/schedule_spec.rb
35
39
  - spec/spec_helper.rb