Sha256: 7f558714d77306a5b8f2065c1bb76575ffd8555e872e741c3a32d7d6589669a8

Contents?: true

Size: 889 Bytes

Versions: 5

Compression:

Stored size: 889 Bytes

Contents

# frozen_string_literal: true

module Metatron
  # Used for "normal" sync requests
  class SyncController < Controller
    options "/" do
      headers "Access-Control-Allow-Methods" => ["POST"]
      halt 200
    end

    post "/" do
      # TODO: move this to Sinatra's `etag` helper when Metacontroller is RFC compliant
      if (provided_etag = calculate_etag) &&
         (match_header = request.env["HTTP_IF_NONE_MATCH"]) &&
         match_header.split(/,\s?/).include?("\"#{provided_etag}\"")
        halt 304
      end

      # If the etag is available, use it, otherwise proceed with the sync
      headers "ETag" => "\"#{provided_etag}\"" if provided_etag
      data = sync
      data[:children] = data[:children]&.map { |c| c.respond_to?(:render) ? c.render : c }
      halt(data.to_json)
    end

    def calculate_etag = nil
    def sync = raise NotImplementedError
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
metatron-0.4.0 lib/metatron/sync_controller.rb
metatron-0.3.4 lib/metatron/sync_controller.rb
metatron-0.3.3 lib/metatron/sync_controller.rb
metatron-0.3.2 lib/metatron/sync_controller.rb
metatron-0.3.1 lib/metatron/sync_controller.rb