Sha256: 72a578a29b17cd81678659e7d9b347a060ff18891e6f7ba2663f211ae49f3832

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'httparty'
require 'time'

module PtvTimetable

  class API
    include HTTParty

    base_uri 'http://timetableapi.ptv.vic.gov.au'

    def initialize(devid, secret_key)
      @devid      = devid
      @secret_key = secret_key
    end

    def health_check(timestamp=Time.now.utc.iso8601)
      signed_request("/v2/healthcheck?timestamp=#{timestamp}")
    end

    def near_me(latitude, longitude)
      signed_request("/v2/nearme/latitude/#{latitude}/longitude/#{longitude}")
    end

    def points_of_interest(latitude1, longitude1, latitude2, longitude2)
      poi        = '0,1,2,3,4,100'
      grid_depth = 3
      limit      = 6
      signed_request("/v2/poi/#{poi}/lat1/#{latitude1}/long1/#{longitude1}/lat2/#{latitude2}/long2/#{latitude2}/griddepth/#{grid_depth}/limit/#{limit}")
    end

    def search(text)
      signed_request("/v2/search/#{text}")
    end

    def broad_next_departures(mode, stop, limit=5)
      signed_request("/v2/mode/#{mode}/stop/#{stop}/departures/by-destination/limit/#{limit}")
    end

    def specific_next_departures(mode, line, stop, direction, limit=5, utc=Time.now.utc.iso8601)
      signed_request("/v2/mode/#{mode}/line/#{line}/stop/#{stop}/directionid/#{direction}/departures/all/limit/#{limit}?for_utc=#{utc}")
    end

    def stopping_pattern(mode, run, stop, utc=Time.now.utc.iso8601)
      signed_request("/v2/mode/#{mode}/run/#{run}/stop/#{stop}/stopping-pattern?for_utc=#{utc}")
    end

    def line_stops(mode, line)
      signed_request("/v2/mode/#{mode}/line/#{line}/stops-for-line")
    end

    private

    def signed_request(request_path)
      request_path += (request_path.include?('?') ? '&' : '?') + "devid=#{@devid}"
      signature = Digest::HMAC.hexdigest(request_path, @secret_key, Digest::SHA1).upcase
      self.class.get("#{request_path}&signature=#{signature}").parsed_response
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ptv_timetable-0.0.1 lib/ptv_timetable/api.rb