Class: ActiveRecord::Turntable::Sequencer::Api

Inherits:
ActiveRecord::Turntable::Sequencer show all
Defined in:
lib/active_record/turntable/sequencer/api.rb

Constant Summary collapse

API_ENDPOINT =
'/sequences/'
NEXT_VALUE_ENDPOINT =
'/new'

Instance Method Summary collapse

Methods inherited from ActiveRecord::Turntable::Sequencer

build, has_sequencer?, sequence_name, table_name

Constructor Details

#initialize(klass, options = {}) ⇒ Api

Returns a new instance of Api.



13
14
15
16
17
18
19
# File 'lib/active_record/turntable/sequencer/api.rb', line 13

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

Instance Method Details

#current_sequence_value(sequence_name) ⇒ Object



28
29
30
31
32
33
# File 'lib/active_record/turntable/sequencer/api.rb', line 28

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

#next_sequence_value(sequence_name) ⇒ Object



21
22
23
24
25
26
# File 'lib/active_record/turntable/sequencer/api.rb', line 21

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