Sha256: f0f2ab9b9699f36e391ce3765030f2eef0042b050a0de33eaaf35601ac3ae695

Contents?: true

Size: 910 Bytes

Versions: 1

Compression:

Stored size: 910 Bytes

Contents

module Trice
  module ControllerMethods
    class ReferenceTimeAssignment

      QUERY_STUB_KEY  = '_requested_at'.freeze
      HEADER_STUB_KEY = 'X-REQUESTED-AT'.freeze

      def initialize(config)
        @stub_configuration = config
      end

      def around(controller, &action)
        t = determine_requested_at(controller)

        Trice.with_reference_time(t, &action)
      end

      private

      def determine_requested_at(controller)
        if @stub_configuration.stubbable?(controller)
          extract_requested_at(controller.request) || Time.now
        else
          Time.now
        end
      end

      def extract_requested_at(request)
        if request.params[QUERY_STUB_KEY]
          Time.zone.parse(request.params[QUERY_STUB_KEY])
        elsif request.headers[HEADER_STUB_KEY]
          Time.zone.parse(request.headers[HEADER_STUB_KEY])
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trice-0.2.0 lib/trice/controller_methods/reference_time_assignment.rb