module GoogleDriveBody
Constants
- IMAGE_SIGNATURES
Public Instance Methods
Source
# File lib/google_drive_body.rb, line 77 def ascii_snippet(text, max: 80) cleaned = text.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "?") .gsub(/\s+/, " ") .strip cleaned = cleaned[0, max] cleaned.empty? ? "html response" : cleaned rescue StandardError "unreadable response" end
Source
# File lib/google_drive_body.rb, line 62 def body_snippet(body, kind:, max: 80) text = body.to_s return "empty response" if text.empty? case kind when :image "image data" when :binary hex = text.b.byteslice(0, 8).unpack1("H*") "binary #{hex}" else ascii_snippet(text, max: max) end end
Source
# File lib/google_drive_body.rb, line 23 def describe_fetch(url, body, error: nil) if error return { url: url.to_s, kind: :error, bytes: 0, snippet: "#{error.class}: #{error.message}" } end data = body.to_s bytes = data.b.bytesize kind = if bytes.zero? :empty elsif image_body?(data) :image elsif html_body?(data) :html else :binary end { url: url.to_s, kind: kind, bytes: bytes, snippet: body_snippet(data, kind: kind) } end
Source
# File lib/google_drive_body.rb, line 53 def html_body?(body) sample = body.to_s.b.byteslice(0, 512).to_s return false if image_body?(body) sample.match?(/\A\s*(?:<!DOCTYPE\s+html|<html\b)/i) || sample.include?("<body") || sample.include?("<form") end
Source
# File lib/google_drive_body.rb, line 13 def image_body?(body) bytes = body.to_s.b.byteslice(0, 12) return false if bytes.nil? || bytes.empty? return true if IMAGE_SIGNATURES.any? { |signature| bytes.start_with?(signature) } return true if bytes.start_with?("RIFF".b) && bytes.byteslice(8, 4) == "WEBP".b false end