class Distance
Constants
- DEFAULT_CONSUMPTION_L_PER_100KM
- FUEL_API_URL
- IRC_CTRL
-
IRC mIRC colours: ctrl+FG or ctrl+FG,BG (use 3.chr — “\x300” is NOT ctrl+0)
- IRC_RESET
- SET_FUEL_PATTERN
- SET_FUEL_PREFIX
- STYLE_CHIP
- STYLE_MUTED
- STYLE_ROUTE
- STYLE_SEP
- SUPPORTED_FUEL_REGIONS
Public Class Methods
Source
# File plugins/distance.rb, line 365 def self.decorate(value, style: STYLE_CHIP) return nil if value.nil? || value.to_s.strip.empty? "#{style}#{value}#{IRC_RESET}" end
Source
# File plugins/distance.rb, line 375 def self.format_message(parts) return "No route found" unless parts message = [ decorate(parts[:path], style: STYLE_ROUTE), decorate("#{parts[:distance]}, #{parts[:duration]}") ].compact.join(" #{separator} ") road = parts[:road].to_s.strip unless road.empty? message = "#{message} #{decorate("via", style: STYLE_MUTED)} #{decorate(road)}" end message end
Source
# File plugins/distance.rb, line 310 def self.handle(text, network:, nick:, fetch: nil, api_key: ENV["GOOGLE_MAPS_API_KEY"], fuel_fetch: nil, store: RabbotDb) parsed = parse_request(text) case parsed[:action] when :set_fuel DistanceFuelStore.set(network: network, nick: nick, consumption: parsed[:consumption], store: store) { reply: set_fuel_reply(parsed[:consumption]) } when :set_fuel_error { error: "Usage: !distance set fuel <L/100km>" } when :route plugin = allocate fetch ||= plugin.method(:fetch_json) fuel_fetch ||= plugin.method(:fetch_fuel) { reply: plugin.journey_report( parsed[:route_text], fetch: fetch, api_key: api_key, fuel_fetch: fuel_fetch, network: network, nick: nick, store: store ) } else { error: usage_message } end end
Source
# File plugins/distance.rb, line 294 def self.handle_return(text, network:, nick:, fetch: nil, api_key: ENV["GOOGLE_MAPS_API_KEY"], fuel_fetch: nil, store: RabbotDb) expanded = DistanceReturn.expand_route(text) return { error: expanded[:error] } if expanded[:error] handle( expanded[:route_text], network: network, nick: nick, fetch: fetch, api_key: api_key, fuel_fetch: fuel_fetch, store: store ) end
Source
# File plugins/distance.rb, line 259 def self.irc_color(fg, bg = nil) bg ? "#{IRC_CTRL}#{fg},#{bg}" : "#{IRC_CTRL}#{fg}" end
Source
# File plugins/distance.rb, line 346 def self.parse_command(text) text = text.to_s.strip if (match = text.match(/\A(\d+(?:\.\d+)?)([dp])?\s+(.+)\z/i)) return { consumption: match[1].to_f, consumption_inline: true, fuel_type: parse_fuel_type_suffix(match[2]), route_text: match[3].strip } end { consumption: DEFAULT_CONSUMPTION_L_PER_100KM, consumption_inline: false, fuel_type: nil, route_text: text } end
Source
# File plugins/distance.rb, line 339 def self.parse_fuel_type_suffix(suffix) case suffix.to_s.downcase when "d" then :diesel when "p" then :petrol end end
Source
# File plugins/distance.rb, line 280 def self.parse_request(text) text = text.to_s.strip if (match = text.match(SET_FUEL_PATTERN)) return { action: :set_fuel, consumption: match[1].to_f } end return { action: :set_fuel_error } if text.match?(SET_FUEL_PREFIX) parse_command(text).merge(action: :route) end
Source
# File plugins/distance.rb, line 371 def self.separator "#{STYLE_SEP}|#{IRC_RESET}" end
Source
# File plugins/distance.rb, line 290 def self.set_fuel_reply(consumption) IrcFormat.accent_paren("Fuel economy saved — #{consumption} L/100km") end
Source
# File plugins/distance.rb, line 273 def self.usage_message 'Usage: !distance [<L/100km>[d|p]] <origin> to <destination> [via <waypoint> ...] — e.g. !distance 9.3d elgin vale to mount low — !distance set fuel 8.9 saves your economy' end
Public Instance Methods
Source
# File plugins/distance.rb, line 401 def distance_summary(route_text, fetch: method(:fetch_json), api_key: ENV["GOOGLE_MAPS_API_KEY"]) result = Maps.fetch_directions(route_text, fetch: fetch, api_key: api_key) return result.gsub("!maps", "!distance") if result.is_a?(String) && result.include?("!maps") return result if result.is_a?(String) self.class.format_message(Maps.summary_parts(result[:data], route: result[:route])) rescue StandardError "Could not fetch directions" end
Source
# File plugins/distance.rb, line 457 def execute(m, route_text) network = bot.config.server nick = m.user.nick result = self.class.handle(route_text, network: network, nick: nick) if result[:error] flood_safe_reply(m, result[:error], with_progress: false) return end themed_flood_safe_reply(m, result[:reply]) if result[:reply] end
Source
# File plugins/distance.rb, line 470 def execute_return(m, text) network = bot.config.server nick = m.user.nick result = self.class.handle_return(text, network: network, nick: nick) if result[:error] flood_safe_reply(m, result[:error], with_progress: false) return end themed_flood_safe_reply(m, result[:reply]) if result[:reply] end
Source
# File plugins/distance.rb, line 440 def fetch_fuel(url) RabbotHttp.fetch(url) end
Source
# File plugins/distance.rb, line 436 def fetch_json(url) URI.parse(url).open("User-Agent" => "Mozilla/5.0").read end
Source
# File plugins/distance.rb, line 411 def journey_report(route_text, fetch: method(:fetch_json), api_key: ENV["GOOGLE_MAPS_API_KEY"], fuel_fetch: method(:fetch_fuel), consumption: nil, network: nil, nick: nil, store: RabbotDb) parsed = self.class.parse_command(route_text) route_text = parsed[:route_text] fuel_type = parsed[:fuel_type] consumption = resolve_consumption( consumption, parsed: parsed, network: network, nick: nick, store: store ) result = Maps.fetch_directions(route_text, fetch: fetch, api_key: api_key) return result.gsub("!maps", "!distance") if result.is_a?(String) && result.include?("!maps") return result if result.is_a?(String) data = result[:data] summary = self.class.format_message(Maps.summary_parts(data, route: result[:route])) card = JourneyFuel.fuel_card_for_directions_data( data, fetch: fuel_fetch, consumption: consumption, fuel_type: fuel_type ) card ? [summary, card].join("\n") : summary rescue StandardError "Could not fetch directions" end
Source
# File plugins/distance.rb, line 444 def resolve_consumption(explicit, parsed:, network:, nick:, store:) return explicit unless explicit.nil? return parsed[:consumption] if parsed[:consumption_inline] return parsed[:consumption] unless network && nick && !nick.to_s.strip.empty? DistanceFuelStore.resolve( network: network, nick: nick, store: store, default: self.class::DEFAULT_CONSUMPTION_L_PER_100KM ) end