Class: Gares::Search

Inherits:
StationList show all
Defined in:
lib/gares/search.rb

Overview

Search Gares-en-mouvement for a station name

Constant Summary collapse

GARES_LIST_URL =

This is the stations database from capitainetrain.com

"https://raw.githubusercontent.com/paulrbr/stations/stations-with-bls/stations.csv"
@@trie =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, field = :name) ⇒ Search

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.

Search can by done via the :name or :sncf_id field given in parameter. Defaults to the :name field.



23
24
25
26
27
# File 'lib/gares/search.rb', line 23

def initialize(query, field = :name)
  fail UnsupportedIndex unless %w(name sncf_id).include?(field.to_s)
  @query = query
  @by = field
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query



9
10
11
# File 'lib/gares/search.rb', line 9

def query
  @query
end

Class Method Details

.find(str) ⇒ Object



35
36
37
# File 'lib/gares/search.rb', line 35

def self.find(str)
  trie.find_prefix(str)
end

Instance Method Details

#stationsObject

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.



31
32
33
# File 'lib/gares/search.rb', line 31

def stations
  @stations ||= (exact_match? ? parse_station : parse_stations)
end