Module: StationMaster::Station::Find

Includes:
Ask
Included in:
StationMaster::Station
Defined in:
lib/station_master/station/find.rb

Instance Method Summary collapse

Instance Method Details

#allObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/station_master/station/find.rb', line 6

def all
  stations_array = []
  index = 0
  begin
    response = MultiJson::load(remote_call(:stations, page: index), symbolize_keys: true).inject([]) do |array, station|
      station_code = station[:codiceStazione]
      latitude = station[:lat]
      longitude = station[:lon]
      region_id = find_region_id_by_code(station[:codiceStazione])
      station_details = find_details(station_code, region_id)
      location = (station_details[:localita][:nomeLungo] || '').split.map(&:capitalize).join(' ') if station_details

      array << Station.new(station_code, latitude, longitude, region_id, location)
    end
    stations_array += response
    index += 1
  end while response.any?

  stations_array
end

#find_by_city(city) ⇒ Object



27
28
29
# File 'lib/station_master/station/find.rb', line 27

def find_by_city(city)
  remote_call(:search, { city: city }).split(/\n/).map(&:strip).inject({}) { |hash, entry| hash.merge!({ entry.split('|').first.to_sym => entry.split('|').last }) }
end

#find_details(station_code, region_id) ⇒ Object



35
36
37
# File 'lib/station_master/station/find.rb', line 35

def find_details(station_code, region_id)
  MultiJson::load(remote_call(:station_details, station_code: station_code, region_id: region_id), symbolize_keys: true)
end

#find_region_id_by_code(station_code) ⇒ Object



31
32
33
# File 'lib/station_master/station/find.rb', line 31

def find_region_id_by_code(station_code)
  remote_call(:region_id, station_code: station_code)
end