Class: GrabzIt::Client
- Inherits:
-
Object
- Object
- GrabzIt::Client
- Defined in:
- lib/grabz_it/client.rb
Constant Summary collapse
- API_BASE_URL =
"http://grabz.it/services/"
Instance Attribute Summary collapse
-
#app_key ⇒ Object
Returns the value of attribute app_key.
-
#app_secret ⇒ Object
Returns the value of attribute app_secret.
-
#browser_height ⇒ Object
Returns the value of attribute browser_height.
-
#browser_width ⇒ Object
Returns the value of attribute browser_width.
-
#callback_url ⇒ Object
Returns the value of attribute callback_url.
-
#custom_id ⇒ Object
Returns the value of attribute custom_id.
-
#delay ⇒ Object
Returns the value of attribute delay.
-
#format ⇒ Object
Returns the value of attribute format.
-
#output_height ⇒ Object
Returns the value of attribute output_height.
-
#output_width ⇒ Object
Returns the value of attribute output_width.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#get_cookie_jar(domain) ⇒ GrabzIt::CookieJar
Get all the cookies that GrabzIt is using for a particular domain.
-
#get_picture(screenshot_id) ⇒ GrabzIt::Image
Get the screenshot image.
-
#get_status(screenshot_id) ⇒ GrabzIt::Status
Get the current status of a GrabzIt screenshot.
-
#initialize(app_key, app_secret) ⇒ Client
constructor
Initialize the
GrabzIt::Client
class. -
#save_picture(target_path, options = {}) ⇒ GrabzIt::Response
Calls the GrabzIt web service to take the screenshot and saves it to the target path provided.
-
#take_picture(options = {}) ⇒ GrabzIt::Response
Calls the GrabzIt web service to take the screenshot.
Constructor Details
#initialize(app_key, app_secret) ⇒ Client
Initialize the GrabzIt::Client
class
16 17 18 19 20 |
# File 'lib/grabz_it/client.rb', line 16 def initialize(app_key, app_secret) @app_key = app_key @app_secret = app_secret raise(ArgumentError, "You must provide app_key and app_secret") unless @app_key && @app_secret end |
Instance Attribute Details
#app_key ⇒ Object
Returns the value of attribute app_key.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def app_key @app_key end |
#app_secret ⇒ Object
Returns the value of attribute app_secret.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def app_secret @app_secret end |
#browser_height ⇒ Object
Returns the value of attribute browser_height.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def browser_height @browser_height end |
#browser_width ⇒ Object
Returns the value of attribute browser_width.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def browser_width @browser_width end |
#callback_url ⇒ Object
Returns the value of attribute callback_url.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def callback_url @callback_url end |
#custom_id ⇒ Object
Returns the value of attribute custom_id.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def custom_id @custom_id end |
#delay ⇒ Object
Returns the value of attribute delay.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def delay @delay end |
#format ⇒ Object
Returns the value of attribute format.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def format @format end |
#output_height ⇒ Object
Returns the value of attribute output_height.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def output_height @output_height end |
#output_width ⇒ Object
Returns the value of attribute output_width.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def output_width @output_width end |
#url ⇒ Object
Returns the value of attribute url.
3 4 5 |
# File 'lib/grabz_it/client.rb', line 3 def url @url end |
Instance Method Details
#get_cookie_jar(domain) ⇒ GrabzIt::CookieJar
Get all the cookies that GrabzIt is using for a particular domain. This may include your user set cookies as well.
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/grabz_it/client.rb', line 153 def (domain) action = "getcookies.ashx" sig = Digest::MD5.hexdigest(@app_secret + "|" + domain) params = { :key => URI.escape(@app_key), :domain => URI.escape(domain), :sig => sig } response_body = query_api(action, params) = CookieJar.new(response_body) raise . if . end |
#get_picture(screenshot_id) ⇒ GrabzIt::Image
Get the screenshot image
197 198 199 200 201 202 |
# File 'lib/grabz_it/client.rb', line 197 def get_picture(screenshot_id) action = "getpicture.ashx" response = query_api(action, { :id => screenshot_id }) image = Image.new(response) image end |
#get_status(screenshot_id) ⇒ GrabzIt::Status
Get the current status of a GrabzIt screenshot.
134 135 136 137 138 139 140 |
# File 'lib/grabz_it/client.rb', line 134 def get_status(screenshot_id) action = "getstatus.ashx" response_body = query_api(action, { :id => screenshot_id }) status = Status.new(response_body) raise status. if status. status end |
#save_picture(target_path, options = {}) ⇒ GrabzIt::Response
Calls the GrabzIt web service to take the screenshot and saves it to the target path provided. Warning, this is a SYNCHONOUS method and can take up to 5 minutes before a response.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/grabz_it/client.rb', line 55 def save_picture(target_path, = {}) response = take_picture() # Wait for the response to be ready iterations = 0 while true do status = get_status(response.screenshot_id) if status.failed? raise "The screenshot did not complete with errors: " + status. elsif status.available? image = get_picture(response.screenshot_id) image.save(target_path) break end # Check again in 1 second with a max of 5 minutes if iterations <= (5 * 60) sleep(1) else raise Timeout::Error end end true end |
#take_picture(options = {}) ⇒ GrabzIt::Response
Calls the GrabzIt web service to take the screenshot.
114 115 116 117 118 119 120 121 |
# File 'lib/grabz_it/client.rb', line 114 def take_picture( = {}) () action = "takepicture.ashx" response_body = query_api(action, generate_params) response = Response.new(response_body) raise response. if response. response end |