Module: Spn2

Extended by:
Curlable
Defined in:
lib/spn2.rb,
lib/spn2_errors.rb,
lib/spn2/version.rb

Overview

namespace

Defined Under Namespace

Classes: Spn2Error, Spn2ErrorBadAuth, Spn2ErrorBadParams, Spn2ErrorFailedCapture, Spn2ErrorInvalidOption, Spn2ErrorMissingKeys, Spn2ErrorNoOutlinks, Spn2ErrorTooManyRequests, Spn2ErrorUnknownResponse, Spn2ErrorUnknownResponseCode

Constant Summary collapse

ESSENTIAL_STATUS_KEYS =
%w[job_id resources status].freeze
JOB_ID_REGEXP =
/^(spn2-([a-f]|\d){40})$/
WEB_ARCHIVE =
'https://web.archive.org'
BINARY_OPTS =
%w[capture_all capture_outlinks capture_screenshot delay_wb_availabilty force_get skip_first_archive
outlinks_availability email_result].freeze
OTHER_OPTS =
%w[if_not_archived_within js_behavior_timeout capture_cookie target_username target_password].freeze
BAD_AUTH_MSG =
'You need to be logged in to use Save Page Now.'
ERROR_CODES =
[400, 502].freeze
TOO_MANY_REQUESTS =
'Too Many Requests'
VERSION =
'0.2.0'

Class Method Summary collapse

Methods included from Curlable

get, post

Class Method Details

.access_keyObject



27
28
29
# File 'lib/spn2.rb', line 27

def access_key
  ENV.fetch('SPN2_ACCESS_KEY')
end

.error_classesObject



23
24
25
# File 'lib/spn2.rb', line 23

def error_classes
  Spn2.constants.map { |e| Spn2.const_get(e) }.select { |e| e.is_a?(Class) && e < Exception }
end

.save(url:, opts: {}) ⇒ Object Also known as: capture



43
44
45
46
47
48
49
50
51
52
# File 'lib/spn2.rb', line 43

def save(url:, opts: {})
  raise Spn2ErrorInvalidOption, "One or more invalid options: #{opts}" unless options_valid?(opts)

  json = json(auth_post(url: "#{WEB_ARCHIVE}/save/#{url}", params: { url: url }.merge(opts)))
  raise Spn2ErrorBadAuth, json.inspect if json['message']&.== BAD_AUTH_MSG

  raise Spn2ErrorFailedCapture, json.inspect unless json['job_id']

  json
end

.secret_keyObject



31
32
33
# File 'lib/spn2.rb', line 31

def secret_key
  ENV.fetch('SPN2_SECRET_KEY')
end

.status(job_ids:, outlinks: false) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/spn2.rb', line 55

def status(job_ids:, outlinks: false)
  params = status_params(job_ids: job_ids, outlinks: outlinks)
  json = json(auth_post(url: "#{WEB_ARCHIVE}/save/status", params: params))
  return json if json.is_a? Array # must be valid response

  handle_status_errors(job_ids: job_ids, json: json, outlinks: outlinks)
  json
end

.system_statusObject



35
36
37
# File 'lib/spn2.rb', line 35

def system_status
  json get(url: "#{WEB_ARCHIVE}/save/status/system") # no auth
end

.user_statusObject



39
40
41
# File 'lib/spn2.rb', line 39

def user_status
  json auth_get(url: "#{WEB_ARCHIVE}/save/status/user?t=#{DateTime.now.strftime('%Q').to_i}")
end