Class: Gares::Search
- Inherits:
-
StationList
- Object
- StationList
- Gares::Search
- 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 captaintrain.com
"https://raw.githubusercontent.com/captaintrain/stations/master/stations.csv"
- @@trie =
nil
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(query, field = :name) ⇒ Search
constructor
Initialize a new Station search with the specified query.
-
#stations ⇒ Object
Returns an array of Gares::Station objects in order to easily search result yielded.
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 remote 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
#query ⇒ Object (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
#stations ⇒ Object
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 |