Class: ArchiveToday::Archiver

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_today/archiver.rb

Constant Summary collapse

BASE_URL =
'https://archive.today/'.freeze
DEFAULT_USER_AGENT =
"archive_today #{ArchiveToday::VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, user_agent: DEFAULT_USER_AGENT, debug: false) ⇒ Archiver

Returns a new instance of Archiver.



11
12
13
14
15
# File 'lib/archive_today/archiver.rb', line 11

def initialize(url:, user_agent: DEFAULT_USER_AGENT, debug: false)
  @debug = debug
  @target_url = url
  @user_agent = user_agent
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



9
10
11
# File 'lib/archive_today/archiver.rb', line 9

def debug
  @debug
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/archive_today/archiver.rb', line 8

def response
  @response
end

#target_urlObject (readonly)

Returns the value of attribute target_url.



9
10
11
# File 'lib/archive_today/archiver.rb', line 9

def target_url
  @target_url
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



9
10
11
# File 'lib/archive_today/archiver.rb', line 9

def user_agent
  @user_agent
end

Instance Method Details

#captureObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/archive_today/archiver.rb', line 17

def capture
  puts 'Submitting URL ...' if debug
  response = connection.post('/submit/') do |req|
    req.body = submission_body
  end
  raise unless response.success?

  self.response = response

  {
    url: finalized_url,
    screenshot_url: screenshot_url
  }
rescue Faraday::ClientError, Faraday::ServerError => e
  puts "[archive_today] Error capturing URL: #{e.response[:status]} #{e.response[:body]}"

  nil
end