Sha256: 78b9014879fbb5339cdc5bbf4808078872fcb43ffeff77820f7f7896e34b6cd6

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# -*- coding: utf-8 -*-
require 'http'
require 'json'
require 'velibe/db/kv_store' # TODO: refactor injection?

module Velibe
  class ApiVelib

    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'

    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

   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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
velibe-0.2.3 lib/velibe/api_velib.rb
velibe-0.2.2 lib/velibe/api_velib.rb
velibe-0.2.1 lib/velibe/api_velib.rb