class ProxyCrawl::ScreenshotsAPI

Constants

INVALID_SAVE_TO_PATH_FILENAME
SAVE_TO_PATH_FILENAME_PATTERN

Attributes

remaining_requests[R]
screenshot_path[R]
screenshot_url[R]
success[R]

Public Instance Methods

get(url, options = {}) { |file| ... } click to toggle source
Calls superclass method ProxyCrawl::API#get
# File lib/proxycrawl/screenshots_api.rb, line 17
def get(url, options = {})
  screenshot_path = options.delete(:save_to_path) || generate_file_path
  raise INVALID_SAVE_TO_PATH_FILENAME unless SAVE_TO_PATH_FILENAME_PATTERN =~ screenshot_path

  response = super(url, options)
  file = File.open(screenshot_path, 'w+')
  file.write(response.body&.force_encoding('UTF-8'))
  @screenshot_path = screenshot_path
  yield(file) if block_given?
  response
ensure
  file&.close
end
post() click to toggle source
# File lib/proxycrawl/screenshots_api.rb, line 13
def post
  raise 'Only GET is allowed for the ScreenshotsAPI'
end

Private Instance Methods

base_url() click to toggle source
# File lib/proxycrawl/screenshots_api.rb, line 40
def base_url
  'https://api.proxycrawl.com/screenshots'
end
generate_file_name() click to toggle source
# File lib/proxycrawl/screenshots_api.rb, line 44
def generate_file_name
  "#{SecureRandom.urlsafe_base64}.jpg"
end
generate_file_path() click to toggle source
# File lib/proxycrawl/screenshots_api.rb, line 48
def generate_file_path
  File.join(Dir.tmpdir, generate_file_name)
end
prepare_response(response, format) click to toggle source
Calls superclass method ProxyCrawl::API#prepare_response
# File lib/proxycrawl/screenshots_api.rb, line 33
def prepare_response(response, format)
  super(response, format)
  @remaining_requests = response['remaining_requests'].to_i
  @success = response['success'] == 'true'
  @screenshot_url = response['screenshot_url']
end