grabzit 2.3.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,39 +1,47 @@
1
1
  # The public REST API for http://grabz.it
2
2
  # @example To include the GrabzIt module do the following
3
3
  # require 'grabzit'
4
- module GrabzIt
4
+ module GrabzIt
5
5
  require 'digest/md5'
6
6
  require 'net/http'
7
7
  require 'rexml/document'
8
8
  require 'cgi'
9
9
  require 'uri'
10
+ require File.join(File.dirname(__FILE__), 'utility')
10
11
  require File.join(File.dirname(__FILE__), 'screenshotstatus')
11
12
  require File.join(File.dirname(__FILE__), 'cookie')
12
13
  require File.join(File.dirname(__FILE__), 'watermark')
13
14
  require File.join(File.dirname(__FILE__), 'exception')
14
-
15
+ require File.join(File.dirname(__FILE__), 'animationoptions')
16
+ require File.join(File.dirname(__FILE__), 'imageoptions')
17
+ require File.join(File.dirname(__FILE__), 'tableoptions')
18
+ require File.join(File.dirname(__FILE__), 'pdfoptions')
19
+
15
20
  # This client provides access to the GrabzIt web services
16
21
  # This API allows you to take screenshot of websites for free and convert them into images, PDF's and tables.
17
22
  # @example Example usage
18
23
  # require 'grabzit'
19
24
  #
20
25
  # grabzItClient = GrabzIt::Client.new("YOUR APPLICATION KEY", "YOUR APPLICATION SECRET")
21
- # grabzItClient.set_image_options("http://www.google.com")
26
+ # grabzItClient.url_to_image("http://www.google.com")
22
27
  # grabzItClient.save("http://www.mysite.com/grabzit/handler")
23
- # @version 2.1
28
+ # @version 3.0
24
29
  # @author GrabzIt
25
30
  # @see http://grabz.it/api/ruby/ GrabzIt Ruby API
26
31
  class Client
27
32
 
28
- WebServicesBaseURL = "http://api.grabz.it/services/"
29
- private_constant :WebServicesBaseURL
33
+ WebServicesBaseURLGet = "http://api.grabz.it/services/"
34
+ private_constant :WebServicesBaseURLGet
35
+ WebServicesBaseURLPost = "http://grabz.it/services/"
36
+ private_constant :WebServicesBaseURLPost
37
+ TakePicture = "takepicture.ashx"
38
+ private_constant :TakePicture
39
+ TakeTable = "taketable.ashx"
40
+ private_constant :TakeTable
41
+ TakePDF = "takepdf.ashx"
42
+ private_constant :TakePDF
30
43
  TrueString = "True"
31
44
  private_constant :TrueString
32
-
33
- @@signaturePartOne;
34
- @@signaturePartTwo;
35
- @@startDelay;
36
- @@request;
37
45
 
38
46
  # Create a new instance of the Client class in order to access the GrabzIt API.
39
47
  #
@@ -45,129 +53,143 @@ module GrabzIt
45
53
  @applicationSecret = applicationSecret
46
54
  end
47
55
 
48
- # This method sets the parameters required to turn a online video into a animated GIF
56
+ # This method specifies the URL of the online video that should be converted into a animated GIF
49
57
  #
50
- # @param url [String] the URL of the online video
51
- # @param customId [String, nil] custom identifier that you can pass through to the animated GIF web service. This will be returned with the callback URL you have specified
52
- # @param width [Integer, 0] the width of the resulting animated GIF in pixels
53
- # @param height [Integer, 0] the height of the resulting animated GIF in pixels
54
- # @param start [Integer, 0] the starting position of the video that should be converted into a animated GIF
55
- # @param duration [Integer, 0] the length in seconds of the video that should be converted into a animated GIF
56
- # @param speed [Float, 0] the speed of the animated GIF from 0.2 to 10 times the original speed
57
- # @param framesPerSecond [Float, 0] the number of frames per second that should be captured from the video. From a minimum of 0.2 to a maximum of 60
58
- # @param repeat [Integer, 0] the number of times to loop the animated GIF. If 0 it will loop forever
59
- # @param reverse [Boolean, false] if true the frames of the animated GIF are reversed
60
- # @param customWaterMarkId [String, nil] add a custom watermark to the animated GIF
61
- # @param quality [Integer, -1] the quality of the image where 0 is poor and 100 excellent. The default is -1 which uses the recommended quality
62
- # @param country [String, nil] request the screenshot from different countries: Default, UK or US
58
+ # @param url [String] The URL of the video to convert into a animated GIF
59
+ # @param options [AnimationOptions, nil] a instance of the AnimationOptions class that defines any special options to use when creating the animated GIF
63
60
  # @return [void]
64
- def set_animation_options(url, customId = nil, width = 0, height = 0, start = 0, duration = 0, speed = 0, framesPerSecond = 0, repeat = 0, reverse = false, customWaterMarkId = nil, quality = -1, country = nil)
65
- @@startDelay = 0;
66
- @@request = WebServicesBaseURL + "takeanimation.ashx?key=" + CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(nil_check(url))+"&width="+nil_int_check(width)+"&height="+nil_int_check(height)+"&duration="+nil_int_check(duration)+"&speed="+nil_float_check(speed)+"&start="+nil_int_check(start)+"&customid="+CGI.escape(nil_check(customId))+"&fps="+nil_float_check(framesPerSecond)+"&repeat="+nil_int_check(repeat)+"&customwatermarkid="+CGI.escape(nil_check(customWaterMarkId))+"&reverse="+b_to_str(reverse)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
67
- @@signaturePartOne = @applicationSecret+"|"+nil_check(url)+"|"
68
- @@signaturePartTwo = "|"+nil_int_check(height)+"|"+nil_int_check(width)+"|"+nil_check(customId)+"|"+nil_float_check(framesPerSecond)+"|"+nil_float_check(speed)+"|"+nil_int_check(duration)+"|"+nil_int_check(repeat)+"|"+b_to_str(reverse)+"|"+nil_int_check(start)+"|"+nil_check(customWaterMarkId)+"|"+nil_check(country)+"|"+nil_int_check(quality)
61
+ def url_to_animation(url, options = nil)
62
+
63
+ if options == nil
64
+ options = AnimationOptions.new()
65
+ end
66
+
67
+ @request = Request.new(WebServicesBaseURLGet + "takeanimation.ashx", false, options, url)
69
68
  return nil
70
69
  end
71
70
 
72
-
73
- # Sets the parameters required to take a screenshot of a web page.
71
+ # This method specifies the URL that should be converted into a image screenshot
74
72
  #
75
- # @param url [String] the URL that the screenshot should be made of
76
- # @param customId [String, nil] custom identifier that you can pass through to the screenshot web service. This will be returned with the callback URL you have specified.
77
- # @param browserWidth [Integer, nil] the width of the browser in pixels
78
- # @param browserHeight [Integer, nil] the height of the browser in pixels
79
- # @param width [Integer, nil] the width of the resulting thumbnail in pixels
80
- # @param height [Integer, nil] the height of the resulting thumbnail in pixels
81
- # @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
82
- # @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
83
- # @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
84
- # @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
85
- # @param customWaterMarkId [String, nil] add a custom watermark to the image
86
- # @param quality [Integer, -1] the quality of the image where 0 is poor and 100 excellent
87
- # @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
73
+ # @param url [String] the URL to capture as a screenshot
74
+ # @param options [ImageOptions, nil] a instance of the ImageOptions class that defines any special options to use when creating the screenshot
88
75
  # @return [void]
89
- def set_image_options(url, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil, requestAs = 0, customWaterMarkId = nil, quality = -1, country = nil)
76
+ def url_to_image(url, options = nil)
90
77
 
91
- if delay == nil
92
- @@startDelay = 0
93
- else
94
- @@startDelay = delay
78
+ if options == nil
79
+ options = ImageOptions.new()
95
80
  end
96
-
97
- @@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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
98
- @@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
99
- @@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)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)+"|"+nil_int_check(quality)
100
-
81
+
82
+ @request = Request.new(WebServicesBaseURLGet + TakePicture, false, options, url)
101
83
  return nil
102
84
  end
103
85
 
104
- # Sets the parameters required to extract one or more tables from a web page.
86
+ # This method specifies the HTML that should be converted into a image
105
87
  #
106
- # @param url [String] the URL that the should be used to extract tables
107
- # @param customId [String, nil] a custom identifier that you can pass through to the web service. This will be returned with the callback URL you have specified.
108
- # @param tableNumberToInclude [Integer, 1] the index of the table to be converted, were all tables in a web page are ordered from the top of the web page to bottom
109
- # @param format [String, 'csv'] the format the table should be in: csv, xlsx
110
- # @param includeHeaderNames [Boolean, true] if true header names will be included in the table
111
- # @param includeAllTables [Boolean, true] if true all table on the web page will be extracted with each table appearing in a separate spreadsheet sheet. Only available with the XLSX format.
112
- # @param targetElement [String, nil] the id of the only HTML element in the web page that should be used to extract tables from
113
- # @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
114
- # @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
115
- # @return [void]
116
- def set_table_options(url, customId = nil, tableNumberToInclude = 1, format = 'csv', includeHeaderNames = true, includeAllTables = false, targetElement = nil, requestAs = 0, country = nil)
117
- @@startDelay = 0
118
-
119
- @@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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&callback="
120
- @@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
121
- @@signaturePartTwo = "|"+nil_check(customId)+"|"+nil_int_check(tableNumberToInclude)+"|"+b_to_str(includeAllTables)+"|"+b_to_str(includeHeaderNames)+"|"+nil_check(targetElement)+"|"+nil_check(format)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)
88
+ # @param html [String] the HTML to convert into a image
89
+ # @param options [ImageOptions, nil] a instance of the ImageOptions class that defines any special options to use when creating the image
90
+ # @return [void]
91
+ def html_to_image(html, options = nil)
122
92
 
93
+ if options == nil
94
+ options = ImageOptions.new()
95
+ end
96
+
97
+ @request = Request.new(WebServicesBaseURLPost + TakePicture, true, options, html)
123
98
  return nil
124
- end
99
+ end
125
100
 
126
- # Sets the parameters required to convert a web page into a PDF.
101
+ # This method specifies a HTML file that should be converted into a image
102
+ #
103
+ # @param path [String] the file path of a HTML file to convert into a image
104
+ # @param options [ImageOptions, nil] a instance of the ImageOptions class that defines any special options to use when creating the image
105
+ # @return [void]
106
+ def file_to_image(path, options = nil)
107
+ html_to_image(read_html_file(path), options)
108
+ end
109
+
110
+ # This method specifies the URL that the HTML tables should be extracted from
127
111
  #
128
- # @param url url [String] the URL that the should be converted into a pdf
129
- # @param customId [String, nil] a custom identifier that you can pass through to the web service. This will be returned with the callback URL you have specified.
130
- # @param includeBackground [Boolean, true] if true the background of the web page should be included in the screenshot
131
- # @param pagesize [String, 'A4'] the page size of the PDF to be returned: 'A3', 'A4', 'A5', 'B3', 'B4', 'B5', 'Letter'.
132
- # @param orientation [String, 'Portrait'] the orientation of the PDF to be returned: 'Landscape' or 'Portrait'
133
- # @param includeLinks [Boolean, true] true if links should be included in the PDF
134
- # @param includeOutline [Boolean, false] True if the PDF outline should be included
135
- # @param title [String, nil] provide a title to the PDF document
136
- # @param coverURL [String, nil] the URL of a web page that should be used as a cover page for the PDF
137
- # @param marginTop [Integer, 10] the margin that should appear at the top of the PDF document page
138
- # @param marginLeft [Integer, 10] the margin that should appear at the left of the PDF document page
139
- # @param marginBottom [Integer, 10] the margin that should appear at the bottom of the PDF document page
140
- # @param marginRight [Integer, 10] the margin that should appear at the right of the PDF document
141
- # @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
142
- # @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
143
- # @param templateId [String, nil] add a PDF template ID that specifies the header and footer of the PDF document
144
- # @param customWaterMarkId [String, nil] add a custom watermark to each page of the PDF document
145
- # @param quality [Integer, -1] the quality of the PDF where 0 is poor and 100 excellent
146
- # @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
112
+ # @param url [String] the URL to extract HTML tables from
113
+ # @param options [TableOptions, nil] a instance of the TableOptions class that defines any special options to use when converting the HTML table
147
114
  # @return [void]
148
- def 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, requestAs = 0, templateId = nil, customWaterMarkId = nil, quality = -1, country = nil)
149
- pagesize = nil_check(pagesize).upcase
150
- $orientation = nil_check(orientation).capitalize
151
-
152
- if delay == nil
153
- @@startDelay = 0
154
- else
155
- @@startDelay = delay
156
- end
115
+ def url_to_table(url, options = nil)
116
+
117
+ if options == nil
118
+ options = TableOptions.new()
119
+ end
120
+
121
+ @request = Request.new(WebServicesBaseURLGet + TakeTable, false, options, url)
122
+ return nil
123
+ end
124
+
125
+ # This method specifies the HTML that the HTML tables should be extracted from
126
+ #
127
+ # @param html [String] the HTML to extract HTML tables from
128
+ # @param options [TableOptions, nil] a instance of the TableOptions class that defines any special options to use when converting the HTML table
129
+ # @return [void]
130
+ def html_to_table(html, options = nil)
157
131
 
158
- @@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))+"&templateid="+CGI.escape(nil_check(templateId))+"&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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
132
+ if options == nil
133
+ options = TableOptions.new()
134
+ end
135
+
136
+ @request = Request.new(WebServicesBaseURLPost + TakeTable, true, options, html)
137
+ return nil
138
+ end
159
139
 
160
- @@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
161
- @@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)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)+"|"+nil_int_check(quality)+"|"+nil_check(templateId)
140
+ # This method specifies a HTML file that the HTML tables should be extracted from
141
+ #
142
+ # @param path [String] the file path of a HTML file to extract HTML tables from
143
+ # @param options [TableOptions, nil] a instance of the TableOptions class that defines any special options to use when converting the HTML table
144
+ # @return [void]
145
+ def file_to_table(path, options = nil)
146
+ html_to_table(read_html_file(path), options)
147
+ end
148
+
149
+ # This method specifies the URL that should be converted into a PDF
150
+ #
151
+ # @param url [String] the URL to capture as a PDF
152
+ # @param options [PDFOptions, nil] a instance of the PDFOptions class that defines any special options to use when creating the PDF
153
+ # @return [void]
154
+ def url_to_pdf(url, options = nil)
155
+
156
+ if options == nil
157
+ options = PDFOptions.new()
158
+ end
159
+
160
+ @request = Request.new(WebServicesBaseURLGet + TakePDF, false, options, url)
161
+ return nil
162
+ end
163
+
164
+ # This method specifies the HTML that should be converted into a PDF
165
+ #
166
+ # @param html [String] the HTML to convert into a PDF
167
+ # @param options [PDFOptions, nil] a instance of the PDFOptions class that defines any special options to use when creating the PDF
168
+ # @return [void]
169
+ def html_to_pdf(html, options = nil)
162
170
 
163
- return nil
164
- end
171
+ if options == nil
172
+ options = PDFOptions.new()
173
+ end
174
+
175
+ @request = Request.new(WebServicesBaseURLPost + TakePDF, true, options, html)
176
+ return nil
177
+ end
178
+
179
+ # This method specifies a HTML file that should be converted into a PDF
180
+ #
181
+ # @param path [String] the file path of a HTML file to convert into a PDF
182
+ # @param options [PDFOptions, nil] a instance of the PDFOptions class that defines any special options to use when creating the PDF
183
+ # @return [void]
184
+ def file_to_pdf(path, options = nil)
185
+ html_to_pdf(read_html_file(path), options)
186
+ end
165
187
 
166
188
  # Calls the GrabzIt web service to take the screenshot
167
189
  #
168
190
  # The handler will be passed a URL with the following query string parameters:
169
191
  # - message (is any error message associated with the screenshot)
170
- # - customId (is a custom id you may have specified in the {#set_image_options}, {#set_table_options} or {#set_pdf_options} method)
192
+ # - customId (is a custom id you may have specified in the [AnimationOptions], [ImageOptions], [PDFOptions] or [TableOptions] classes)
171
193
  # - id (is the unique id of the screenshot which can be used to retrieve the screenshot with the {#get_result} method)
172
194
  # - filename (is the filename of the screenshot)
173
195
  # - format (is the format of the screenshot)
@@ -176,16 +198,21 @@ module GrabzIt
176
198
  # @return [String] the unique identifier of the screenshot. This can be used to get the screenshot with the {#get_result} method
177
199
  # @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
178
200
  def save(callBackURL = nil)
179
- if @@signaturePartOne == nil && @@signaturePartTwo == nil && @@request == nil
201
+ if @request == nil
180
202
  raise GrabzItException.new("No screenshot parameters have been set.", GrabzItException::PARAMETER_MISSING_PARAMETERS)
181
203
  end
182
-
183
- sig = encode(@@signaturePartOne+nil_check(callBackURL)+@@signaturePartTwo)
184
- currentRequest = @@request
185
204
 
186
- currentRequest += CGI.escape(nil_check(callBackURL))+"&sig="+sig
205
+ sig = encode(@request.options()._getSignatureString(@applicationSecret, callBackURL, @request.getTargetUrl()))
206
+
207
+ data = nil
187
208
 
188
- return get_result_value(get(currentRequest), "ID")
209
+ if !@request.isPost()
210
+ data = get(@request.url() + "?" + URI.encode_www_form(@request.options()._getParameters(@applicationKey, sig, callBackURL, "url", @request.data())))
211
+ else
212
+ data = post(@request.url(), URI.encode_www_form(@request.options()._getParameters(@applicationKey, sig, callBackURL, "html", @request.data())))
213
+ end
214
+
215
+ return get_result_value(data, "ID")
189
216
  end
190
217
 
191
218
  # Calls the GrabzIt web service to take the screenshot and saves it to the target path provided. if no target path is provided
@@ -205,7 +232,7 @@ module GrabzIt
205
232
  end
206
233
 
207
234
  #Wait for it to be possibly ready
208
- sleep((@@startDelay / 1000) + 3)
235
+ sleep((@request.options().startDelay() / 1000) + 3)
209
236
 
210
237
  #Wait for it to be ready.
211
238
  while true do
@@ -248,7 +275,7 @@ module GrabzIt
248
275
  return nil
249
276
  end
250
277
 
251
- result = get(WebServicesBaseURL + "getstatus.ashx?id=" + nil_check(id))
278
+ result = get(WebServicesBaseURLGet + "getstatus.ashx?id=" + GrabzIt::Utility.nil_check(id))
252
279
 
253
280
  doc = REXML::Document.new(result)
254
281
 
@@ -270,7 +297,7 @@ module GrabzIt
270
297
  return nil
271
298
  end
272
299
 
273
- return get(WebServicesBaseURL + "getfile.ashx?id=" + nil_check(id))
300
+ return get(WebServicesBaseURLGet + "getfile.ashx?id=" + GrabzIt::Utility.nil_check(id))
274
301
  end
275
302
 
276
303
  # Get all the cookies that GrabzIt is using for a particular domain. This may include your user set cookies as well
@@ -279,11 +306,11 @@ module GrabzIt
279
306
  # @return [Array<Cookie>] an array of cookies
280
307
  # @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
281
308
  def get_cookies(domain)
282
- sig = encode(nil_check(@applicationSecret)+"|"+nil_check(domain))
309
+ sig = encode(GrabzIt::Utility.nil_check(@applicationSecret)+"|"+GrabzIt::Utility.nil_check(domain))
283
310
 
284
- qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&sig="+sig
311
+ qs = "key=" +CGI.escape(GrabzIt::Utility.nil_check(@applicationKey))+"&domain="+CGI.escape(GrabzIt::Utility.nil_check(domain))+"&sig="+sig
285
312
 
286
- result = get(WebServicesBaseURL + "getcookies.ashx?" + qs)
313
+ result = get(WebServicesBaseURLGet + "getcookies.ashx?" + qs)
287
314
 
288
315
  doc = REXML::Document.new(result)
289
316
 
@@ -317,11 +344,15 @@ module GrabzIt
317
344
  # @return [Boolean] returns true if the cookie was successfully set
318
345
  # @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
319
346
  def set_cookie(name, domain, value = "", path = "/", httponly = false, expires = "")
320
- sig = encode(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|"+nil_check(value)+"|"+nil_check(path)+"|"+b_to_str(httponly)+"|"+nil_check(expires)+"|0")
347
+ sig = encode(GrabzIt::Utility.nil_check(@applicationSecret)+"|"+GrabzIt::Utility.nil_check(name)+"|"+GrabzIt::Utility.nil_check(domain)+
348
+ "|"+GrabzIt::Utility.nil_check(value)+"|"+GrabzIt::Utility.nil_check(path)+"|"+GrabzIt::Utility.b_to_str(httponly)+
349
+ "|"+GrabzIt::Utility.nil_check(expires)+"|0")
321
350
 
322
- 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="+CGI.escape(nil_check(expires))+"&sig="+sig
351
+ qs = "key=" +CGI.escape(GrabzIt::Utility.nil_check(@applicationKey))+"&domain="+CGI.escape(GrabzIt::Utility.nil_check(domain))+"&name="+
352
+ CGI.escape(GrabzIt::Utility.nil_check(name))+"&value="+CGI.escape(GrabzIt::Utility.nil_check(value))+"&path="+CGI.escape(GrabzIt::Utility.nil_check(path))+
353
+ "&httponly="+GrabzIt::Utility.b_to_str(httponly)+"&expires="+CGI.escape(GrabzIt::Utility.nil_check(expires))+"&sig="+sig
323
354
 
324
- return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString)
355
+ return (get_result_value(get(WebServicesBaseURLGet + "setcookie.ashx?" + qs), "Result") == TrueString)
325
356
  end
326
357
 
327
358
  # Delete a custom cookie or block a global cookie from being used
@@ -331,11 +362,13 @@ module GrabzIt
331
362
  # @return [Boolean] returns true if the cookie was successfully set
332
363
  # @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
333
364
  def delete_cookie(name, domain)
334
- sig = encode(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|1")
365
+ sig = encode(GrabzIt::Utility.nil_check(@applicationSecret)+"|"+GrabzIt::Utility.nil_check(name)+
366
+ "|"+GrabzIt::Utility.nil_check(domain)+"|1")
335
367
 
336
- qs = "key=" + CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&name="+CGI.escape(nil_check(name))+"&delete=1&sig="+sig
368
+ qs = "key=" + CGI.escape(GrabzIt::Utility.nil_check(@applicationKey))+"&domain="+CGI.escape(GrabzIt::Utility.nil_check(domain))+
369
+ "&name="+CGI.escape(GrabzIt::Utility.nil_check(name))+"&delete=1&sig="+sig
337
370
 
338
- return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString)
371
+ return (get_result_value(get(WebServicesBaseURLGet + "setcookie.ashx?" + qs), "Result") == TrueString)
339
372
  end
340
373
 
341
374
  # Get your uploaded custom watermark
@@ -345,7 +378,7 @@ module GrabzIt
345
378
  def get_watermark(identifier)
346
379
  watermarks = get_watermarks(identifier)
347
380
  if watermarks.length == 1
348
- return watermarks[0];
381
+ return watermarks[0]
349
382
  end
350
383
 
351
384
  return nil
@@ -370,7 +403,8 @@ module GrabzIt
370
403
  if !File.file?(path)
371
404
  raise "File: " + path + " does not exist"
372
405
  end
373
- sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier)+"|"+nil_int_check(xpos)+"|"+nil_int_check(ypos));
406
+ sig = encode(GrabzIt::Utility.nil_check(@applicationSecret)+"|"+GrabzIt::Utility.nil_check(identifier)+"|"+GrabzIt::Utility.nil_int_check(xpos)+
407
+ "|"+GrabzIt::Utility.nil_int_check(ypos))
374
408
 
375
409
  boundary = '--------------------------'+Time.now.to_f.to_s
376
410
 
@@ -387,19 +421,19 @@ module GrabzIt
387
421
  post_body << "\r\n--"+boundary+"\r\n"
388
422
 
389
423
  post_body << "Content-Disposition: form-data; name=\"key\"\r\n\r\n"
390
- post_body << CGI.escape(nil_check(@applicationKey))
424
+ post_body << CGI.escape(GrabzIt::Utility.nil_check(@applicationKey))
391
425
  post_body << "\r\n--"+boundary+"\r\n"
392
426
 
393
427
  post_body << "Content-Disposition: form-data; name=\"identifier\"\r\n\r\n"
394
- post_body << CGI.escape(nil_check(identifier))
428
+ post_body << CGI.escape(GrabzIt::Utility.nil_check(identifier))
395
429
  post_body << "\r\n--"+boundary+"\r\n"
