Class: Thumbalizr
- Inherits:
-
Object
- Object
- Thumbalizr
- Defined in:
- lib/thumbalizr.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Base URL for all API requests.
-
#debug ⇒ Object
readonly
print debug output to the standard output.
-
#key ⇒ Object
readonly
Embed API key.
-
#secret ⇒ Object
readonly
Thumbalizr secret.
Instance Method Summary collapse
-
#download(url = '', file = '') ⇒ Array<Symbol, Symbol>
Download a Thumbalizr thumbnail.
-
#download_wait(url = '', file = '', wait = 10) ⇒ Array<Symbol, Symbol>
Download a Thumbalizr thumbnail.
- #fetch(url, limit = 3) ⇒ Object
-
#initialize(key = '', secret = '', debug = false, base = 'https://api.thumbalizr.com/api/v1/embed/') ⇒ Thumbalizr
constructor
New client.
-
#url(url = '', parameters = {}) ⇒ Object
Return the Thumbalizr URL for the screenshot requested.
Constructor Details
#initialize(key = '', secret = '', debug = false, base = 'https://api.thumbalizr.com/api/v1/embed/') ⇒ Thumbalizr
New client
37 38 39 40 41 42 |
# 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 |
Instance Attribute Details
#base ⇒ Object (readonly)
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.
26 27 28 |
# File 'lib/thumbalizr.rb', line 26 def base @base end |
#debug ⇒ Object (readonly)
print debug output to the standard output
29 30 31 |
# File 'lib/thumbalizr.rb', line 29 def debug @debug end |
#key ⇒ Object (readonly)
Embed API key
20 21 22 |
# File 'lib/thumbalizr.rb', line 20 def key @key end |
#secret ⇒ Object (readonly)
Thumbalizr secret
23 24 25 |
# File 'lib/thumbalizr.rb', line 23 def secret @secret end |
Instance Method Details
#download(url = '', file = '') ⇒ Array<Symbol, Symbol>
Download a Thumbalizr thumbnail
result is either OK, FAILED or QUEUED. If the screenshot failed or is not finished, the image content or file name will be empty
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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_wait(url = '', file = '', wait = 10) ⇒ Array<Symbol, Symbol>
Download a Thumbalizr thumbnail. Unlike the download() function, this function waits until the screenshto is etiehr finished or failed.
result is either OK, FAILED or QUEUED. If the screenshot failed or is not finished, the image content or file name will be empty
103 104 105 106 107 108 109 110 111 112 |
# 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 |
#fetch(url, limit = 3) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# 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 |
#url(url = '', parameters = {}) ⇒ Object
Return the Thumbalizr URL for the screenshot requested
See tumbalizr.com/api/documentation for the full list of possible options
50 51 52 53 54 55 56 57 58 59 60 |
# 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 |