Class: Flexite::Diff::ApplyService

Inherits:
Object
  • Object
show all
Defined in:
app/services/flexite/diff/apply_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, stage, checksum) ⇒ ApplyService

Returns a new instance of ApplyService.



4
5
6
7
8
# File 'app/services/flexite/diff/apply_service.rb', line 4

def initialize(token, stage, checksum)
  @stage = stage
  @token = Token.new(token)
  @checksum = checksum
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/flexite/diff/apply_service.rb', line 10

def call
  if @token.invalid?
    return { error: 'Invalid token', code: 401 }
  end

  diffs = Flexite.cache.read("#{Flexite.state_digest}-#{@checksum}-#{@stage}-diffs")

  if diffs.blank?
    Flexite.cache.delete_matched(Flexite.match_key("-#{@stage}-hashdiffs"))
    return { error: 'Difference is inconsistent', code: 400 }
  end

  Config.transaction(requires_new: true) do
    Entry.transaction do
      diffs.each do |type, *changes|
        send("handle_#{type}", *changes)
      end
    end
  end

  Flexite.reload_root_cache
  { message: 'Difference was applied', code: 200 }
rescue => exc
  { error: exc.message, code: 500 }
end