Class: GdsApi::PublishingApi
- Defined in:
- lib/gds_api/publishing_api.rb,
lib/gds_api/publishing_api/special_route_publisher.rb
Overview
Adapter for the Publishing API.
Defined Under Namespace
Classes: NoLiveVersion, SpecialRoutePublisher
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#destroy_intent(base_path) ⇒ Object
Delete a publishing intent for a base_path.
-
#discard_draft(content_id, options = {}) ⇒ Object
Discard a draft.
-
#get_content(content_id, params = {}) ⇒ GdsApi::Response
Return a content item.
- #get_content_by_embedded_document(content_id, params = {}) ⇒ Object
-
#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_events_for_content_id(content_id, params = {}) ⇒ GdsApi::Response
Get events for a specific content_id.
-
#get_expanded_links(content_id, locale: nil, with_drafts: true, generate: false) ⇒ Object
Get expanded links.
-
#get_host_content_for_content_id(content_id, params = {}) ⇒ GdsApi::Response
Get content items which embed a reusable content_id.
-
#get_host_content_item_for_content_id(content_id, host_content_id) ⇒ GdsApi::Response
Get a specific content item which embeds a reusable content_id.
-
#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_live_content(content_id, locale = "en") ⇒ GdsApi::Response
Return a live content item, i.e.
-
#get_paged_editions(params = {}) ⇒ Enumerator
Returns an Enumerator of Response objects for each page of results of editions for the provided query string parameters.
-
#get_schema(schema_name) ⇒ GdsApi::Response
Get a content schema by name.
-
#get_schemas ⇒ GdsApi::Response
Get all schemas.
-
#graphql_content_item(query) ⇒ GdsApi::Response
Make a GraphQL query and return the response in the same format as a Content Store content item.
-
#graphql_query(query) ⇒ GdsApi::Response
Make a GraphQL query.
-
#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_intent(base_path, payload) ⇒ Object
Create a publishing intent for a base_path.
-
#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.
- #unreserve_path(base_path, publishing_app) ⇒ Object
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
#destroy_intent(base_path) ⇒ Object
Delete a publishing intent for a base_path.
543 544 545 546 547 |
# File 'lib/gds_api/publishing_api.rb', line 543 def destroy_intent(base_path) delete_json(intent_url(base_path)) rescue GdsApi::HTTPNotFound => e e end |
#discard_draft(content_id, options = {}) ⇒ Object
Discard a draft
Deletes the draft content item.
195 196 197 198 199 200 201 |
# File 'lib/gds_api/publishing_api.rb', line 195 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.
35 36 37 |
# File 'lib/gds_api/publishing_api.rb', line 35 def get_content(content_id, params = {}) get_json(content_url(content_id, params)) end |
#get_content_by_embedded_document(content_id, params = {}) ⇒ Object
376 377 378 379 |
# File 'lib/gds_api/publishing_api.rb', line 376 def (content_id, params = {}) warn "GdsAPI::PublishingApi: #get_content_by_embedded_document deprecated (please use #get_host_content_for_content_id)" get_host_content_for_content_id(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.
335 336 337 338 |
# File 'lib/gds_api/publishing_api.rb', line 335 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.
407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/gds_api/publishing_api.rb', line 407 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.
449 450 451 |
# File 'lib/gds_api/publishing_api.rb', line 449 def get_editions(params = {}) get_json(get_editions_url(params)) end |
#get_events_for_content_id(content_id, params = {}) ⇒ GdsApi::Response
Get events for a specific content_id
394 395 396 397 |
# File 'lib/gds_api/publishing_api.rb', line 394 def get_events_for_content_id(content_id, params = {}) query = query_string(params) get_json("#{endpoint}/v2/content/#{content_id}/events#{query}") end |
#get_expanded_links(content_id, locale: nil, with_drafts: true, generate: false) ⇒ Object
Get expanded links
Return the expanded links of the item.
275 276 277 278 279 280 281 282 283 |
# File 'lib/gds_api/publishing_api.rb', line 275 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_host_content_for_content_id(content_id, params = {}) ⇒ GdsApi::Response
Get content items which embed a reusable content_id
The content items returned will be in either the draft of published state.
354 355 356 357 |
# File 'lib/gds_api/publishing_api.rb', line 354 def get_host_content_for_content_id(content_id, params = {}) query = query_string(params) get_json("#{endpoint}/v2/content/#{content_id}/host-content#{query}") end |
#get_host_content_item_for_content_id(content_id, host_content_id) ⇒ GdsApi::Response
Get a specific content item which embeds a reusable content_id
372 373 374 |
# File 'lib/gds_api/publishing_api.rb', line 372 def get_host_content_item_for_content_id(content_id, host_content_id) get_json("#{endpoint}/v2/content/#{content_id}/host-content/#{host_content_id}") end |
#get_linkables(document_type: nil) ⇒ Object
FIXME: Add documentation
424 425 426 427 428 429 430 |
# File 'lib/gds_api/publishing_api.rb', line 424 def get_linkables(document_type: nil) if document_type.nil? raise ArgumentError, "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
435 436 437 438 439 |
# File 'lib/gds_api/publishing_api.rb', line 435 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.
224 225 226 |
# File 'lib/gds_api/publishing_api.rb', line 224 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.
245 246 247 |
# File 'lib/gds_api/publishing_api.rb', line 245 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
496 497 498 |
# File 'lib/gds_api/publishing_api.rb', line 496 def get_links_for_content_ids(content_ids) post_json("#{endpoint}/v2/links/by-content-id", content_ids:).to_hash end |
#get_live_content(content_id, locale = "en") ⇒ GdsApi::Response
Return a live content item, i.e. content that has a state of “published” or “unpublished”
Raises exception if the item doesn’t exist.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gds_api/publishing_api.rb', line 50 def get_live_content(content_id, locale = "en") content_item = get_content(content_id, locale:) live_states = %w[published unpublished] return content_item if live_states.include?(content_item.to_h["publication_state"]) live_version_number = content_item["state_history"].find { |_, v| live_states.include?(v) }&.first raise NoLiveVersion, "No live version exists for content_id: #{content_id}" unless live_version_number get_content(content_id, locale:, version: live_version_number) 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.
461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/gds_api/publishing_api.rb', line 461 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 |
#get_schema(schema_name) ⇒ GdsApi::Response
Get a content schema by name
592 593 594 |
# File 'lib/gds_api/publishing_api.rb', line 592 def get_schema(schema_name) get_json("#{endpoint}/v2/schemas/#{schema_name}").to_hash end |
#get_schemas ⇒ GdsApi::Response
Get all schemas
567 568 569 |
# File 'lib/gds_api/publishing_api.rb', line 567 def get_schemas get_json("#{endpoint}/v2/schemas").to_hash end |
#graphql_content_item(query) ⇒ GdsApi::Response
Make a GraphQL query and return the response in the same format as a Content Store content item
610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/gds_api/publishing_api.rb', line 610 def graphql_content_item(query) create_response = proc do |r| updated_body = JSON.parse(r.body).dig("data", "edition") updated_response = RestClient::Response.create( updated_body.to_json, r.net_http_res, r.request, ) GdsApi::Response.new(updated_response) end post_json("#{endpoint}/graphql", query:, &create_response) end |
#graphql_query(query) ⇒ GdsApi::Response
Make a GraphQL query
601 602 603 |
# File 'lib/gds_api/publishing_api.rb', line 601 def graphql_query(query) post_json("#{endpoint}/graphql", query:) 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 GdsApi::PublishingApiV2#lookup_content_ids.
102 103 104 105 106 107 108 109 110 |
# File 'lib/gds_api/publishing_api.rb', line 102 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_unpublishing_types:, 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.
75 76 77 78 79 80 81 82 |
# File 'lib/gds_api/publishing_api.rb', line 75 def lookup_content_ids(base_paths:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false) = { 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
305 306 307 308 309 310 311 312 313 |
# File 'lib/gds_api/publishing_api.rb', line 305 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.
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/gds_api/publishing_api.rb', line 123 def publish(content_id, update_type = nil, = {}) params = { 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
19 20 21 |
# File 'lib/gds_api/publishing_api.rb', line 19 def put_content(content_id, payload) put_json(content_url(content_id), payload) end |
#put_intent(base_path, payload) ⇒ Object
Create a publishing intent for a base_path.
publishing_api.put_intent(
'/some/base_path',
{
publish_time: '2024-03-15T09:00:00.000+00:00',
publishing_app: 'content-publisher',
rendering_app: 'government-frontend',
}
)
534 535 536 |
# File 'lib/gds_api/publishing_api.rb', line 534 def put_intent(base_path, payload) put_json(intent_url(base_path), payload) end |
#put_path(base_path, payload) ⇒ Object
Reserves a path for a publishing application
Returns success or failure only.
508 509 510 511 |
# File 'lib/gds_api/publishing_api.rb', line 508 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
145 146 147 148 149 150 151 |
# File 'lib/gds_api/publishing_api.rb', line 145 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.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/gds_api/publishing_api.rb', line 169 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:, } 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 |
#unreserve_path(base_path, publishing_app) ⇒ Object
513 514 515 516 |
# File 'lib/gds_api/publishing_api.rb', line 513 def unreserve_path(base_path, publishing_app) payload = { publishing_app: } delete_json(unreserve_url(base_path), payload) end |