<commit_before>require "lta_data_mall/version"
require 'net/http'
require 'json'
require 'date'

module LtaDataMall
  class Configuration
    attr_accessor :account_key
  end

  def self.configuration
    if @configuration.nil?
      @configuration = Configuration.new
    end
    if block_given?
      yield @configuration
    else
      @configuration
    end
  end

  class HttpClient
    def self.call(url, headers)
    end

    def self.json(endpoint, parameters)
      uri = URI.parse(endpoint)
      uri.query = URI.encode_www_form(parameters)

      http = Net::HTTP.new(uri.host, uri.port)

      get_request = Net::HTTP::Get.new(uri.to_s)
      get_request['AccountKey'] = ::LtaDataMall.configuration.account_key

      http.start do |http|
	      response = http.request(get_request)

        return OpenStruct.new ({
          date: response['Date'],
          body: response.body
        })
      end
    end
  end

  class BusArrival
    class Stop
      attr_reader :response_datetime, :stop_code, :services

      def initialize(service_data_string, response_datetime)
        @response_datetime = DateTime.parse(response_datetime)
        service_data = JSON.parse(service_data_string)
        @stop_code = service_data['BusStopCode']

        @services = []
        service_data['Services'].each do |service|
          @services << Service.new(service)
        end
      end
    end

    class Service
      attr_reader :no, :operator, :arrivals

      OPERATOR_TYPES = {
        'SBST' => :sbst,
        'SMRT' => :smrt,
        'TTS'  => :tts,
        'GAS'  => :gas
      }

      def initialize(service_data)
        @no = service_data['ServiceNo']
        @operator = OPERATOR_TYPES[service_data['Operator']]

        @arrivals = []
        ['NextBus', 'NextBus2', 'NextBus3'].each do |next_bus_key|
          @arrivals << Bus.new(service_data[next_bus_key])
        end
      end
    end

    class Bus

      attr_reader :origin_code, :destination_code, :estimated_arrival,
        :latitude, :longitude, :visit_number, :load, :is_wab, :vehicle

      alias_method :is_wab?, :is_wab

        LOAD_TYPES = {
          'SEA' => :seats_available,
          'SDA' => :standing_available,
          'LSD' => :limited_standing
        }

        VEHICLE_TYPES = {
          'SD' => :single_deck,
          'DD' => :double_deck,
          'BD' => :articulated
        }

      def initialize(service_data)
        @origin_code = service_data['OriginCode']
        @destination_code = service_data['DestinationCode']
        begin
          @estimated_arrival = DateTime.parse(service_data['EstimatedArrival'])
        rescue
          @estimated_arrival = nil
        end
        @latitude = service_data['Latitude']
        @longitude = service_data['Longitude']
        @visit_number = service_data['VisitNumber'].to_i
        @load = LOAD_TYPES[service_data['Load']]
        @is_wab = service_data['Feature'] == 'WAB'
        @vehicle = VEHICLE_TYPES[service_data['Type']]
      end

      def has_seats?
        @load == :seats_available
      end
    end

    ENDPOINT_URL = 'http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2'

    def get_bus_arrival(bus_stop_code, bus_route = nil)
      parameters = { 'BusStopCode' => bus_stop_code.to_s }
      response = ::LtaDataMall::HttpClient.json(ENDPOINT_URL, parameters)

      Stop.new(response.body, response.date)
    end
  end
end<commit_msg>Add rdoc documentation Accept bus_route number<commit_after>require "lta_data_mall/version"
require 'net/http'
require 'json'
require 'date'

# An unofficial API Wrapper for LTA Data Mall
#
# Author::    <NAME>
# Copyright:: Copyright (c) 2016-2017 <NAME>
# License::   AGPL-3.0

module LtaDataMall
  class Configuration
    attr_accessor :account_key
  end

  # Get configuration block
  def self.configuration
    if @configuration.nil?
      @configuration = Configuration.new
    end
    if block_given?
      yield @configuration
    else
      @configuration
    end
  end

  class HttpClient #:nodoc:
    def self.call(url, headers)
    end

    def self.json(endpoint, parameters)
      uri = URI.parse(endpoint)
      uri.query = URI.encode_www_form(parameters)

      http = Net::HTTP.new(uri.host, uri.port)

      get_request = Net::HTTP::Get.new(uri.to_s)
      get_request['AccountKey'] = ::LtaDataMall.configuration.account_key

      http.start do |http|
	      response = http.request(get_request)

        return OpenStruct.new ({
          date: response['Date'],
          body: response.body
        })
      end
    end
  end

  class BusArrival
    class Stop
      attr_reader :response_datetime, :stop_code, :services

      def initialize(service_data_string, response_datetime) #:nodoc:
        @response_datetime = DateTime.parse(response_datetime)
        service_data = JSON.parse(service_data_string)
        @stop_code = service_data['BusStopCode']

        @services = []
        service_data['Services'].each do |service|
          @services << Service.new(service)
        end
      end
    end

    class Service
      attr_reader :no, :operator, :arrivals

      OPERATOR_TYPES = {
        'SBST' => :sbst,
        'SMRT' => :smrt,
        'TTS'  => :tts,
        'GAS'  => :gas
      }

      def initialize(service_data) #:nodoc:
        @no = service_data['ServiceNo']
        @operator = OPERATOR_TYPES[service_data['Operator']]

        @arrivals = []
        ['NextBus', 'NextBus2', 'NextBus3'].each do |next_bus_key|
          @arrivals << Bus.new(service_data[next_bus_key])
        end
      end
    end

    class Bus

      attr_reader :origin_code, :destination_code, :estimated_arrival,
        :latitude, :longitude, :visit_number, :load, :is_wab, :vehicle

      alias_method :is_wab?, :is_wab

        LOAD_TYPES = {
          'SEA' => :seats_available,
          'SDA' => :standing_available,
          'LSD' => :limited_standing
        }

        VEHICLE_TYPES = {
          'SD' => :single_deck,
          'DD' => :double_deck,
          'BD' => :articulated
        }

      def initialize(service_data) #:nodoc:
        @origin_code = service_data['OriginCode']
        @destination_code = service_data['DestinationCode']
        begin
          @estimated_arrival = DateTime.parse(service_data['EstimatedArrival'])
        rescue
          @estimated_arrival = nil
        end
        @latitude = service_data['Latitude']
        @longitude = service_data['Longitude']
        @visit_number = service_data['VisitNumber'].to_i
        @load = LOAD_TYPES[service_data['Load']]
        @is_wab = service_data['Feature'] == 'WAB'
        @vehicle = VEHICLE_TYPES[service_data['Type']]
      end

      # Returns true if there are seats available
      def has_seats?
        @load == :seats_available
      end
    end

    ENDPOINT_URL = 'http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2'

    def get_bus_arrival(bus_stop_code, bus_route = nil)
      parameters = { 'BusStopCode' => bus_stop_code.to_s }
      parameters['ServiceNo'] = bus_route unless (bus_route.nil?|| bus_route == '') 
      response = ::LtaDataMall::HttpClient.json(ENDPOINT_URL, parameters)

      Stop.new(response.body, response.date)
    end
  end
end
{'fraction_non_alphanumeric': 0.11008031016338965, 'fraction_numerical': 0.0024923843810578787, 'mean_word_length': 2.5528775209050663, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2600891', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2279, 'n_words': 442}