Class: GdsApi::PublishingApi

Inherits:
Base
  • Object
show all
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: SpecialRoutePublisher

Instance Method Summary collapse

Instance Method Details

#destroy_intent(base_path) ⇒ Object

Delete a publishing intent for a base_path.



536
537
538
539
540
# File 'lib/gds_api/publishing_api.rb', line 536

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.

Options Hash (options):

  • locale (String)

    The language, defaults to 'en' in publishing-api.

  • previous_version (Integer)

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

See Also:



185
186
187
188
189
190
191
# File 'lib/gds_api/publishing_api.rb', line 185

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

  params = merge_optional_keys({}, options, 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.

Options Hash (params):

  • locale (String)

    The language, defaults to 'en' in publishing-api.

Raises:

See Also:



33
34
35
# File 'lib/gds_api/publishing_api.rb', line 33

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

#get_content_by_embedded_document(content_id, params = {}) ⇒ Object



369
370
371
372
# File 'lib/gds_api/publishing_api.rb', line 369

def get_content_by_embedded_document(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.

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'],
  locale: 'en',
  order: '-public_updated_at'
)

See Also:



325
326
327
328
# File 'lib/gds_api/publishing_api.rb', line 325

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.



400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/gds_api/publishing_api.rb', line 400

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.



442
443
444
# File 'lib/gds_api/publishing_api.rb', line 442

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



387
388
389
390
# File 'lib/gds_api/publishing_api.rb', line 387

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

Return the expanded links of the item.

Examples:


publishing_api.get_expanded_links("8157589b-65e2-4df6-92ba-2c91d80006c0", with_drafts: false).to_h

#=> {
  "generated" => "2017-08-01T10:42:49Z",
  "expanded_links" => {
    "organisations" => [
      {
        "content_id" => "21aa83a2-a47f-4189-a252-b02f8c322012",
        ... (and more attributes)
      }
    ]
  }
}

See Also:



265
266
267
268
269
270
271
272
273
# File 'lib/gds_api/publishing_api.rb', line 265

def get_expanded_links(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.



344
345
346
347
# File 'lib/gds_api/publishing_api.rb', line 344

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, params = {}) ⇒ GdsApi::Response

Get a specific content item which embeds a reusable content_id



364
365
366
367
# File 'lib/gds_api/publishing_api.rb', line 364

def get_host_content_item_for_content_id(content_id, host_content_id, params = {})
  query = query_string(params)
  get_json("#{endpoint}/v2/content/#{content_id}/host-content/#{host_content_id}#{query}")
end

#get_linkables(document_type: nil) ⇒ Object

FIXME: Add documentation



417
418
419
420
421
422
423
# File 'lib/gds_api/publishing_api.rb', line 417

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



428
429
430
431
432
# File 'lib/gds_api/publishing_api.rb', line 428

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 the link set for the given content_id.

Given a Content ID, it fetchs the existing link set and their version.

Examples:


publishing_api.get_links("a-content-id")
# => {
  "content_id" => "a-content-id",
  "links" => [
    "organisation" => "organisation-content-id",
    "document_collection" => "document-collection-content-id"
  ],
  "version" => 17
}

See Also:



214
215
216
# File 'lib/gds_api/publishing_api.rb', line 214

def get_links(content_id)
  get_json(links_url(content_id))
end

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.

Examples:


publishing_api.get_links_changes(
  link_types: ['taxons'],
  target_content_ids: ['a544d48b-1e9e-47fb-b427-7a987c658c14']
)


235
236
237
# File 'lib/gds_api/publishing_api.rb', line 235

def get_links_changes(params)
  get_json(links_changes_url(params))
end

Returns a mapping of content_ids => links hashes

Examples:


publishing_api.get_links_for_content_ids([
  "e1067450-7d13-45ff-ada4-5e3dd4025fb7",
  "72ed754c-4c82-415f-914a-ab6760454cb4"
])

#=> {
  "e1067450-7d13-45ff-ada4-5e3dd4025fb7" => {
    links: {
      taxons: ["13bba81c-b2b1-4b13-a3de-b24748977198"]},
      ... (and more attributes)
    version: 10},
  "72ed754c-4c82-415f-914a-ab6760454cb4" => { ..etc }
}


489
490
491
# File 'lib/gds_api/publishing_api.rb', line 489

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.

