Skip to content

Instantly share code, notes, and snippets.

@cjroebuck
Forked from urlbox/urlbox.rb
Created September 15, 2015 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjroebuck/dd25bb94f3b8672e11e8 to your computer and use it in GitHub Desktop.
Save cjroebuck/dd25bb94f3b8672e11e8 to your computer and use it in GitHub Desktop.
urlbox ruby
require 'openssl'
require 'open-uri'
def encodeURIComponent(val)
URI.escape(val, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end
def urlbox(url, options={}, format='png')
urlbox_apikey = 'YOUR_API_KEY'
urlbox_secret = 'YOUR_API_SECRET'
query = {
:url => url, # required - the url you want to screenshot
:force => options[:force], # optional - boolean - whether you want to generate a new screenshot rather than receive a previously cached one - this also overwrites the previously cached image
:full_page => options[:full_page], # optional - boolean - return a screenshot of the full screen
:thumb_width => options[:thumb_width], # optional - number - thumbnail the resulting screenshot using this width in pixels
:width => options[:width], # optional - number - set viewport width to use (in pixels)
:height => options[:height], # optional - number - set viewport height to use (in pixels)
:quality => options[:quality] # optional - number (0-100) - set quality of the screenshot
}
query_string = query.
sort_by {|s| s[0].to_s }.
select {|s| s[1] }.
map {|s| s.map {|v| encodeURIComponent(v.to_s) }.join('=') }.
join('&')
token = OpenSSL::HMAC.hexdigest('sha1', urlbox_secret, query_string)
"https://api.urlbox.io/v1/#{urlbox_apikey}/#{token}/#{format}?#{query_string}"
end
### USAGE: (format can be png or jpg, we default to png) ###
url = urlbox("www.google.com", {thumb_width: 200, full_page: true, quality: 90}, 'jpg')
puts url
# url: "https://api.urlbox.io/v1/YOUR_API_KEY/1e6b72c3189da3f4ce8f1f2a0740ddb098e06f84/jpg?full_page=true&quality=90&thumb_width=200&url=www.google.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment