class SpBus::Scrapers::Locations

Constants

URL

Attributes

buses[R]

Public Class Methods

new(destination_id) click to toggle source
# File lib/spbus/scrapers/locations.rb, line 8
def initialize(destination_id)
  @destination_id = destination_id
end

Public Instance Methods

fetch() click to toggle source
# File lib/spbus/scrapers/locations.rb, line 12
def fetch
  doc = SpBus::Request.new(url_with_params).get
  @json = JSON.parse(doc, symbolize_names: true)

  validate_response

  @buses = @json[:vs].collect do |hash|
    build_bus(hash)
  end

  true
end

Private Instance Methods

build_bus(hash) click to toggle source
# File lib/spbus/scrapers/locations.rb, line 40
def build_bus(hash)
  bus = SpBus::Bus.new
  bus.latitude = hash[:py]
  bus.longitude = hash[:px]
  bus
end
url_with_params() click to toggle source
# File lib/spbus/scrapers/locations.rb, line 27
def url_with_params
  params = URI.encode_www_form(codigoLinha: @destination_id)
  "#{URL}?#{params}"
end
validate_response() click to toggle source
# File lib/spbus/scrapers/locations.rb, line 32
def validate_response
  if @json.nil? ||
     @json[:hr].nil? ||
     @json[:hr].empty?
    raise SpBus::UnknownResponse
  end
end