Sha256: 5f06d7db2e835de63744978397d4be236a7f5c84f213fecc9ee9d82290d59b65

Contents?: true

Size: 983 Bytes

Versions: 23

Compression:

Stored size: 983 Bytes

Contents

# -*- coding: utf-8 -*-
#
# Sequencer via HTTP API
#
require 'httpclient'

module ActiveRecord::Turntable
  class Sequencer
    class Api < Sequencer
      API_ENDPOINT = '/sequences/'
      NEXT_VALUE_ENDPOINT = '/new'

      def initialize(klass, options = {})
        @klass = klass
        @options = options
        @host = @options["api_host"]
        @port = @options["api_port"]
        @client = HTTPClient.new
      end

      def next_sequence_value(sequence_name)
        res = @client.get_content("http://#{@host}:#{@port}#{API_ENDPOINT}#{sequence_name}#{NEXT_VALUE_ENDPOINT}")
        new_id = res.to_i
        raise SequenceNotFoundError if new_id.zero?
        return new_id
      end

      def current_sequence_value(sequence_name)
        res = @client.get_content("http://#{@host}:#{@port}#{API_ENDPOINT}#{sequence_name}")
        current_id = res.to_i
        raise SequenceNotFoundError if current_id.zero?
        return current_id
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
activerecord-turntable-2.5.0 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.4.0 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.3.3 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.3.2 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.3.1 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.3.0 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.2.2 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.2.1 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.2.0 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.1.1 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.1.0 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.1.0.rc2 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.1.0.rc1 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.1.0.beta2 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.1.0.beta1 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.0.6 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.0.5 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.0.4 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.0.3 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-2.0.2 lib/active_record/turntable/sequencer/api.rb