Class: GrabzIt::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/grabz_it/client.rb

Constant Summary collapse

API_BASE_URL =
"http://grabz.it/services/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_key, app_secret) ⇒ Client

Initialize the GrabzIt::Client class

Parameters:

  • app_key (String)

    Application Key provided by grabz.it

  • app_secret (String)

    Application Secret provided by grabz.it

Raises:

  • (ArgumentError)

    Exception raised if the app_key or app_secret is not provided



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_keyObject

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_secretObject

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_heightObject

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_widthObject

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_urlObject

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_idObject

Returns the value of attribute custom_id.



3
4
5
# File 'lib/grabz_it/client.rb', line 3

def custom_id
  @custom_id
end

#delayObject

Returns the value of attribute delay.



3
4
5
# File 'lib/grabz_it/client.rb', line 3

def delay
  @delay
end

#formatObject

Returns the value of attribute format.



3
4
5
# File 'lib/grabz_it/client.rb', line 3

def format
  @format
end

#output_heightObject

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_widthObject

Returns the value of attribute output_width.



3
4
5
# File 'lib/grabz_it/client.rb', line 3

def output_width
  @output_width
end

#urlObject

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 all the cookies that GrabzIt is using for a particular domain. This may include your user set cookies as well.

Examples:

client = GrabzIt::Client.new('TEST_KEY', 'TEST_SECRET')
cookie_jar = client.get_cookie_jar('google')

Parameters:

  • domain (String)

    The domain of the cookie

Returns:



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/grabz_it/client.rb', line 153

def get_cookie_jar(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)
  cookie_jar = CookieJar.new(response_body)
  raise cookie_jar.message if cookie_jar.message
  cookie_jar
end

#get_picture(screenshot_id) ⇒ GrabzIt::Image

Get the screenshot image

Examples:

client = GrabzIt::Client.new('TEST_KEY', 'TEST_SECRET')
image = client.get_image('Y2F2bmViQGdtYWlsLmNvbQ==-20943258e37c4fc28c4977cd76c40f58')
puts image.content_type
=> 'image/png'
puts image.size
=> 129347
image.save("/tmp/myimage.png")
File.exist?("/tmp/myimage.png")
=> true

Parameters:

  • screenshot_id (String)

    The id of the screenshot provided by the GrabzIt api

Returns:



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.

Examples:

client = GrabzIt::Client.new('TEST_KEY', 'TEST_SECRET')
status = client.get_status('Y2F2bmViQGdtYWlsLmNvbQ==-20943258e37c4fc28c4977cd76c40f58')

Parameters:

  • screenshot_id (String)

    The id of the screenshot provided by the GrabzIt api

Returns:



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.message if status.message
  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.

Examples:

client = GrabzIt::Client.new('TEST_KEY', 'TEST_SECRET')
options = {
  :url            => 'http://grabz.it',
  :callback_url   => 'http://example.com/callback',
  :browser_width  => 1024,
  :browser_height => 768,
  :output_width   => 800,
  :output_height  => 600,
  :custom_id      => '12345',
  :format         => 'png',
  :delay          => 1000
}
response = client.save_picture('google', options)

Parameters:

  • target_path (String)

    File path that the file should be saved to (including file name and extension)

  • options (Hash) (defaults to: {})

    Data that is to be passed to the web service

Options Hash (options):

  • :url (String)

    The URL that the screenshot should be made of

  • :callback_url (String)

    The handler the GrabzIt web service should call after it has completed its work

  • :custom_id (String)

    A custom identifier that you can pass through to the screenshot webservice. This will be returned with the callback URL you have specified.

  • :browser_width (Integer)

    The width of the browser in pixels

  • :browser_height (Integer)

    The height of the browser in pixels

  • :output_width (Integer)

    The width of the resulting thumbnail in pixels

  • :output_height (Integer)

    The height of the resulting thumbnail in pixels

  • :format (String)

    The format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png

  • :delay (Integer)

    The number of milliseconds to wait before taking the screenshot

Returns:



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, options = {})
  response = take_picture(options)

  # 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.message

    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.

Examples:

client = GrabzIt::Client.new('TEST_KEY', 'TEST_SECRET')
options = {
  :url            => 'http://grabz.it',
  :callback_url   => 'http://example.com/callback',
  :browser_width  => 1024,
  :browser_height => 768,
  :output_width   => 800,
  :output_height  => 600,
  :custom_id      => '12345',
  :format         => 'png',
  :delay          => 1000
}
response = client.take_picture(options)

Parameters:

  • options (Hash) (defaults to: {})

    Data that is to be passed to the web service

Options Hash (options):

  • :url (String)

    The URL that the screenshot should be made of

  • :callback_url (String)

    The handler the GrabzIt web service should call after it has completed its work

  • :custom_id (String)

    A custom identifier that you can pass through to the screenshot webservice. This will be returned with the callback URL you have specified.

  • :browser_width (Integer)

    The width of the browser in pixels

  • :browser_height (Integer)

    The height of the browser in pixels

  • :output_width (Integer)

    The width of the resulting thumbnail in pixels

  • :output_height (Integer)

    The height of the resulting thumbnail in pixels

  • :format (String)

    The format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png

  • :delay (Integer)

    The number of milliseconds to wait before taking the screenshot

Returns:



114
115
116
117
118
119
120
121
# File 'lib/grabz_it/client.rb', line 114

def take_picture(options = {})
  parse_options(options)
  action = "takepicture.ashx"
  response_body = query_api(action, generate_params)
  response = Response.new(response_body)
  raise response.message if response.message
  response
end