Sha256: f53bf33cbfb3fb4cf6827d4c3fa7f16dc38ce7d18b1979b497561559b161dba7

Contents?: true

Size: 843 Bytes

Versions: 2

Compression:

Stored size: 843 Bytes

Contents

require 'httparty'
require 'hashie'

class BikesOffline < Exception; end

class Bikes
  include HTTParty

  STATIONS_URL  = 'https://api.jcdecaux.com/vls/v1/stations'
  CONTRACTS_URL = 'https://api.jcdecaux.com/vls/v1/contracts'

  def initialize(apikey, scheme)
    @api = apikey
    @scheme = scheme
  end

  def stations
    invoke(STATIONS_URL, { contract: @scheme})
  end

  def contracts
    invoke(CONTRACTS_URL)
  end

  def station(num)
    invoke(STATIONS_URL + '/' + num.to_s, {contract: @scheme})
  end

  private

  def invoke(url, queryx = {})
    response = self.class.get(url, { query: { apiKey: @api}.merge(queryx)})
    raise BikesOffline unless response.code == 200

    por = JSON.parse(response.body)
    if (por.is_a? Array)
      por.map{|ob| Hashie::Mash.new(ob)}
    else
      Hashie::Mash.new(por)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bikes-0.0.2 lib/bikes.rb
bikes-0.0.1 lib/bikes.rb