class ScreenshotLayer::Client

Public Class Methods

new(access_key, secret_keyword) click to toggle source
# File lib/screenshot_capture.rb, line 16
def initialize(access_key, secret_keyword)

  if access_key.nil?
    raise ScreenshotLayer::MissingArgumentException.new 'access_key'
  end

  if secret_keyword.nil?
    raise ScreenshotLayer::MissingArgumentException.new 'secret_keyword'
  end

  @access_key = access_key
  @secret_keyword = secret_keyword

end

Public Instance Methods

capture(url, options = {}) click to toggle source
# File lib/screenshot_capture.rb, line 32
def capture(url, options = {})

  if url.nil?
    raise ScreenshotLayer::MissingArgumentException.new 'url'
    return
  end

  # Create a shallow copy so we don't manipulate the original reference
  query = options.dup

  # Generate the SecretKey for the request
  md5 = Digest::MD5.new
  md5.update url + @secret_keyword
  secret_key = md5.hexdigest

  # Populate the Query
  query.access_key = @access_key
  query.secret_key = secret_key
  query.url = URI.escape(url)

  # We then create the Request
  req = CaptureRequest.new(query)

  #  We create a Hash of the request so we can send it via HTTP
  req_dto = req.to_dh

  begin

    # We make the actual request
    res = self.class.get('/capture', req_dto)

    # We ensure that we tap the response so we can use the results
    res.inspect

    # If we have an export option passed in, we save it to local file system
    if options.filename.nil?

      # We just return the parsed binary response
      return res.parsed_response

    else

      begin

        # Ensure the path exists before we write
        FileUtils.mkdir_p(File.dirname(options.filename))

        File.open(options.filename, 'a+') do |file|

          file.write(res.body)

          result = {
              success: true,
              info: ScreenshotLayer::CaptureResponse::INFO_MESSAGE_SUCCESS_FILENAME,
              file_name: options.filename
          }
          return result

        end

      end
    end


  rescue => e
    puts e.inspect
    return

  ensure
    # Clean Up

  end
end