Class: PublishingPlatformApi::PublishingApi

Inherits:
Base
  • Object
show all
Defined in:
lib/publishing_platform_api/publishing_api.rb

Overview

Adapter for the Publishing API.

Defined Under Namespace

Classes: NoLiveVersion

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 PublishingPlatformApi::Base

Instance Method Details

#discard_draft(content_id, options = {}) ⇒ Object

Discard a draft

Deletes the draft content item.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • previous_version (Integer)

    used to ensure the request is discarding the latest lock version of the draft



102
103
104
105
106
107
108
# File 'lib/publishing_platform_api/publishing_api.rb', line 102

def discard_draft(content_id, options = {})
  optional_keys = %i[previous_version]

  params = merge_optional_keys({}, options, optional_keys)

  post_json(discard_url(content_id), params)
end

#get_content(content_id, params = {}) ⇒ PublishingPlatformApi::Response

Return a content item

Raises exception if the item doesn’t exist.

Parameters:

  • content_id (UUID)
  • params (Hash) (defaults to: {})

Returns:

Raises:



28
29
30
# File 'lib/publishing_platform_api/publishing_api.rb', line 28

def get_content(content_id, params = {})
  get_json(content_url(content_id, params))
end

#get_content_items(params) ⇒ Object

Get a list of content items from the Publishing API.

The only required key in the params hash is ‘document_type`. These will be used to filter down the content items being returned by the API. Other allowed options can be seen from the link below.

Examples:


publishing_api.get_content_items(
  document_type: 'taxon',
  q: 'Driving',
  page: 1,
  per_page: 50,
  publishing_app: 'content-tagger',
  fields: ['title', 'description', 'public_updated_at'],
  order: '-public_updated_at'
)

Parameters:

  • params (Hash)

    At minimum, this hash has to include the ‘document_type` of the content items we wish to see. All other optional keys are documented above.



155
156
157
158
# File 'lib/publishing_platform_api/publishing_api.rb', line 155

def get_content_items(params)
  query = query_string(params)
  get_json("#{endpoint}/content#{query}")
end

Patch the links of a content item

Examples:


publishing_api.patch_links(
  '86963c13-1f57-4005-b119-e7cf3cb92ecf',
  links: {
    topics: ['d6e1527d-d0c0-40d5-9603-b9f3e6866b8a'],
    mainstream_browse_pages: ['d6e1527d-d0c0-40d5-9603-b9f3e6866b8a'],
  },
  previous_version: 10,
  bulk_publishing: true
)

Parameters:

  • content_id (UUID)
  • params (Hash)

Options Hash (params):

  • links (Hash)

    A “links hash”

  • previous_version (Integer)

    The previous version (returned by ‘get_links`). If this version is not the current version, the publishing-api will reject the change and return 409 Conflict. (optional)



128
129
130
131
132
133
134
135
136
# File 'lib/publishing_platform_api/publishing_api.rb', line 128

def patch_links(content_id, params)
  payload = {
    links: params.fetch(:links),
  }

  payload = merge_optional_keys(payload, params, %i[previous_version bulk_publishing])

  patch_json(links_url(content_id), payload)
end

#publish(content_id, update_type = nil, options = {}) ⇒ Object

Publish a content item

The publishing-api will “publish” a draft item, so that it will be visible on the public site.

Parameters:

  • content_id (UUID)
  • update_type (String) (defaults to: nil)

    Either ‘major’, ‘minor’ or ‘republish’

  • options (Hash) (defaults to: {})


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/publishing_platform_api/publishing_api.rb', line 40

def publish(content_id, update_type = nil, options = {})
  params = {
    update_type:,
  }

  optional_keys = %i[previous_version]

  params = merge_optional_keys(params, options, optional_keys)

  post_json(publish_url(content_id), params)
end

#put_content(content_id, payload) ⇒ Object

Put a content item

Parameters:

  • content_id (UUID)
  • payload (Hash)

    A valid content item



14
15
16
# File 'lib/publishing_platform_api/publishing_api.rb', line 14

def put_content(content_id, payload)
  put_json(content_url(content_id), payload)
end

#put_path(base_path, payload) ⇒ Object

Reserves a path for a publishing application

Returns success or failure only.

Parameters:

  • payload (Hash)

Options Hash (payload):

  • publishing_app (Hash)

    The publishing application, like ‘content-tagger`



166
167
168
169
# File 'lib/publishing_platform_api/publishing_api.rb', line 166

def put_path(base_path, payload)
  url = "#{endpoint}/paths#{base_path}"
  put_json(url, payload)
end

#republish(content_id, options = {}) ⇒ Object

Republish a content item

The publishing-api will “republish” a live edition. This can be used to remove an unpublishing or to re-send a published edition downstream

Parameters:

  • content_id (UUID)
  • options (Hash) (defaults to: {})


59
60
61
62
63
64
65
# File 'lib/publishing_platform_api/publishing_api.rb', line 59

def republish(content_id, options = {})
  optional_keys = %i[previous_version]

  params = merge_optional_keys({}, options, optional_keys)

  post_json(republish_url(content_id), params)
end

#unpublish(content_id, type:, explanation: nil, alternative_path: nil, discard_drafts: false, allow_draft: false, previous_version: nil, unpublished_at: nil, redirects: nil) ⇒ Object

Unpublish a content item

The publishing API will “unpublish” a live item, to remove it from the public site, or update an existing unpublishing.

Parameters:

  • content_id (UUID)
  • type (String)

    Either ‘withdrawal’, ‘gone’ or ‘redirect’.

  • explanation (String) (defaults to: nil)

    (optional) Text to show on the page.

  • alternative_path (String) (defaults to: nil)

    (optional) Alternative path to show on the page or redirect to.

  • discard_drafts (Boolean) (defaults to: false)

    (optional) Whether to discard drafts on that item. Defaults to false.

  • previous_version (Integer) (defaults to: nil)

    (optional) A lock version number for optimistic locking.

  • unpublished_at (Time) (defaults to: nil)

    (optional) The time the content was withdrawn. Ignored for types other than withdrawn

  • redirects (Array) (defaults to: nil)

    (optional) Required if no alternative_path is given. An array of redirect values, ie: { path:, type:, destination: }



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/publishing_platform_api/publishing_api.rb', line 80

def unpublish(content_id, type:, explanation: nil, alternative_path: nil, discard_drafts: false, allow_draft: false, previous_version: nil, unpublished_at: nil, redirects: nil)
  params = {
    type:,
  }

  params[:explanation] = explanation if explanation
  params[:alternative_path] = alternative_path if alternative_path
  params[:previous_version] = previous_version if previous_version
  params[:discard_drafts] = discard_drafts if discard_drafts
  params[:allow_draft] = allow_draft if allow_draft
  params[:unpublished_at] = unpublished_at.utc.iso8601 if unpublished_at
  params[:redirects] = redirects if redirects

  post_json(unpublish_url(content_id), params)
end

#unreserve_path(base_path, publishing_app) ⇒ Object



171
172
173
174
# File 'lib/publishing_platform_api/publishing_api.rb', line 171

def unreserve_path(base_path, publishing_app)
  payload = { publishing_app: }
  delete_json(unreserve_url(base_path), payload)
end