|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "digest/md5" |
| 4 | +require "net/http" |
| 5 | +require "uri" |
| 6 | +require "json" |
| 7 | + |
| 8 | +class Capture |
| 9 | + API_URL = "https://cdn.capture.page" |
| 10 | + EDGE_URL = "https://edge.capture.page" |
| 11 | + |
| 12 | + attr_reader :key, :options |
| 13 | + |
| 14 | + def initialize(key, secret, options = {}) |
| 15 | + @key = key |
| 16 | + @secret = secret |
| 17 | + options = options.nil? ? {} : options |
| 18 | + raise TypeError, "options must be a Hash" unless options.is_a?(Hash) |
| 19 | + @options = options |
| 20 | + end |
| 21 | + |
| 22 | + def build_image_url(url, options = {}) |
| 23 | + build_url(url, "image", options) |
| 24 | + end |
| 25 | + |
| 26 | + def build_pdf_url(url, options = {}) |
| 27 | + build_url(url, "pdf", options) |
| 28 | + end |
| 29 | + |
| 30 | + def build_content_url(url, options = {}) |
| 31 | + build_url(url, "content", options) |
| 32 | + end |
| 33 | + |
| 34 | + def build_metadata_url(url, options = {}) |
| 35 | + build_url(url, "metadata", options) |
| 36 | + end |
| 37 | + |
| 38 | + def build_animated_url(url, options = {}) |
| 39 | + build_url(url, "animated", options) |
| 40 | + end |
| 41 | + |
| 42 | + def fetch_image(url, options = {}) |
| 43 | + fetch_binary(build_image_url(url, options)) |
| 44 | + end |
| 45 | + |
| 46 | + def fetch_pdf(url, options = {}) |
| 47 | + fetch_binary(build_pdf_url(url, options)) |
| 48 | + end |
| 49 | + |
| 50 | + def fetch_content(url, options = {}) |
| 51 | + fetch_json(build_content_url(url, options)) |
| 52 | + end |
| 53 | + |
| 54 | + def fetch_metadata(url, options = {}) |
| 55 | + fetch_json(build_metadata_url(url, options)) |
| 56 | + end |
| 57 | + |
| 58 | + def fetch_animated(url, options = {}) |
| 59 | + fetch_binary(build_animated_url(url, options)) |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + def build_url(url, request_type, options) |
| 65 | + raise TypeError, "key and secret must be strings" unless @key.is_a?(String) && @secret.is_a?(String) |
| 66 | + raise ArgumentError, "Key and Secret is required" if @key.empty? || @secret.empty? |
| 67 | + raise ArgumentError, "url is required" if url.nil? || (url.is_a?(String) && url.empty?) |
| 68 | + raise TypeError, "url should be of type string (something like www.google.com)" unless url.is_a?(String) |
| 69 | + |
| 70 | + options = options.nil? ? {} : options |
| 71 | + raise TypeError, "options must be a Hash" unless options.is_a?(Hash) |
| 72 | + params = options.merge("url" => url) |
| 73 | + query_string = encode_query_string(params) |
| 74 | + token = generate_token(@secret, query_string) |
| 75 | + base_url = @options[:use_edge] || @options["useEdge"] ? EDGE_URL : API_URL |
| 76 | + |
| 77 | + "#{base_url}/#{@key}/#{token}/#{request_type}?#{query_string}" |
| 78 | + end |
| 79 | + |
| 80 | + def generate_token(secret, query_string) |
| 81 | + Digest::MD5.hexdigest("#{secret}#{query_string}") |
| 82 | + end |
| 83 | + |
| 84 | + def encode_query_string(params) |
| 85 | + filtered = params.each_with_object({}) do |(k, v), hash| |
| 86 | + next if v.nil? |
| 87 | + |
| 88 | + hash[k] = case v |
| 89 | + when true then "true" |
| 90 | + when false then "false" |
| 91 | + else v.to_s |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + URI.encode_www_form(filtered) |
| 96 | + end |
| 97 | + |
| 98 | + def fetch_binary(url) |
| 99 | + uri = URI.parse(url) |
| 100 | + response = Net::HTTP.get_response(uri) |
| 101 | + raise "HTTP Error: #{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess) |
| 102 | + |
| 103 | + response.body.force_encoding("BINARY") |
| 104 | + end |
| 105 | + |
| 106 | + def fetch_json(url) |
| 107 | + uri = URI.parse(url) |
| 108 | + response = Net::HTTP.get_response(uri) |
| 109 | + raise "HTTP Error: #{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess) |
| 110 | + |
| 111 | + JSON.parse(response.body) |
| 112 | + end |
| 113 | +end |
0 commit comments