Class: Touringplans::RoutesTable
- Inherits:
-
Object
- Object
- Touringplans::RoutesTable
- Defined in:
- lib/touringplans.rb
Overview
Generates and updates routes for all types of venues in a YAML document.
Class Method Summary collapse
- ._convert_hash_to_yaml(hash) ⇒ Object
- ._generate_interest_route(venue_permalink, interest_permalink, place_permalink) ⇒ Object
- ._generate_interest_routes_hash(interest) ⇒ Object
- ._initialize_file ⇒ Object
- ._read_file_to_terminal(file) ⇒ Object
- ._save_content_to_file(file, content) ⇒ Object
- .load_routes_file(routes_relative_file_path: "/routes.yml") ⇒ Object
- .original_routes ⇒ Object
- .stringify_keys(hash) ⇒ Object
- .symbolize_keys(hash) ⇒ Object
- .update_file ⇒ Object
Instance Method Summary collapse
-
#initialize(filename: "routes_table.yml") ⇒ RoutesTable
constructor
A new instance of RoutesTable.
Constructor Details
#initialize(filename: "routes_table.yml") ⇒ RoutesTable
Returns a new instance of RoutesTable.
157 158 159 |
# File 'lib/touringplans.rb', line 157 def initialize(filename: "routes_table.yml") @filename = filename end |
Class Method Details
._convert_hash_to_yaml(hash) ⇒ Object
264 265 266 |
# File 'lib/touringplans.rb', line 264 def self._convert_hash_to_yaml(hash) hash.to_yaml end |
._generate_interest_route(venue_permalink, interest_permalink, place_permalink) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/touringplans.rb', line 248 def self._generate_interest_route(venue_permalink, interest_permalink, place_permalink) # {magic_kingdom_attractions_haunted_mansion: { # method: "get", # path: "/magic-kingdom/attractions/haunted-mansion.json" # } # } path = "/#{venue_permalink}/#{interest_permalink}/#{place_permalink}" key = path.to_s.downcase.gsub("/", " ").gsub("-", " ").strip key = key.gsub(" ", "_") method = "get" format = "json" { key => { "method".to_s => method, "path".to_s => "#{path}.#{format}" } } end |
._generate_interest_routes_hash(interest) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/touringplans.rb', line 234 def self._generate_interest_routes_hash(interest) interest_venues = Touringplans.list_all(interest) interest_routes = {} interest_venues.each do |iv| new_route = _generate_interest_route(iv.venue_permalink, interest, iv.permalink) key = new_route.keys.first values = new_route[key] interest_routes[key] = values end interest_routes end |
._initialize_file ⇒ Object
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/touringplans.rb', line 223 def self._initialize_file # delete old file if it exits lib_dir = FileUtils.getwd + "/lib" routes_file = "#{lib_dir}/routes.yml" # ensure the file exists touched_routes_file_array = FileUtils.touch(routes_file) # we want the first string value touched_routes_file_array.first end |
._read_file_to_terminal(file) ⇒ Object
274 275 276 277 |
# File 'lib/touringplans.rb', line 274 def self._read_file_to_terminal(file) new_file = File.open(file, "r") new_file.close end |
._save_content_to_file(file, content) ⇒ Object
268 269 270 271 272 |
# File 'lib/touringplans.rb', line 268 def self._save_content_to_file(file, content) new_file = File.open(file, "w") new_file.write(content) new_file.close end |
.load_routes_file(routes_relative_file_path: "/routes.yml") ⇒ Object
204 205 206 207 208 |
# File 'lib/touringplans.rb', line 204 def self.load_routes_file(routes_relative_file_path: "/routes.yml") tp_path = $LOAD_PATH.grep(/touringplans/).last routes_file = "#{tp_path}#{routes_relative_file_path}" YAML.safe_load(File.read(routes_file)) end |
.original_routes ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/touringplans.rb', line 161 def self.original_routes # this method exists so that we can create a yaml file of routes tpr = Touringplans.routes # convert symbols back to strings stringify_keys(tpr) # rt_keys = tpr.keys # rt_values = tpr.values # string_keys = [] # rt_keys.each {|k| string_keys << k.to_s} # # create new hash with string keys # string_keys.zip(rt_values).to_h end |
.stringify_keys(hash) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/touringplans.rb', line 189 def self.stringify_keys(hash) # inspired by https://avdi.codes/recursively-symbolize-keys/ hash.each_with_object({}) do |(key, value), result| new_key = case key when Symbol then key.to_s else key end new_value = case value when Hash then stringify_keys(value) else value end result[new_key] = new_value end end |
.symbolize_keys(hash) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/touringplans.rb', line 175 def self.symbolize_keys(hash) hash.each_with_object({}) do |(key, value), result| new_key = case key when String then key.to_sym else key end new_value = case value when Hash then symbolize_keys(value) else value end result[new_key] = new_value end end |
.update_file ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/touringplans.rb', line 210 def self.update_file # gather info into hashes attractions_routes = _generate_interest_routes_hash("attractions") dining_routes = _generate_interest_routes_hash("dining") hotels_routes = _generate_interest_routes_hash("hotels") updated_routes = original_routes.merge(attractions_routes, dining_routes, hotels_routes) updated_routes_yaml = _convert_hash_to_yaml(updated_routes) file = _initialize_file _save_content_to_file(file, updated_routes_yaml) end |