namespace :content do
  desc "Updates suggested related links for content from a JSON file"
  task :update_related_links_from_json, [:json_path] => :environment do |_, args|
    puts 'Reading and parsing data...'

    url = URI.parse(args[:json_path]) rescue false
    file =
      if url.is_a?(URI::HTTP) || url.is_a?(URI::HTTPS)
        url.open(&:read)
      else
        File.read(args[:json_path])
      end

    json = JSON.parse(file)

    @failed_content_ids = []

    start_time = Time.now
    puts "Start updating content items, at #{start_time}"

    json.each_pair do |source_content_id, related_content_ids|
      update_content(source_content_id, related_content_ids)
    end

    end_time = Time.now
    elapsed_time = end_time - start_time

    puts "Total elapsed time: #{elapsed_time}s"
    puts "Failed content ids: #{@failed_content_ids}"
  end

  def update_content(source_content_id, related_content_ids)
    response = Commands::V2::PatchLinkSet.call(
      content_id: source_content_id,
      links: {
        suggested_ordered_related_items: related_content_ids
      },
      bulk_publishing: true
    )

    if response.code == 200
      puts "Updated related links for content #{source_content_id}"
    else
      @failed_content_ids << source_content_id
      STDERR.puts "Failed to update content id #{source_content_id} - response status #{response.code}"
    end
  end
end

Add Rake task to update related links
Add Rake task to update related links

This commit adds a new Rake task which reads in a JSON file and uses it to update each content item it finds to a set of associated related links.

For example:

```
{
  "a118443c-0a43-48c0-a4b6-80e3da714d9a": [
    "94a2c30c-a477-49dd-a7f4-0fa29e72f32b"
  ],
  "5c71dc4b-7631-11e4-a3cb-005056011aef": [
        "94a2c30c-a477-49dd-a7f4-0fa29e72f32b", "1602726f-05d2-44fd-b465-ace19524da56"
  ],
  "1602726f-05d2-44fd-b465-ace19524da56": []
}

```

Here, the three content items specified by their content id as the key are each mapped to the set of content which is deemed to be related.

Solo: da39a3ee5e6b4b0d3255bfef95601890afd80709@karlbaker02
