Sha256: 733f94e148e7583b71ad77aeb38ea7bb2bca61fda8113d5973c3288e423f0305

Contents?: true

Size: 981 Bytes

Versions: 6

Compression:

Stored size: 981 Bytes

Contents

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

module ActiveRecord::Turntable
  class Sequencer
    class Api < Sequencer
      API_ENDPOINT = "/sequences/".freeze
      NEXT_VALUE_ENDPOINT = "/new".freeze

      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?
        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?
        current_id
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activerecord-turntable-3.1.0 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-3.0.1 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-3.0.0 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-3.0.0.alpha3 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-3.0.0.alpha2 lib/active_record/turntable/sequencer/api.rb
activerecord-turntable-3.0.0.alpha1 lib/active_record/turntable/sequencer/api.rb