Class: GrabzItClient
- Inherits:
-
Object
- Object
- GrabzItClient
- Defined in:
- lib/grabzitclient.rb
Overview
The public REST API for grabz.it This API allows you to take screenshot of websites for free and convert them into images, PDF's and tables.
Constant Summary
Instance Method Summary (collapse)
-
- (Boolean) add_watermark(identifier, path, xpos, ypos)
Add a new custom watermark.
-
- (Boolean) delete_cookie(name, domain)
Delete a custom cookie or block a global cookie from being used.
-
- (Boolean) delete_watermark(identifier)
Delete a custom watermark.
-
- (Array<GrabzItCookie>) get_cookies(domain)
An array of cookies.
-
- (Object) get_picture(id)
deprecated
Deprecated.
Use #get_result instead.
-
- (Object) get_result(id)
This method returns the screenshot itself.
-
- (ScreenShotStatus) get_status(id)
Get the current status of a GrabzIt screenshot.
-
- (Array<GrabzItWaterMark>) get_watermarks(identifier = nil)
Get your uploaded custom watermarks.
-
- (GrabzItClient) initialize(applicationKey, applicationSecret)
constructor
Create a new instance of the GrabzItClient class in order to access the GrabzIt API.
-
- (String) save(callBackURL = nil)
Save the result asynchronously and returns a unique identifier, which can be used to get the screenshot with the get_result method.
-
- (Boolean) save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
deprecated
Deprecated.
Use #set_image_options and #save_to instead.
-
- (Boolean) save_to(saveToFile)
Save the result synchronously to a file.
-
- (Boolean) set_cookie(name, domain, value = "", path = "/", httponly = false, expires = "")
Sets a new custom cookie on GrabzIt, if the custom cookie has the same name and domain as a global cookie the global cookie is overridden.
-
- (void) set_image_options(url, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil, requestMobileVersion = false, customWaterMarkId = nil)
Sets the parameters required to take a screenshot of a web page.
-
- (void) set_pdf_options(url, customId = nil, includeBackground = true, pagesize = 'A4', orientation = 'Portrait', includeLinks = true, includeOutline = false, title = nil, coverURL = nil, marginTop = 10, marginLeft = 10, marginBottom = 10, marginRight = 10, delay = nil, requestMobileVersion = false, customWaterMarkId = nil)
Sets the parameters required to convert a web page into a PDF.
-
- (void) set_table_options(url, customId = nil, tableNumberToInclude = 1, format = 'csv', includeHeaderNames = true, includeAllTables = false, targetElement = nil, requestMobileVersion = false)
Sets the parameters required to extract one or more tables from a web page.
-
- (String) take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
deprecated
Deprecated.
Use #set_image_options and #save instead.
Constructor Details
- (GrabzItClient) initialize(applicationKey, applicationSecret)
Create a new instance of the GrabzItClient class in order to access the GrabzIt API.
30 31 32 33 |
# File 'lib/grabzitclient.rb', line 30 def initialize(applicationKey, applicationSecret) @applicationKey = applicationKey @applicationSecret = applicationSecret end |
Instance Method Details
- (Boolean) add_watermark(identifier, path, xpos, ypos)
Add a new custom watermark.
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/grabzitclient.rb', line 278 def add_watermark(identifier, path, xpos, ypos) if !File.file?(path) raise "File: " + path + " does not exist" end sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(identifier)+"|"+nil_int_check(xpos)+"|"+nil_int_check(ypos)); boundary = '--------------------------'+Time.now.to_f.to_s url = WebServicesBaseURL + "addwatermark.ashx" uri = URI.parse(url) file = File.open(path, "rb") data = file.read post_body = Array.new post_body << "\r\n--"+boundary+"\r\n" post_body << "Content-Disposition: form-data; name=\"watermark\"; filename=\""+File.basename(path)+"\"\r\nContent-Type: image/jpeg\r\n\r\n" post_body << data post_body << "\r\n--"+boundary+"\r\n" post_body << "Content-Disposition: form-data; name=\"key\"\r\n\r\n" post_body << CGI.escape(nil_check(@applicationKey)) post_body << "\r\n--"+boundary+"\r\n" post_body << "Content-Disposition: form-data; name=\"identifier\"\r\n\r\n" post_body << CGI.escape(nil_check(identifier)) post_body << "\r\n--"+boundary+"\r\n" post_body << "Content-Disposition: form-data; name=\"xpos\"\r\n\r\n" post_body << nil_check(xpos) post_body << "\r\n--"+boundary+"\r\n" post_body << "Content-Disposition: form-data; name=\"ypos\"\r\n\r\n" post_body << nil_check(ypos) post_body << "\r\n--"+boundary+"\r\n" post_body << "Content-Disposition: form-data; name=\"sig\"\r\n\r\n" post_body << sig post_body << "\r\n--"+boundary+"--\r\n" request = Net::HTTP::Post.new(url) request.content_type = "multipart/form-data, boundary="+boundary request.body = post_body.join response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(request) } return (get_result_value(response.body(), "Result") == TrueString) end |
- (Boolean) delete_cookie(name, domain)
Delete a custom cookie or block a global cookie from being used
240 241 242 243 244 245 246 |
# File 'lib/grabzitclient.rb', line 240 def (name, domain) sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|1") qs = "key=" + CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&name="+CGI.escape(nil_check(name))+"&delete=1&sig="+sig return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString) end |
- (Boolean) delete_watermark(identifier)
Delete a custom watermark.
331 332 333 334 335 336 337 |
# File 'lib/grabzitclient.rb', line 331 def delete_watermark(identifier) sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(identifier)) qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig return (get_result_value(get(WebServicesBaseURL + "deletewatermark.ashx?" + qs), "Result") == TrueString) end |
- (Array<GrabzItCookie>) get_cookies(domain)
An array of cookies
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/grabzitclient.rb', line 186 def (domain) sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(domain)) qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&sig="+sig result = get(WebServicesBaseURL + "getcookies.ashx?" + qs) doc = REXML::Document.new(result) = doc.root.elements["Message"].text() if != nil raise end = Array.new = doc.elements.to_a("//WebResult/Cookies/Cookie") .each do || expires = nil if .elements["Expires"] != nil expires = .elements["Expires"].text end grabzItCookie = GrabzItCookie.new(.elements["Name"].text, .elements["Domain"].text, .elements["Value"].text, .elements["Path"].text, (.elements["HttpOnly"].text == TrueString), expires, .elements["Type"].text) << grabzItCookie end return end |
- (Object) get_picture(id)
Use #get_result instead.
This method returns the image itself. If nothing is returned then something has gone wrong or the image is not ready yet.
382 383 384 |
# File 'lib/grabzitclient.rb', line 382 def get_picture(id) return get_result(id) end |
- (Object) get_result(id)
This method returns the screenshot itself. If nothing is returned then something has gone wrong or the screenshot is not ready yet
178 179 180 |
# File 'lib/grabzitclient.rb', line 178 def get_result(id) return get(WebServicesBaseURL + "getfile.ashx?id=" + nil_check(id)) end |
- (ScreenShotStatus) get_status(id)
Get the current status of a GrabzIt screenshot
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/grabzitclient.rb', line 161 def get_status(id) result = get(WebServicesBaseURL + "getstatus.ashx?id=" + nil_check(id)) doc = REXML::Document.new(result) processing = doc.root.elements["Processing"].text() cached = doc.root.elements["Cached"].text() expired = doc.root.elements["Expired"].text() = doc.root.elements["Message"].text() return ScreenShotStatus.new((processing == TrueString), (cached == TrueString), (expired == TrueString), ) end |
- (Array<GrabzItWaterMark>) get_watermarks(identifier = nil)
Get your uploaded custom watermarks
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/grabzitclient.rb', line 251 def get_watermarks(identifier = nil) sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(identifier)) qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig result = get(WebServicesBaseURL + "getwatermarks.ashx?" + qs) doc = REXML::Document.new(result) watermarks = Array.new xml_watemarks = doc.elements.to_a("//WebResult/WaterMarks/WaterMark") xml_watemarks.each do |watemark| grabzItWaterMark = GrabzItWaterMark.new(watemark.elements["Identifier"].text, watemark.elements["XPosition"].text.to_i, watemark.elements["YPosition"].text.to_i, watemark.elements["Format"].text) watermarks << grabzItWaterMark end return watermarks end |
- (String) save(callBackURL = nil)
This is the recommended method of saving a file
Save the result asynchronously and returns a unique identifier, which can be used to get the screenshot with the get_result method
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/grabzitclient.rb', line 109 def save(callBackURL = nil) if @@signaturePartOne == nil && @@signaturePartTwo == nil && @@request == nil raise "No screenshot parameters have been set." end sig = Digest::MD5.hexdigest(@@signaturePartOne+nil_check(callBackURL)+@@signaturePartTwo) @@request += CGI.escape(nil_check(callBackURL))+"&sig="+sig return get_result_value(get(@@request), "ID") end |
- (Boolean) save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
Use #set_image_options and #save_to instead.
This method takes the screenshot and then saves the result to a file. WARNING this method is synchronous.
373 374 375 376 |
# File 'lib/grabzitclient.rb', line 373 def save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil) (url, nil, browserWidth, browserHeight, width, height, format, delay, targetElement) return save_to(saveToFile) end |
- (Boolean) save_to(saveToFile)
Warning this method is synchronous so will cause a application to pause while the result is processed.
Save the result synchronously to a file.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/grabzitclient.rb', line 127 def save_to(saveToFile) id = save() #Wait for it to be ready. while true do status = get_status(id) if !status.cached && !status.processing raise "The screenshot did not complete with the error: " + status.Message break; elsif status.cached result = get_result(id) if !result raise "The screenshot image could not be found on GrabzIt." break end screenshot = File.new(saveToFile, "wb") screenshot.write(result) screenshot.close break end sleep(1) end return true end |
- (Boolean) set_cookie(name, domain, value = "", path = "/", httponly = false, expires = "")
This can be useful if a websites functionality is controlled by cookies
Sets a new custom cookie on GrabzIt, if the custom cookie has the same name and domain as a global cookie the global cookie is overridden
227 228 229 230 231 232 233 |
# File 'lib/grabzitclient.rb', line 227 def (name, domain, value = "", path = "/", httponly = false, expires = "") sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|"+nil_check(value)+"|"+nil_check(path)+"|"+b_to_str(httponly)+"|"+nil_check(expires)+"|0") qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&name="+CGI.escape(nil_check(name))+"&value="+CGI.escape(nil_check(value))+"&path="+CGI.escape(nil_check(path))+"&httponly="+b_to_str(httponly)+"&expires="+nil_check(expires)+"&sig="+sig return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString) end |
- (void) set_image_options(url, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil, requestMobileVersion = false, customWaterMarkId = nil)
This method returns an undefined value.
Sets the parameters required to take a screenshot of a web page.
48 49 50 51 52 53 54 |
# File 'lib/grabzitclient.rb', line 48 def (url, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil, requestMobileVersion = false, customWaterMarkId = nil) @@request = WebServicesBaseURL + "takepicture.ashx?key="+CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(nil_check(url))+"&width="+nil_int_check(width)+"&height="+nil_int_check(height)+"&format="+CGI.escape(nil_check(format))+"&bwidth="+nil_int_check(browserWidth)+"&bheight="+nil_int_check(browserHeight)+"&customid="+CGI.escape(nil_check(customId))+"&delay="+nil_int_check(delay)+"&target="+CGI.escape(nil_check(targetElement))+"&customwatermarkid="+CGI.escape(nil_check(customWaterMarkId))+"&requestmobileversion="+b_to_str(requestMobileVersion)+"&callback=" @@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|" @@signaturePartTwo = "|"+nil_check(format)+"|"+nil_int_check(height)+"|"+nil_int_check(width)+"|"+nil_int_check(browserHeight)+"|"+nil_int_check(browserWidth)+"|"+nil_check(customId)+"|"+nil_int_check(delay)+"|"+nil_check(targetElement)+"|"+nil_check(customWaterMarkId)+"|"+b_to_str(requestMobileVersion) return nil end |
- (void) set_pdf_options(url, customId = nil, includeBackground = true, pagesize = 'A4', orientation = 'Portrait', includeLinks = true, includeOutline = false, title = nil, coverURL = nil, marginTop = 10, marginLeft = 10, marginBottom = 10, marginRight = 10, delay = nil, requestMobileVersion = false, customWaterMarkId = nil)
This method returns an undefined value.
Sets the parameters required to convert a web page into a PDF.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/grabzitclient.rb', line 92 def (url, customId = nil, includeBackground = true, pagesize = 'A4', orientation = 'Portrait', includeLinks = true, includeOutline = false, title = nil, coverURL = nil, marginTop = 10, marginLeft = 10, marginBottom = 10, marginRight = 10, delay = nil, requestMobileVersion = false, customWaterMarkId = nil) pagesize = nil_check(pagesize).upcase $orientation = nil_check(orientation).capitalize @@request = WebServicesBaseURL + "takepdf.ashx?key=" + CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(nil_check(url))+"&background="+b_to_str(includeBackground)+"&pagesize="+pagesize+"&orientation="+orientation+"&customid="+CGI.escape(nil_check(customId))+"&customwatermarkid="+CGI.escape(nil_check(customWaterMarkId))+"&includelinks="+b_to_str(includeLinks)+"&includeoutline="+b_to_str(includeOutline)+"&title="+CGI.escape(nil_check(title))+"&coverurl="+CGI.escape(nil_check(coverURL))+"&mleft="+nil_int_check(marginLeft)+"&mright="+nil_int_check(marginRight)+"&mtop="+nil_int_check(marginTop)+"&mbottom="+nil_int_check(marginBottom)+"&delay="+nil_int_check(delay)+"&requestmobileversion="+b_to_str(requestMobileVersion)+"&callback=" @@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|" @@signaturePartTwo = "|"+nil_check(customId)+"|"+b_to_str(includeBackground)+"|"+pagesize +"|"+orientation+"|"+nil_check(customWaterMarkId)+"|"+b_to_str(includeLinks)+"|"+b_to_str(includeOutline)+"|"+nil_check(title)+"|"+nil_check(coverURL)+"|"+nil_int_check(marginTop)+"|"+nil_int_check(marginLeft)+"|"+nil_int_check(marginBottom)+"|"+nil_int_check(marginRight)+"|"+nil_int_check(delay)+"|"+b_to_str(requestMobileVersion) return nil end |
- (void) set_table_options(url, customId = nil, tableNumberToInclude = 1, format = 'csv', includeHeaderNames = true, includeAllTables = false, targetElement = nil, requestMobileVersion = false)
This method returns an undefined value.
Sets the parameters required to extract one or more tables from a web page.
66 67 68 69 70 71 72 |
# File 'lib/grabzitclient.rb', line 66 def (url, customId = nil, tableNumberToInclude = 1, format = 'csv', includeHeaderNames = true, includeAllTables = false, targetElement = nil, requestMobileVersion = false) @@request = WebServicesBaseURL + "taketable.ashx?key=" + CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(url)+"&includeAllTables="+b_to_str(includeAllTables)+"&includeHeaderNames="+b_to_str(includeHeaderNames)+"&format="+CGI.escape(nil_check(format))+"&tableToInclude="+nil_int_check(tableNumberToInclude)+"&customid="+CGI.escape(nil_check(customId))+"&target="+CGI.escape(nil_check(targetElement))+"&requestmobileversion="+b_to_str(requestMobileVersion)+"&callback=" @@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|" @@signaturePartTwo = "|"+nil_check(customId)+"|"+nil_int_check(tableNumberToInclude)+"|"+b_to_str(includeAllTables)+"|"+b_to_str(includeHeaderNames)+"|"+nil_check(targetElement)+"|"+nil_check(format)+"|"+b_to_str(requestMobileVersion) return nil end |
- (String) take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
Use #set_image_options and #save instead.
This method calls the GrabzIt web service to take the screenshot.
353 354 355 356 |
# File 'lib/grabzitclient.rb', line 353 def take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil) (url, customId, browserWidth, browserHeight, width, height, format, delay, targetElement) return save(callback) end |