Class: NetworkManager::CandidateHandler

Inherits:
NetworkManager show all
Defined in:
app/services/network_manager/candidate_handler.rb

Overview

Synchronizing candidates with central site

Constant Summary collapse

REMOTE_URL =
"#{MAIN_HOST}/network/candidates"

Constants inherited from NetworkManager

MAIN_HOST

Instance Attribute Summary

Attributes inherited from NetworkManager

#data, #entity

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NetworkManager

ignored_attributes, relationship_data

Class Method Details

.permitted_attributesObject



7
8
9
10
11
12
# File 'app/services/network_manager/candidate_handler.rb', line 7

def self.permitted_attributes
  %i[
    about approved birthday created_at details_url lead name patronymic
    program supports_impeachment surname sync_state
  ]
end

Instance Method Details

#create_localObject

Create local post from remote data



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/network_manager/candidate_handler.rb', line 33

def create_local
  uuid = @data.dig(:id)

  log_event "[I] Creating local candidate #{uuid}"

  @entity = Candidate.new(uuid: uuid)
  @entity.agent = Agent[@data.dig(:meta, :agent_name).to_s]

  apply_for_update

  assign_forces_from_data if @entity.save

  @entity
end

#create_remote(entity) ⇒ Object

Create candidate on central site

Parameters:



17
18
19
20
21
# File 'app/services/network_manager/candidate_handler.rb', line 17

def create_remote(entity)
  log_event("[I] Creating remote candidate #{entity.id} (#{entity.uuid})")
  @entity = entity
  rest(:post, REMOTE_URL, data_for_remote)
end

#update_localObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/network_manager/candidate_handler.rb', line 48

def update_local
  uuid = @data[:id]

  log_event "[I] Updating local candidate #{uuid}"

  @entity = Candidate.find_by(uuid: uuid)

  if @entity.nil?
    log_event "[E] Cannot find candidate #{uuid}"
    false
  else
    apply_for_update
    @entity.save
  end
end

#update_remote(entity) ⇒ Object

Update candidate on central site

Parameters:



26
27
28
29
30
# File 'app/services/network_manager/candidate_handler.rb', line 26

def update_remote(entity)
  log_event("[I] Updating remote candidate #{entity.id} (#{entity.uuid})")
  @entity = entity
  rest(:patch, "#{REMOTE_URL}/#{entity.uuid}", data_for_remote)
end