Sha256: 3b6d90b3a5522800e5b4001a2492a4a2fdbb8082ba38e902b5520cab444a2586

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

module Gares
  # Search Gares-en-mouvement for a station name
  class Search < StationList
    attr_reader :query

    # This is the stations database from capitainetrain.com
    GARES_LIST_URL = "https://raw.githubusercontent.com/capitainetrain/stations/master/stations.csv"

    # List of keywords to ignore while searching
    IGNORE_KEYWORDS = ["ST", "GARE", "SNCF"]
    # Initialize a new Station search with the specified query
    #
    #   search = Gares::Search.new("Aix")
    #
    # Gares::Search is lazy loaded, meaning that unless you access the +stations+
    # attribute, no remomte query is made.
    #
    def initialize(query, field = :name)
      @query = query
      @by = field
    end

    # Returns an array of Gares::Station objects in order to easily search result yielded.
    # If the +query+ was an exact match, a single element array will be returned.
    def stations
      @stations ||= (exact_match? ? parse_station : parse_stations)
    end

    private

    def data
      @data ||= self.class.query.map { |raw_station| Gares::Station.new(raw_station) }
    end

    def result
      keywords = @query.split(" ").select { |keyword| !IGNORE_KEYWORDS.include?(keyword.upcase) }
      @result ||= data.select do |station|
        station.send(@by) && station.send(@by).to_ascii =~ /#{keywords.join(".*")}/i
      end
    end

    def self.query
      @data ||= SmarterCSV.process(open(GARES_LIST_URL), col_sep: ";")
    end

    def parse_station
      [result.first]
    end

    def exact_match?
      result.count == 1
    end
  end # Search
end # Gares

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gares-2.0.0.pre.dev1 lib/gares/search.rb
gares-2.0.0.pre.dev lib/gares/search.rb