396
430
 
397
431
  post_body << "Content-Disposition: form-data; name=\"xpos\"\r\n\r\n"
398
- post_body << nil_check(xpos)
432
+ post_body << GrabzIt::Utility.nil_check(xpos)
399
433
  post_body << "\r\n--"+boundary+"\r\n"
400
434
 
401
435
  post_body << "Content-Disposition: form-data; name=\"ypos\"\r\n\r\n"
402
- post_body << nil_check(ypos)
436
+ post_body << GrabzIt::Utility.nil_check(ypos)
403
437
  post_body << "\r\n--"+boundary+"\r\n"
404
438
 
405
439
  post_body << "Content-Disposition: form-data; name=\"sig\"\r\n\r\n"
@@ -422,70 +456,20 @@ module GrabzIt
422
456
  # @return [Boolean] returns true if the watermark was successfully deleted
423
457
  # @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
424
458
  def delete_watermark(identifier)
425
- sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier))
426
-
427
- qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig
428
-
429
- return (get_result_value(get(WebServicesBaseURL + "deletewatermark.ashx?" + qs), "Result") == TrueString)
430
- end
459
+ sig = encode(GrabzIt::Utility.nil_check(@applicationSecret)+"|"+GrabzIt::Utility.nil_check(identifier))
431
460
 
432
- # This method calls the GrabzIt web service to take the screenshot.
433
- #
434
- # @param url [String] the URL that the screenshot should be made of
435
- # @param callback [String, nil] the handler the GrabzIt web service should call after it has completed its work
436
- # @param customId [String, nil] custom identifier that you can pass through to the screenshot webservice. This will be returned with the callback URL you have specified.
437
- # @param browserWidth [Integer, nil] the width of the browser in pixels
438
- # @param browserHeight [Integer, nil] the height of the browser in pixels
439
- # @param width [Integer, nil] the width of the resulting thumbnail in pixels
440
- # @param height [Integer, nil] the height of the resulting thumbnail in pixels
441
- # @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
442
- # @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
443
- # @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
444
- # @return [String] returns the unique identifier of the screenshot. This can be used to get the screenshot with the get_result method
445
- # @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
446
- # @deprecated Use {#set_image_options} and {#save} instead.
447
- def take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
448
- set_image_options(url, customId, browserWidth, browserHeight, width, height, format, delay, targetElement)
449
- return save(callback)
450
- end
461
+ qs = "key=" +CGI.escape(GrabzIt::Utility.nil_check(@applicationKey))+"&identifier="+CGI.escape(GrabzIt::Utility.nil_check(identifier))+"&sig="+sig
451
462
 
452
- # This method takes the screenshot and then saves the result to a file. WARNING this method is synchronous
453
- #
454
- # @param url [String] the URL that the screenshot should be made of
455
- # @param saveToFile [String] the file path that the screenshot should saved to
456
- # @param browserWidth [Integer, nil] the width of the browser in pixels
457
- # @param browserHeight [Integer, nil] the height of the browser in pixels
458
- # @param width [Integer, nil] the width of the resulting thumbnail in pixels
459
- # @param height [Integer, nil] the height of the resulting thumbnail in pixels
460
- # @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
461
- # @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
462
- # @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
463
- # @example Synchronously save the screenshot to test.jpg
464
- # save_picture('images/test.jpg')
465
- # @return [Boolean] returns the true if it is successfull otherwise it throws an exception
466
- # @raise [RuntimeError] if the screenshot cannot be saved a RuntimeError will be raised that will contain an explanation
467
- # @deprecated Use {#set_image_options} and {#save_to} instead.
468
- def save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
469
- set_image_options(url, nil, browserWidth, browserHeight, width, height, format, delay, targetElement)
470
- return save_to(saveToFile)
463
+ return (get_result_value(get(WebServicesBaseURLGet + "deletewatermark.ashx?" + qs), "Result") == TrueString)
471
464
  end
472
465
 
473
- # This method returns the image itself. If nothing is returned then something has gone wrong or the image is not ready yet.
474
- #
475
- # @param id [String] the id of the screenshot
476
- # @return [Object] returns the screenshot
477
- # @deprecated Use {#get_result} instead.
478
- def get_picture(id)
479
- return get_result(id)
480
- end
481
-
482
466
  private
