Class: ProxyCrawl::ScreenshotsAPI

Inherits:
API
  • Object
show all
Defined in:
lib/proxycrawl/screenshots_api.rb

Constant Summary collapse

INVALID_SAVE_TO_PATH_FILENAME =
'Filename must end with .jpg or .jpeg'
SAVE_TO_PATH_FILENAME_PATTERN =
/.+\.(jpg|JPG|jpeg|JPEG)$/.freeze

Constants inherited from API

API::INVALID_TOKEN, API::INVALID_URL

Instance Attribute Summary collapse

Attributes inherited from API

#body, #original_status, #pc_status, #status_code, #storage_url, #timeout, #token, #url

Instance Method Summary collapse

Methods inherited from API

#initialize

Constructor Details

This class inherits a constructor from ProxyCrawl::API

Instance Attribute Details

#remaining_requestsObject (readonly)

Returns the value of attribute remaining_requests.



8
9
10
# File 'lib/proxycrawl/screenshots_api.rb', line 8

def remaining_requests
  @remaining_requests
end

#screenshot_pathObject (readonly)

Returns the value of attribute screenshot_path.



8
9
10
# File 'lib/proxycrawl/screenshots_api.rb', line 8

def screenshot_path
  @screenshot_path
end

#screenshot_urlObject (readonly)

Returns the value of attribute screenshot_url.



8
9
10
# File 'lib/proxycrawl/screenshots_api.rb', line 8

def screenshot_url
  @screenshot_url
end

#successObject (readonly)

Returns the value of attribute success.



8
9
10
# File 'lib/proxycrawl/screenshots_api.rb', line 8

def success
  @success
end

Instance Method Details

#get(url, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

#postObject



13
14
15
# File 'lib/proxycrawl/screenshots_api.rb', line 13

def post
  raise 'Only GET is allowed for the ScreenshotsAPI'
end