Sync All Screenings for a Child Profile

To sync all screenings for a child profile, first fetch the full child profile.

curl \
  -H "Accept: application/json" \
  -H "X-Auth-Token: 54f958af0a4e0f272df3a76d63c14f79" \
  https://localhost/api/child_profiles/94154

This response only includes the _links object for brevity.

{
    "_links": {
        "caregiver_profiles": {
            "href": "/api/child_profiles/94154/caregiver_profiles"
        },
        "primary_caregiver_profile": {
            "href": "/api/caregiver_profiles/77194"
        },
        "screenings": {
            "href": "/api/child_profiles/94154/screenings"
        }
    },
}

Then use the link provided for syncing screenings.

curl \
  -H "Accept: application/json" \
  -H "X-Auth-Token: 54f958af0a4e0f272df3a76d63c14f79" \
  https://localhost/api/child_profiles/94154/screenings
{
    "_links": {
        "find": {
            "href": "/api/screenings/{id}",
            "templated": true
        },
        "next": {
            "href": "/api/child_profiles/94154/screenings?page=2"
        },
        "self": {
            "href": "/api/child_profiles/94154/screenings"
        }
    },
    "screenings": [
        { "id": 92100 },
        { "id": 92101 },
        { "id": 92102 },
        { "id": 92103 },
        { "id": 92104 },
        { "id": 92105 },
        { "id": 92106 },
        { "id": 92107 },
        { "id": 92108 },
        { "id": 92109 }
    ],
    "total_entries": 11,
    "total_pages": 2
}

This is a paginated response and will only include screening IDs. To fetch a detailed screening, use the templated link. URI Templates are RFC 6570. The response will include a next link to help you paginate through screenings.

curl \
  -H "Accept: application/json" \
  -H "X-Auth-Token: 54f958af0a4e0f272df3a76d63c14f79" \
  https://localhost/api/screenings/92103

The response is too long to display here, and will be different for each type of ASQ questionnaire. See the “Screening Details” section of the normal documentation for examples.

Example Code

require 'json'
require 'faraday'
require 'addressable'

class ASQMiddleware < Faraday::Middleware
  def initialize(app, options)
    @app = app
    @token = options[:token]
  end

  def call(env)
    env[:request_headers]["Accept"] = "application/json"
    env[:request_headers]["X-Auth-Token"] = @token
    @app.call(env)
  end
end

class Pager
  include Enumerable

  attr_reader :connection, :path, :key

  def initialize(connection, path, key)
    @connection = connection
    @path = path
    @key = key
  end

  def each
    fetch(path) do |item, links|
      yield item, links
    end
  end

  private

  def fetch(path, &block)
    response = connection.get(path)
    json = JSON.parse(response.body)
    items = json.fetch(key, [])

    links = json.fetch("_links", {})

    items.each do |item|
      yield item, links
    end

    if links.key?("next")
      fetch(links["next"]["href"], &block)
    end
  end
end

connection = Faraday.new(:url => "http://localhost") do |conn|
  conn.use ASQMiddleware, :token => "54f958af0a4e0f272df3a76d63c14f79"
  conn.adapter Faraday.default_adapter
end

response = connection.get("/api/child_profiles/94154")
raise "Error" unless response.success?
child_profile = JSON.parse(response.body)

pager = Pager.new(connection, child_profile["_links"]["screenings"]["href"], "screenings")
screenings = pager.map do |screening, links|
  id = screening["id"]
  template = Addressable::Template.new(links["find"]["href"])
  path = template.expand(:id => id).to_s

  response = connection.get(path)
  raise "Error" unless response.success?
  JSON.parse(response.body)
end