483
467
  def get_watermarks(identifier = nil)
484
- sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier))
468
+ sig = encode(GrabzIt::Utility.nil_check(@applicationSecret)+"|"+GrabzIt::Utility.nil_check(identifier))
485
469
 
486
- qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig
470
+ qs = "key=" +CGI.escape(GrabzIt::Utility.nil_check(@applicationKey))+"&identifier="+CGI.escape(GrabzIt::Utility.nil_check(identifier))+"&sig="+sig
487
471
 
488
- result = get(WebServicesBaseURL + "getwatermarks.ashx?" + qs)
472
+ result = get(WebServicesBaseURLGet + "getwatermarks.ashx?" + qs)
489
473
 
490
474
  doc = REXML::Document.new(result)
491
475
 
@@ -509,6 +493,15 @@ module GrabzIt
509
493
  response_check(response)
510
494
  return response.body
511
495
  end
496
+
497
+ private
498
+ def post(url, params)
499
+ headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
500
+ uri = URI.parse(url)
501
+ response = Net::HTTP.start(uri.host, uri.port) { |http| http.post(uri.request_uri, params, headers) }
502
+ response_check(response)
503
+ return response.body
504
+ end
512
505
 
513
506
  private
514
507
  def response_check(response)
@@ -519,37 +512,6 @@ module GrabzIt
519
512
  raise GrabzItException.new("A network error occured when connecting to the GrabzIt servers.", GrabzItException::NETWORK_GENERAL_ERROR)
520
513
  end
521
514
  end
522
-
523
- private
524
- def b_to_str(bValue)
525
- if bValue
526
- return 1.to_s
527
- end
528
- return 0.to_s
529
- end
530
-
531
- private
532
- def nil_check(param)
533
- if param == nil
534
- return ""
535
- end
536
- return param
537
- end
538
-
539
- private
540
- def nil_int_check(param)
541
- return param.to_i.to_s
542
- end
543
-
544
- private
545
- def nil_float_check(param)
546
- val = param.to_f
547
- if ((val % 1) == 0)
548
- return val.to_i.to_s
549
- end
550
-
551
- return val.to_s
552
- end
553
515
 
554
516
  private
555
517
  def check_for_exception(doc)
@@ -570,6 +532,16 @@ module GrabzIt
570
532
  return Digest::MD5.hexdigest(text.encode('ascii', {:invalid => :replace, :undef => :replace, :replace => '?'}))
571
533
  end
572
534
 
535
+ private
536
+ def read_html_file(path)
537
+ if !File.file?(path)
538
+ raise "File: " + path + " does not exist"
539
+ end
540
+
541
+ file = File.open(path, "rb")
542
+ return file.read
543
+ end
544
+
573
545
  private
574
546
  def get_result_value(result, field)
575
547
  doc = REXML::Document.new(result)
@@ -1,9 +1,9 @@
1
1
  module GrabzIt
2
2
  # This class represents the cookies stored in GrabzIt
3
- # @version 2.1
3
+ # @version 3.0
4
4
  # @author GrabzIt
5
5
  class Cookie
6
- # @api private
6
+ # @!visibility private
7
7
  def initialize(name = '', domain = '', value = '', path = '', httpOnly = false, expires = nil, type = nil)
8
8
  @Name = name
9
9
  @Value = value
@@ -1,6 +1,6 @@
1
1
  module GrabzIt
2
2
  # This class represents the exceptions thrown by GrabzIt
3
- # @version 2.1
3
+ # @version 3.0
4
4
  # @author GrabzIt
5
5
  class GrabzItException < Exception
6
6
 
@@ -59,7 +59,8 @@ module GrabzIt
59
59
  PARAMETER_SPEED_TOO_SLOW = 154
60
60
  PARAMETER_INVALID_ANIMATION_COMBINATION = 155
61
61
  PARAMETER_START_TOO_SMALL = 156
62
- PARAMETER_DURATION_TOO_LARGE = 157
62
+ PARAMETER_DURATION_TOO_LARGE = 157
63
+ PARAMETER_NO_HTML = 163
63
64
  NETWORK_SERVER_OFFLINE = 200
64
65
  NETWORK_GENERAL_ERROR = 201
65
66
  NETWORK_DDOS_ATTACK = 202