Class: Coelacanth::Client::ScreenshotOne

Inherits:
Base
  • Object
show all
Defined in:
lib/coelacanth/client/screenshot_one.rb

Overview

Coelacanth::Client

Instance Method Summary collapse

Methods inherited from Base

#client, #initialize

Constructor Details

This class inherits a constructor from Coelacanth::Client::Base

Instance Method Details

#get_responseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coelacanth/client/screenshot_one.rb', line 10

def get_response
  uri = URI.parse(@url)
  response = Coelacanth::HTTP.get_response(
    uri,
    open_timeout: Coelacanth::HTTP::DEFAULT_OPEN_TIMEOUT,
    read_timeout: Coelacanth::HTTP::DEFAULT_READ_TIMEOUT
  )
  @origin_response = response
  @status_code = response.code.to_i

  return response.body if response.is_a?(Net::HTTPSuccess)

  Coelacanth::HTTP.raise_http_error(uri, response)
rescue Coelacanth::TimeoutError
  fallback_response = fallback_client.get_response
  @status_code = fallback_client.instance_variable_get(:@status_code)
  fallback_response
end

#get_screenshotObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/coelacanth/client/screenshot_one.rb', line 29

def get_screenshot
  api_key = @config.read("screenshot_one.key")
  uri = URI("https://api.screenshotone.com/take")
  params = {
    access_key: api_key,
    url: @url,
    format: "jpg",
    block_ads: true,
    block_cookie_banners: true,
    block_banners_by_heuristics: false,
    block_trackers: true,
    delay: 0,
    timeout: 60,
    response_type: "by_format",
    image_quality: 80
  }
  uri.query = URI.encode_www_form(params)

  response = Coelacanth::HTTP.get_response(
    uri,
    open_timeout: Coelacanth::HTTP::DEFAULT_OPEN_TIMEOUT,
    read_timeout: 30
  )
  raise "Failed to fetch screenshot: #{response.code}" unless response.is_a?(Net::HTTPSuccess)

  response.body
rescue Coelacanth::TimeoutError
  fallback_client.get_screenshot
end