Class: GdsApi::PublishingApiV2
- Defined in:
- lib/gds_api/publishing_api_v2.rb
Overview
Adapter for the Publishing API.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#discard_draft(content_id, options = {}) ⇒ Object
Discard a draft.
-
#get_content(content_id, params = {}) ⇒ GdsApi::Response
Return a content item.
-
#get_content_items(params) ⇒ Object
Get a list of content items from the Publishing API.
-
#get_content_items_enum(params) ⇒ Enumerator
Returns an Enumerator of content items for the provided query string parameters.
-
#get_editions(params = {}) ⇒ GdsApi::Response
Returns a paginated list of editions for the provided query string parameters.
-
#get_expanded_links(content_id, locale: nil, with_drafts: true, generate: false) ⇒ Object
Get expanded links.
-
#get_linkables(document_type: nil) ⇒ Object
FIXME: Add documentation.
-
#get_linked_items(content_id, params = {}) ⇒ Object
FIXME: Add documentation.
-
#get_links(content_id) ⇒ GdsApi::Response
Get the link set for the given content_id.
-
#get_links_changes(params) ⇒ Object
Returns an array of changes to links.
-
#get_links_for_content_ids(content_ids) ⇒ Hash
Returns a mapping of content_ids => links hashes.
-
#get_paged_editions(params = {}) ⇒ Enumerator
Returns an Enumerator of Response objects for each page of results of editions for the provided query string parameters.
-
#import(content_id, locale, content_items) ⇒ Object
Import content into the publishing API.
-
#lookup_content_id(base_path:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false) ⇒ UUID
Find the content_id for a base_path.
-
#lookup_content_ids(base_paths:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false) ⇒ Hash
Find the content_ids for a list of base_paths.
-
#patch_links(content_id, params) ⇒ Object
Patch the links of a content item.
-
#publish(content_id, update_type = nil, options = {}) ⇒ Object
Publish a content item.
-
#put_content(content_id, payload) ⇒ Object
Put a content item.
-
#put_path(base_path, payload) ⇒ Object
Reserves a path for a publishing application.
-
#republish(content_id, options = {}) ⇒ Object
Republish a content item.
-
#unpublish(content_id, type:, explanation: nil, alternative_path: nil, discard_drafts: false, allow_draft: false, previous_version: nil, locale: nil, unpublished_at: nil, redirects: nil) ⇒ Object
Unpublish a content item.
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
#discard_draft(content_id, options = {}) ⇒ Object
Discard a draft
Deletes the draft content item.
186 187 188 189 190 191 192 |
# File 'lib/gds_api/publishing_api_v2.rb', line 186 def discard_draft(content_id, = {}) optional_keys = %i[locale previous_version] params = merge_optional_keys({}, , optional_keys) post_json(discard_url(content_id), params) end |
#get_content(content_id, params = {}) ⇒ GdsApi::Response
Return a content item
Raises exception if the item doesn’t exist.
32 33 34 |
# File 'lib/gds_api/publishing_api_v2.rb', line 32 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.
326 327 328 329 |
# File 'lib/gds_api/publishing_api_v2.rb', line 326 def get_content_items(params) query = query_string(params) get_json("#{endpoint}/v2/content#{query}") end |
#get_content_items_enum(params) ⇒ Enumerator
Returns an Enumerator of content items for the provided query string parameters.
339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/gds_api/publishing_api_v2.rb', line 339 def get_content_items_enum(params) Enumerator.new do |yielder| (1..Float::INFINITY).each do |index| merged_params = params.merge(page: index) page = get_content_items(merged_params).to_h results = page.fetch('results', []) results.each do |result| yielder << result end break if page.fetch('pages') <= index end end end |
#get_editions(params = {}) ⇒ GdsApi::Response
Returns a paginated list of editions for the provided query string parameters.
381 382 383 |
# File 'lib/gds_api/publishing_api_v2.rb', line 381 def get_editions(params = {}) get_json(get_editions_url(params)) end |
#get_expanded_links(content_id, locale: nil, with_drafts: true, generate: false) ⇒ Object
Get expanded links
Return the expanded links of the item.
266 267 268 269 270 271 272 273 274 |
# File 'lib/gds_api/publishing_api_v2.rb', line 266 def (content_id, locale: nil, with_drafts: true, generate: false) params = {} params[:with_drafts] = "false" unless with_drafts params[:generate] = "true" if generate params[:locale] = locale if locale query = query_string(params) validate_content_id(content_id) get_json("#{endpoint}/v2/expanded-links/#{content_id}#{query}") end |
#get_linkables(document_type: nil) ⇒ Object
FIXME: Add documentation
356 357 358 359 360 361 362 |
# File 'lib/gds_api/publishing_api_v2.rb', line 356 def get_linkables(document_type: nil) if document_type.nil? raise ArgumentError.new("Please provide a `document_type`") end get_json("#{endpoint}/v2/linkables?document_type=#{document_type}") end |
#get_linked_items(content_id, params = {}) ⇒ Object
FIXME: Add documentation
367 368 369 370 371 |
# File 'lib/gds_api/publishing_api_v2.rb', line 367 def get_linked_items(content_id, params = {}) query = query_string(params) validate_content_id(content_id) get_json("#{endpoint}/v2/linked/#{content_id}#{query}") end |
#get_links(content_id) ⇒ GdsApi::Response
Get the link set for the given content_id.
Given a Content ID, it fetchs the existing link set and their version.
215 216 217 |
# File 'lib/gds_api/publishing_api_v2.rb', line 215 def get_links(content_id) get_json(links_url(content_id)) end |
#get_links_changes(params) ⇒ Object
Returns an array of changes to links.
The link changes can be filtered by link_type, source content_id, target content_id and user. A maximum of 250 changes will be returned.
236 237 238 |
# File 'lib/gds_api/publishing_api_v2.rb', line 236 def get_links_changes(params) get_json(links_changes_url(params)) end |
#get_links_for_content_ids(content_ids) ⇒ Hash
Returns a mapping of content_ids => links hashes
428 429 430 |
# File 'lib/gds_api/publishing_api_v2.rb', line 428 def get_links_for_content_ids(content_ids) post_json("#{endpoint}/v2/links/by-content-id", content_ids: content_ids).to_hash end |
#get_paged_editions(params = {}) ⇒ Enumerator
Returns an Enumerator of Response objects for each page of results of editions for the provided query string parameters.
393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/gds_api/publishing_api_v2.rb', line 393 def get_paged_editions(params = {}) Enumerator.new do |yielder| next_link = get_editions_url(params) while next_link yielder.yield begin response = get_json(next_link) end next_link_info = response['links'].select { |link| link['rel'] == 'next' }.first next_link = next_link_info && next_link_info['href'] end end end |
#import(content_id, locale, content_items) ⇒ Object
Import content into the publishing API
The publishing-api will delete any content which has the content id provided, and then import the data given.
136 137 138 139 140 141 142 |
# File 'lib/gds_api/publishing_api_v2.rb', line 136 def import(content_id, locale, content_items) params = { history: content_items, } post_json("#{endpoint}/v2/content/#{content_id}/import?locale=#{locale}", params) end |
#lookup_content_id(base_path:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false) ⇒ UUID
Find the content_id for a base_path.
Convenience method if you only need to look up one content_id for a base_path. For multiple base_paths, use #lookup_content_ids.
76 77 78 79 80 81 82 83 84 |
# File 'lib/gds_api/publishing_api_v2.rb', line 76 def lookup_content_id(base_path:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false) lookups = lookup_content_ids( base_paths: [base_path], exclude_document_types: exclude_document_types, exclude_unpublishing_types: exclude_unpublishing_types, with_drafts: with_drafts, ) lookups[base_path] end |
#lookup_content_ids(base_paths:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false) ⇒ Hash
Find the content_ids for a list of base_paths.
49 50 51 52 53 54 55 56 |
# File 'lib/gds_api/publishing_api_v2.rb', line 49 def lookup_content_ids(base_paths:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false) = { base_paths: base_paths } [:exclude_document_types] = exclude_document_types if exclude_document_types [:exclude_unpublishing_types] = exclude_unpublishing_types if exclude_unpublishing_types [:with_drafts] = with_drafts if with_drafts response = post_json("#{endpoint}/lookup-by-base-path", ) response.to_hash end |
#patch_links(content_id, params) ⇒ Object
Patch the links of a content item
296 297 298 299 300 301 302 303 304 |
# File 'lib/gds_api/publishing_api_v2.rb', line 296 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.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/gds_api/publishing_api_v2.rb', line 97 def publish(content_id, update_type = nil, = {}) params = { update_type: update_type } optional_keys = %i[locale previous_version] params = merge_optional_keys(params, , optional_keys) post_json(publish_url(content_id), params) end |
#put_content(content_id, payload) ⇒ Object
Put a content item
16 17 18 |
# File 'lib/gds_api/publishing_api_v2.rb', line 16 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.
440 441 442 443 |
# File 'lib/gds_api/publishing_api_v2.rb', line 440 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
119 120 121 122 123 124 125 |
# File 'lib/gds_api/publishing_api_v2.rb', line 119 def republish(content_id, = {}) optional_keys = %i[locale previous_version] params = merge_optional_keys({}, , 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, locale: 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.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/gds_api/publishing_api_v2.rb', line 160 def unpublish(content_id, type:, explanation: nil, alternative_path: nil, discard_drafts: false, allow_draft: false, previous_version: nil, locale: nil, unpublished_at: nil, redirects: nil) params = { type: 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[:locale] = locale if locale params[:unpublished_at] = unpublished_at.utc.iso8601 if unpublished_at params[:redirects] = redirects if redirects post_json(unpublish_url(content_id), params) end |