module BomRadarFetch
BomRadarFetch — parse BOM radar loop pages and fetch latest frames. Public: loop_url, parse_image_paths, latest_frame, fetch_frame Depends: bom_radar_config, rabbot_http Tests: test/lib/bom_radar_fetch_test.rb
Constants
- IMAGE_PATH_PATTERN
- TIMESTAMP_PATTERN
Public Instance Methods
Source
# File lib/bom_radar_fetch.rb, line 78 def background_url(product_id) path = format(BomRadarConfig::BACKGROUND_PATH, product_id: product_id) "#{BomRadarConfig::BASE_URL}#{path}" end
Source
# File lib/bom_radar_fetch.rb, line 74 def default_fetch ->(url) { RabbotHttp.fetch(url, user_agent: BomRadarConfig::USER_AGENT) } end
Source
# File lib/bom_radar_fetch.rb, line 83 def fetch_background(product_id, fetch: default_fetch) bytes = fetch.call(background_url(product_id)) return nil if bytes.nil? || bytes.to_s.empty? decoded = BomRadarPng.decode_indices(bytes.to_s.b) decoded.merge(bytes: bytes.to_s.b) rescue StandardError nil end
Source
# File lib/bom_radar_fetch.rb, line 63 def fetch_frame(product_id, fetch: default_fetch) html = fetch_loop(product_id, fetch: fetch) frame = latest_frame(html) return nil unless frame bytes = fetch.call(frame[:url]) return nil if bytes.nil? || bytes.to_s.empty? frame.merge(bytes: bytes.to_s.b) end
Source
# File lib/bom_radar_fetch.rb, line 59 def fetch_loop(product_id, fetch: default_fetch) fetch.call(loop_url(product_id)) end
Source
# File lib/bom_radar_fetch.rb, line 50 def format_timestamp(time, zone: "Australia/Brisbane") return nil unless time local = time.getlocal(TZInfo::Timezone.get(zone).period_for_utc(time).utc_total_offset) local.strftime("%H:%M %Z") rescue TZInfo::TimezoneNotFound, StandardError time.utc.strftime("%H:%M UTC") end
Source
# File lib/bom_radar_fetch.rb, line 28 def latest_frame(html) paths = parse_image_paths(html) path = paths.last return nil unless path { path: path, url: "#{BomRadarConfig::BASE_URL}#{path}", timestamp: parse_timestamp(path) } end
Source
# File lib/bom_radar_fetch.rb, line 20 def loop_url(product_id) "#{BomRadarConfig::BASE_URL}/products/#{product_id}.loop.shtml" end
Source
# File lib/bom_radar_fetch.rb, line 24 def parse_image_paths(html) html.to_s.scan(IMAGE_PATH_PATTERN).flatten end
Source
# File lib/bom_radar_fetch.rb, line 40 def parse_timestamp(path) match = path.to_s.match(TIMESTAMP_PATTERN) return nil unless match stamp = match[1] Time.utc(stamp[0, 4].to_i, stamp[4, 2].to_i, stamp[6, 2].to_i, stamp[8, 2].to_i, stamp[10, 2].to_i) rescue ArgumentError nil end