class Thumbalizr
Attributes
@!attribute [r] Base URL for all API requests. You should use the default base provided by the library. Be careful if you decide to use HTTP instead of HTTPS as your API key could be sniffed and your account could be used without your consent.
@!attribute [r] print debug output to the standard output
@!attribute [r] Embed API key
@!attribute [r] Thumbalizr
secret
Public Class Methods
New client
@param key [String] Embed API key @param secret [String] Thumbalizr
secret @param debug [Boolean] Set to true to print debug output to the standard output. false (disabled) by default. @param base [String] Base URL for all API requests. You should use the default base provided by the library.
# File lib/thumbalizr.rb, line 37 def initialize(key='', secret='', debug=false, base='https://api.thumbalizr.com/api/v1/embed/') @key = key || '' @secret = secret || '' @base = base || 'https://api.thumbalizr.com/api/v1/embed/' @debug = debug || false end
Public Instance Methods
Download a Thumbalizr
thumbnail
@param url [String] Thumbalizr
URL generated by url() @param url [String] Option local file name to save the image file to. @return [Array<Symbol, Symbol>] !{:result => <result>, :image => <content>} if no file is specified, or {:result => <result>, :file => <file namet>} if file is specified result is either OK, FAILED or QUEUED. If the screenshot failed or is not finished, the image content or file name will be empty
# File lib/thumbalizr.rb, line 68 def download(url='', file='') begin response = fetch(url.to_s) case response when Net::HTTPSuccess then status = response.header['X-Thumbalizr-Status'] data = '' if (status == 'OK') if (file == '') data = response.response.body else File.open(file, 'w') {|f| f.write(response.response.body) } data = file end elsif (status == 'FAILED') puts "Error: {response.header['X-Thumbalizr-Error']}" if (@debug) end return {:result => status, :image => data} else puts "Error: #{response.header['X-Thumbalizr-Error']}" if (@debug) return {:result => response.header['X-Thumbalizr-Status'], :image => ''} end rescue Exception => e puts "{e.message}" if (@debug) raise e end end
Download a Thumbalizr
thumbnail. Unlike the download() function, this function waits until the screenshto is etiehr finished or failed.
@param url [String] Thumbalizr
URL generated by url() @param url [String] Option local file name to save the image file to. @return [Array<Symbol, Symbol>] !{:result => <result>, :image => <content>} if no file is specified, or {:result => <result>, :file => <file namet>} if file is specified result is either OK, FAILED or QUEUED. If the screenshot failed or is not finished, the image content or file name will be empty
# File lib/thumbalizr.rb, line 103 def download_wait(url='', file='', wait=10) result = download(url, file) while (result[:result] == 'QUEUED') puts "Waiting #{wait}s" if (@debug) sleep(wait) result = download(url, file) end return result end
# File lib/thumbalizr.rb, line 115 def fetch(url, limit=3) raise ArgumentError, 'HTTP redirect too deep' if (limit == 0) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = 240 http.read_timeout = 240 request = Net::HTTP::Get.new(uri.request_uri, {'User-Agent' => 'Thumbalizr Ruby 1.0'}) if (uri.scheme == 'https') http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end response = http.request(request) case response when Net::HTTPRedirection then path = response['location'] url = URL.new( URI.join(@base, path).to_s ) return fetch(url.to_s, limit - 1) else return response end end
Return the Thumbalizr
URL for the screenshot requested
@param url [String] URL of the web page @param parameters [Array<Symbol, Symbol>] Additional options See tumbalizr.com/api/documentation for the full list of possible options @return url [String] Thumbalizr
URL fo the thumbnail
# File lib/thumbalizr.rb, line 50 def url(url='', parameters={}) query = 'url=' + CGI::escape(url.to_s) parameters.each_pair do |key, value| query += "&#{key}=" + CGI::escape(value.to_s) end token = Digest::MD5.hexdigest(query + @secret) return "#{@base}#{@key}/#{token}/?#{query}" end