module GoogleDriveApi
Constants
- API_KEY_ENV
- BASE_URL
Public Instance Methods
Source
# File lib/google_drive_api.rb, line 26 def api_error?(body) return false if GoogleDriveBody.image_body?(body) text = body.to_s.strip return false if text.empty? text.start_with?("{") && text.include?("\"error\"") end
Source
# File lib/google_drive_api.rb, line 35 def api_error_snippet(body) payload = JSON.parse(body.to_s) error = payload["error"] || {} code = error["code"] message = error["message"].to_s.strip label = [code, message].compact.join(" — ") label.empty? ? "drive api error" : label rescue JSON::ParserError GoogleDriveBody.ascii_snippet(body) end
Source
# File lib/google_drive_api.rb, line 13 def api_key(env_key: API_KEY_ENV) ENV[env_key].to_s.strip end
Source
# File lib/google_drive_api.rb, line 17 def configured?(api_key: api_key()) !api_key.to_s.strip.empty? end
Source
# File lib/google_drive_api.rb, line 63 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 api_error?(data) :api_error elsif bytes.zero? :empty elsif GoogleDriveBody.image_body?(data) :image elsif GoogleDriveBody.html_body?(data) :html else :binary end snippet = case kind when :api_error then api_error_snippet(data) when :image then "image data" when :binary hex = data.b.byteslice(0, 8).unpack1("H*") "binary #{hex}" else GoogleDriveBody.ascii_snippet(data) end { url: url.to_s, kind: kind, bytes: bytes, snippet: snippet } end
Source
# File lib/google_drive_api.rb, line 21 def download_url(file_id, api_key:) key = URI.encode_www_form_component(api_key.to_s) "#{BASE_URL}/#{file_id}?alt=media&key=#{key}" end
Source
# File lib/google_drive_api.rb, line 54 def fetch_step(steps, url, fetch) body = fetch.call(url) steps << describe_fetch(url, body) api_error?(body) ? nil : body rescue StandardError => e steps << GoogleDriveBody.describe_fetch(url, nil, error: e) nil end
Source
# File lib/google_drive_api.rb, line 46 def fetch_with_trace(file_id, fetch:, api_key:) steps = [] url = download_url(file_id, api_key: api_key) body = fetch_step(steps, url, fetch) { body: body, steps: steps } end