Skip to content

Commit

Permalink
Use factory to create initial token values
Browse files Browse the repository at this point in the history
  • Loading branch information
gburgett committed Mar 27, 2020
1 parent fcf7cc5 commit 6fab72c
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions wcc-contentful/lib/wcc/contentful/sync_engine.rb
Expand Up @@ -28,7 +28,7 @@ class SyncEngine
include ::Wisper::Publisher

def state
(@state&.dup || { 'sys' => { 'id' => @state_key, 'type' => 'token' } }).freeze
(@state&.dup || token_wrapper_factory(nil)).freeze
end

attr_reader :store
Expand All @@ -52,7 +52,7 @@ def initialize(state: nil, store: nil, client: nil, key: nil)
@state = read_state if should_sync?
end
if state
@state = _ensure_state_hash(state)
@state = token_wrapper_factory(state)
raise ArgumentError, ':state param must be a String or Hash' unless @state.is_a? Hash
unless @state.dig('sys', 'type') == 'token'
raise ArgumentError, ':state param must be of sys.type = "token"'
Expand All @@ -75,7 +75,7 @@ def next(up_to_id: nil)
count = 0

@mutex.synchronize do
@state ||= read_state || { 'sys' => { 'id' => @state_key, 'type' => 'token' } }
@state ||= read_state || token_wrapper_factory(nil)
next_sync_token = @state['token']

sync_resp = client.sync(sync_token: next_sync_token)
Expand Down Expand Up @@ -111,16 +111,15 @@ def read_state
return unless found = store&.find(@state_key)

# backwards compat - migrate existing state
_ensure_state_hash(found)
token_wrapper_factory(found)
end

def write_state
store.index(@state) if store&.index?
end

def _ensure_state_hash(state)
state = { 'token' => state } if state.is_a? String
return unless state.is_a? Hash
def token_wrapper_factory(state)
state = { 'token' => state } unless state.is_a? Hash

state.merge!('sys' => { 'id' => @state_key, 'type' => 'token' }) unless state['sys']
state
Expand Down

0 comments on commit 6fab72c

Please sign in to comment.