Options Hash (locale):

  • locale (String)

    The language, defaults to 'en' in publishing-api.

Raises:

See Also:



48
49
50
# File 'lib/gds_api/publishing_api.rb', line 48

def get_live_content(content_id, locale = "en")
  get_content(content_id, locale:, content_store: :live)
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.



454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/gds_api/publishing_api.rb', line 454

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

Examples:


publishing_api.get_schema("email_address")
  # => {
    "email_address" => {
        "type": "email_address",
        "required": ["email"],
        "properties": {
          "email": { "type" => "string" },
        },
      }
  }

Raises:

See Also:



585
586
587
# File 'lib/gds_api/publishing_api.rb', line 585

def get_schema(schema_name)
  get_json("#{endpoint}/v2/schemas/#{schema_name}").to_hash
end

#get_schemasGdsApi::Response

Get all schemas

Examples:


publishing_api.get_schemas()
  # => {
    "email_address" => {
        "type": "email_address",
        "required": ["email"],
        "properties": {
          "email": { "type" => "string" },
        },
      }
  }

See Also:



560
561
562
# File 'lib/gds_api/publishing_api.rb', line 560

def get_schemas
  get_json("#{endpoint}/v2/schemas").to_hash
end

#graphql_live_content_item(base_path) ⇒ GdsApi::Response

Get the live content item using GraphQL



601
602
603
604
605
# File 'lib/gds_api/publishing_api.rb', line 601

def graphql_live_content_item(base_path)
  get_json("#{endpoint}/graphql/content#{base_path}")
rescue GdsApi::HTTPNotFound => e
  raise ItemNotFound.build_from(e)
end

#graphql_query(query) ⇒ GdsApi::Response

Make a GraphQL query



594
595
596
# File 'lib/gds_api/publishing_api.rb', line 594

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.

Examples:


publishing_api.lookup_content_id(base_path: '/foo')
# => "51ac4247-fd92-470a-a207-6b852a97f2db"

See Also:



92
93
94
95
96
97
98
99
100
# File 'lib/gds_api/publishing_api.rb', line 92

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.

Examples:


publishing_api.lookup_content_ids(base_paths: ['/foo', '/bar'])
# => { "/foo" => "51ac4247-fd92-470a-a207-6b852a97f2db", "/bar" => "261bd281-f16c-48d5-82d2-9544019ad9ca" }

See Also:



65
66
67
68
69
70
71
72
# File 'lib/gds_api/publishing_api.rb', line 65

def lookup_content_ids(base_paths:, exclude_document_types: nil, exclude_unpublishing_types: nil, with_drafts: false)
  options = { base_paths: }
  options[:exclude_document_types] = exclude_document_types if exclude_document_types
  options[:exclude_unpublishing_types] = exclude_unpublishing_types if exclude_unpublishing_types
  options[:with_drafts] = with_drafts if with_drafts
  response = post_json("#{endpoint}/lookup-by-base-path", options)
  response.to_hash
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
)

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)

  • bulk_publishing (Boolean)

    Set to true to indicate that this is part of a mass-republish. Allows the publishing-api to prioritise human-initiated publishing (optional, default false)

See Also:



295
296
297
298
299
300
301
302
303
# File 'lib/gds_api/publishing_api.rb', line 295

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.

Options Hash (options):

  • locale (String)

    The language, defaults to 'en' in publishing-api.

See Also:



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gds_api/publishing_api.rb', line 113

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

  optional_keys = i[locale 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



17
18
19
# File 'lib/gds_api/publishing_api.rb', line 17

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: 'frontend', } )



527
528
529
# File 'lib/gds_api/publishing_api.rb', line 527

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.

Options Hash (payload):

  • publishing_app (Hash)

    The publishing application, like content-tagger

See Also:



501
502
503
504
# File 'lib/gds_api/publishing_api.rb', line 501

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

Options Hash (options):

  • locale (String)

    The language, defaults to 'en' in publishing-api.

See Also:

  • ...


135
136
137
138
139
140
141
# File 'lib/gds_api/publishing_api.rb', line 135

def republish(content_id, options = {})
  optional_keys = i[locale 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, 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.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gds_api/publishing_api.rb', line 159

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



506
507
508
509
# File 'lib/gds_api/publishing_api.rb', line 506

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