Class: Velibe::ApiVelib

Inherits:
Object
  • Object
show all
Defined in:
lib/velibe/api_velib.rb

Constant Summary collapse

API_KEY =
ENV['VELIBE_TOKEN'] || Velibe::KvStore.token || raise('No token provided')
API_HOST =
'https://api.jcdecaux.com'
API_PARAM =
{ contract: 'paris', apiKey: API_KEY }
API_BASE_URI =
'/vls/v1/stations'

Class Method Summary collapse

Class Method Details

.get_station(station_number) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/velibe/api_velib.rb', line 15

def self.get_station(station_number)
  uri = "#{API_HOST}#{API_BASE_URI}/#{station_number}"
  response = HTTP.get(uri, params: API_PARAM)

  if response.code == 200
    data = JSON.parse(response, symbolize_names: true)
    return StationStatus.from_hash(data)
  else
    nil # TODO: maybe raise error! (and handle 404, and no internet)
  end
end

.get_stations(stations, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/velibe/api_velib.rb', line 27

def self.get_stations(stations, &block)
  result = []
  HTTP.persistent(API_HOST) do |http|
    stations.each do |station_number|
      response = http.get("#{API_BASE_URI}/#{station_number}", params: API_PARAM)
      result << ( block ? block.call(response) : response)
    end
  end
  result
end