Sync All Caregiver Profile Data

Fetching a list of caregiver profiles

Start out by fetching the list of child profiles. The response includes 10 profiles per page, and only the child profile IDs.

curl \
  -H "Accept: application/json" \
  -H "X-Auth-Token: 54f958af0a4e0f272df3a76d63c14f79" \
  https://localhost/api/caregiver_profiles
{
    "_links": {
        "find": {
            "href": "/api/caregiver_profiles/{id}",
            "templated": true
        },
        "next": {
            "href": "/api/caregiver_profiles?page=2"
        },
        "self": {
            "href": "/api/caregiver_profiles"
        }
    },
    "caregiver_profiles": [
        { "id": 44233 },
        { "id": 44234 },
        { "id": 44235 },
        { "id": 44236 },
        { "id": 44337 },
        { "id": 45008 },
        { "id": 45009 },
        { "id": 45010 },
        { "id": 45011 },
        { "id": 47118 }
    ],
    "total_entries": 38,
    "total_pages": 4
}

To fetch the second page and on, use the next link provided in the _links object.

curl \
  -H "Accept: application/json" \
  -H "X-Auth-Token: 54f958af0a4e0f272df3a76d63c14f79" \
  https://localhost/api/caregiver_profiles?page=2

Load a Detailed Caregiver Profile

Once you have a list of caregiver profiles ids, you can begin fetching the detailed profiles. The index response contains a templated response to fetch the profile. This is RFC 6570. You should use the alternate_id to match records in your local database.

curl \
  -H "Accept: application/json" \
  -H "X-Auth-Token: 54f958af0a4e0f272df3a76d63c14f79" \
  https://localhost/api/caregiver_profiles/45009
{
    "caregiver_profile": {
        "address1": "1 main st.",
        "address2": null,
        "address3": null,
        "alternate_phone": null,
        "city": "littleton",
        "country": "United States",
        "county": null,
        "custom_fields": {
            "Blood Type": null,
            "Eye Color": null
        },
        "email": null,
        "first_name": "john",
        "id": 45009,
        "last_name": "doe",
        "middle_initial": null,
        "phone": "555-555-5555",
        "primary_language": "English",
        "relationship_to_child": "Child care provider",
        "secondary_language": "English",
        "state": "Massachusetts",
        "title": "Mr.",
        "zip": "11720"
    }
}

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

def get_last_run
  # Get the previous timestamp or return nil if not available
end

def set_last_run
  # Save the current timestamp
end

timestamp = get_last_run

if timestamp
  path = "/api/caregiver_profiles?since=#{timestamp}"
else
  path = "/api/caregiver_profiles"
end

pager = Pager.new(connection, path, "caregiver_profiles")
pager.each do |caregiver_profile, links|
  template = Addressable::Template.new(links["find"]["href"])
  path = template.expand(:id => caregiver_profile["id"]).to_s

  response = connection.get(path)
  raise "Error" unless response.success?
  caregiver_profile = JSON.parse(response.body)
  # Save the profile
  p caregiver_profile["caregiver_profile"]["id"]
end

set_last_run