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
21
22
23
24
25
# File 'lib/grabz_it/client.rb', line 16

def initialize(app_key, app_secret)
  @app_key = app_key
  @app_secret = app_secret

  if GrabzIt.logger
    GrabzIt.logger.info "Initialized client with [#{app_key}] and [#{app_secret}]"
  end

  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:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/grabz_it/client.rb', line 174

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
  }
  res = query_api(action, params)
  if GrabzIt.logger
    GrabzIt.logger.info "[Client][get_cookie_jar] Request: #{params}"
    GrabzIt.logger.info "[Client][get_cookie_jar] Response: #{res.body}"
  end
  cookie_jar = CookieJar.new(res.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:



222
223
224
225
226
227
228
229
230
231
# File 'lib/grabz_it/client.rb', line 222

def get_picture(screenshot_id)
  action = "getpicture.ashx"
  res = query_api(action, { :id => screenshot_id })
  image = Image.new(res)
  if GrabzIt.logger
    GrabzIt.logger.info "[Client][get_picture] Request: #{{ :id => screenshot_id }}"
    GrabzIt.logger.info "[Client][get_picture] Response: #{image.to_s}"
  end
  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:



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/grabz_it/client.rb', line 151

def get_status(screenshot_id)
  action = "getstatus.ashx"
  response_body = query_api(action, { :id => screenshot_id })
  if GrabzIt.logger
    GrabzIt.logger.info "[Client][get_status] Request: #{{ :id => screenshot_id }}"
    GrabzIt.logger.info "[Client][get_status] Response: #{response_body}"
  end
  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:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/grabz_it/client.rb', line 60

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)

      if GrabzIt.logger
        GrabzIt.logger.info "[Client][save_picture] Image was created with screenshot_id of [#{image.screenshot_id}]"
      end

      break
    end

    # Check again in 1 second with a max of 5 minutes
    if iterations <= (5 * 60)
      sleep(1)
    else
      if GrabzIt.logger
        GrabzIt.logger.error "[Client][save_picture] Attempting to retrieve the screenshot timed out after 5 minutes"
      end
      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:



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/grabz_it/client.rb', line 127

def take_picture(options = {})
  parse_options(options)
  action = "takepicture.ashx"
  res = query_api(action, generate_params)
  if GrabzIt.logger
    GrabzIt.logger.info "[Client][take_picture] Request: #{generate_params}"
    GrabzIt.logger.info "[Client][take_picture] Response: #{res.body}"
  end
  response = Response.new(res.body)
  raise response.message if response.message
  response
end