module FuelNsw
Constants
- FUEL_TYPES
- STYLE_TITLE
Public Instance Methods
Source
# File lib/fuel_nsw.rb, line 80 def cheapest(prices) prices.min_by { |row| row[:price] } end
Source
# File lib/fuel_nsw.rb, line 49 def fetch_location_prices(postcode:, fuel_type:, token:, fetch: nil, suburb: nil) fuel_code = resolve_fuel_type(fuel_type) return [] unless fuel_code url = location_url(postcode: postcode, fuel_code: fuel_code, suburb: suburb) body = if fetch fetch.call(url, headers: { "Authorization" => "Bearer #{token}" }) else RabbotHttp.fetch(url, user_agent: "Rabbot/1.0") end parse_prices(JSON.parse(body)) rescue StandardError [] end
Source
# File lib/fuel_nsw.rb, line 24 def fetch_token(key:, secret:, fetch: nil) url = "#{FuelConfig::API_BASE}#{FuelConfig::TOKEN_PATH}" auth = Base64.strict_encode64("#{key}:#{secret}") if fetch body = fetch.call(url, method: :post, headers: { "Authorization" => "Basic #{auth}" }) else body = RabbotHttp.fetch(url, user_agent: "Rabbot/1.0") end payload = JSON.parse(body) payload["access_token"].to_s rescue StandardError nil end
Source
# File lib/fuel_nsw.rb, line 84 def format_cheapest(fuel_type, postcode, row) lines = [ IrcFormat.report_header(tag: "FUEL", title: "Cheapest #{fuel_type} ยท #{postcode}", style: STYLE_TITLE) ] if row.nil? lines << "No prices found." else lines << "#{row[:name]} โ #{row[:suburb]} โ #{format('%.1f', row[:price])}c/L" end lines << IrcFormat.report_footer("FuelCheck NSW", "api.nsw.gov.au") lines.join("\n") end
Source
# File lib/fuel_nsw.rb, line 38 def location_url(postcode:, fuel_code:, suburb: nil) base = "#{FuelConfig::API_BASE}/FuelPriceCheck/v2/fuel/prices/location" params = { postcode: postcode.to_s.strip, fueltype: fuel_code } params[:suburb] = suburb if suburb "#{base}?#{params.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')}" end
Source
# File lib/fuel_nsw.rb, line 64 def parse_prices(payload) stations = payload["stations"] || payload["prices"] || payload Array(stations).filter_map do |row| next unless row.is_a?(Hash) price = row["price"] || row["fuelPrice"] next if price.nil? { name: (row["name"] || row["stationName"]).to_s.strip, suburb: (row["suburb"] || row["address"]).to_s.strip, price: price.to_f } end end
Source
# File lib/fuel_nsw.rb, line 45 def resolve_fuel_type(text) FUEL_TYPES[text.to_s.strip.downcase] end