Class: GdsApi::LinkCheckerApi

Inherits:
Base
  • Object
show all
Defined in:
lib/gds_api/link_checker_api.rb

Defined Under Namespace

Classes: BatchReport, LinkReport, MonitorReport

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#client, #create_client, #get_list, #initialize, #url_for_slug

Constructor Details

This class inherits a constructor from GdsApi::Base

Instance Method Details

#check(uri, synchronous: nil, checked_within: nil) ⇒ LinkReport

Checks whether a link is broken.

Makes a GET request to the link checker api to check a link.

Parameters:

  • uri (String)

    The URI to check.

  • synchronous (Boolean) (defaults to: nil)

    Whether the check should happen immediately. (optional)

  • checked_within (Fixnum) (defaults to: nil)

    The number of seconds the last check should be within before doing another check. (optional)

Returns:

  • (LinkReport)

    A SimpleDelegator of the GdsApi::Response which responds to:

    :uri              the URI of the link
    :status           the status of the link, one of: ok, pending, broken, caution
    :checked          the date the link was checked
    :errors           a list of error descriptions
    :warnings         a list of warning descriptions
    :problem_summary  a short description of the most critical problem with the link
    :suggested_fix    where possible, this provides a potential fix for the problem
    

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gds_api/link_checker_api.rb', line 23

def check(uri, synchronous: nil, checked_within: nil)
  params = {
    uri: uri,
    synchronous: synchronous,
    checked_within: checked_within,
  }

  response = get_json(
    "#{endpoint}/check" + query_string(params.delete_if { |_, v| v.nil? }),
  )

  LinkReport.new(response.to_hash)
end

#create_batch(uris, checked_within: nil, webhook_uri: nil, webhook_secret_token: nil) ⇒ BatchReport

Create a batch of links to check.

Makes a POST request to the link checker api to create a batch.

Parameters:

  • uris (Array)

    A list of URIs to check.

  • checked_within (Fixnum) (defaults to: nil)

    The number of seconds the last check should be within before doing another check. (optional)

  • webhook_uri (String) (defaults to: nil)

    The URI to be called when the batch finishes. (optional)

  • webhook_secret_token (String) (defaults to: nil)

    A secret token that the API will use to generate a signature of the request. (optional)

Returns:

  • (BatchReport)

    A SimpleDelegator of the GdsApi::Response which responds to:

    :id            the ID of the batch
    :status        the status of the check, one of: complete or in_progress
    :links         an array of link reports
    :totals        an +OpenStruct+ of total information, fields: links, ok, caution, broken, pending
    :completed_at  a date when the batch was completed
    

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gds_api/link_checker_api.rb', line 55

def create_batch(uris, checked_within: nil, webhook_uri: nil, webhook_secret_token: nil)
  payload = {
    uris: uris,
    checked_within: checked_within,
    webhook_uri: webhook_uri,
    webhook_secret_token: webhook_secret_token,
  }

  response = post_json(
    "#{endpoint}/batch", payload.delete_if { |_, v| v.nil? }
  )

  BatchReport.new(response.to_hash)
end

#get_batch(id) ⇒ BatchReport

Get information about a batch.

Makes a GET request to the link checker api to get a batch.

Parameters:

  • id (Fixnum)

    The batch ID to get information about.

Returns:

  • (BatchReport)

    A SimpleDelegator of the GdsApi::Response which responds to:

    :id            the ID of the batch
    :status        the status of the check, one of: completed or in_progress
    :links         an array of link reports
    :totals        an +OpenStruct+ of total information, fields: links, ok, caution, broken, pending
    :completed_at  a date when the batch was completed
    

Raises:



84
85
86
87
88
89
90
# File 'lib/gds_api/link_checker_api.rb', line 84

def get_batch(id)
  BatchReport.new(
    get_json(
      "#{endpoint}/batch/#{id}",
    ).to_hash,
  )
end

#upsert_resource_monitor(links, app, reference) ⇒ MonitorReport

Update or create a set of links to be monitored for a resource.

Makes a POST request to the link checker api to create a resource monitor.

Parameters:

  • links (Array)

    A list of URIs to monitor.

  • reference (String)

    A unique id for the resource being monitored

  • app (String)

    The name of the service the call originated e.g. ‘whitehall’

Returns:

  • (MonitorReport)

    A SimpleDelegator of the GdsApi::Response which responds to:

    :id            the ID of the created resource monitor
    

Raises:



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gds_api/link_checker_api.rb', line 105

def upsert_resource_monitor(links, app, reference)
  payload = {
    links: links,
    app: app,
    reference: reference,
  }

  response = post_json("#{endpoint}/monitor", payload)

  MonitorReport.new(response.to_hash)
end