Class: Comunit::Network::Handler
- Inherits:
-
Object
- Object
- Comunit::Network::Handler
show all
- Includes:
- Logging, Sending
- Defined in:
- app/services/comunit/network/handler.rb
Overview
Base entity handler for synchronization
Constant Summary
collapse
- MAIN_HOST =
'https://comunit.online'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Sending
#headers, #rest
Methods included from Logging
#log, #log_error, #log_info, #log_warn
Constructor Details
#initialize(entity) ⇒ Handler
Returns a new instance of Handler.
15
16
17
|
# File 'app/services/comunit/network/handler.rb', line 15
def initialize(entity)
@entity = entity
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
12
13
14
|
# File 'app/services/comunit/network/handler.rb', line 12
def data
@data
end
|
#entity ⇒ Object
Returns the value of attribute entity.
12
13
14
|
# File 'app/services/comunit/network/handler.rb', line 12
def entity
@entity
end
|
Class Method Details
.[](data) ⇒ Object
20
21
22
23
24
|
# File 'app/services/comunit/network/handler.rb', line 20
def self.[](data)
instance = new(nil)
instance.data = data
instance
end
|
.ignored_attributes ⇒ Object
35
36
37
|
# File 'app/services/comunit/network/handler.rb', line 35
def self.ignored_attributes
%w[data id image]
end
|
.permitted_attributes ⇒ Object
39
40
41
|
# File 'app/services/comunit/network/handler.rb', line 39
def self.permitted_attributes
%i[created_at uuid]
end
|
.relationship_data(entity, wrap = true) ⇒ Object
28
29
30
31
32
33
|
# File 'app/services/comunit/network/handler.rb', line 28
def self.relationship_data(entity, wrap = true)
return nil if entity.nil?
data = { id: entity.uuid, type: entity.class.table_name }
wrap ? { data: data } : data
end
|
Instance Method Details
#amend(id) ⇒ Object
64
65
66
67
68
69
70
71
72
|
# File 'app/services/comunit/network/handler.rb', line 64
def amend(id)
@entity = entity_class.find_by(id: id)
if @entity.nil?
log_info "Could not find #{entity_class} #{id}"
else
log_info "Amending #{@entity.class} #{id}"
pull_and_validate
end
end
|
#entity_class ⇒ Object
94
95
96
|
# File 'app/services/comunit/network/handler.rb', line 94
def entity_class
self.class.to_s.demodulize.gsub(/Handler$/, '').safe_constantize
end
|
#prepare_model_data ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'app/services/comunit/network/handler.rb', line 84
def prepare_model_data
{
id: entity.uuid,
type: entity.class.table_name,
attributes: attributes_for_remote,
relationships: relationships_for_remote,
meta: meta_for_remote
}
end
|
#pull(uuid) ⇒ Object
52
53
54
55
56
57
58
59
60
61
|
# File 'app/services/comunit/network/handler.rb', line 52
def pull(uuid)
@entity = entity_class.find_or_initialize_by(uuid: uuid)
log_info "Pulling #{@entity.class} #{uuid}"
if data.dig(:comunit, :site_id) == ENV['SITE_ID']
log_info 'Entity has the same origin site, skipping'
else
pull_and_validate
end
end
|
#pull_and_validate ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'app/services/comunit/network/handler.rb', line 74
def pull_and_validate
pull_data
log_info "Validation status after pull: #{@entity.valid?}"
if @entity.valid?
@entity.save && after_pull
else
log_error @entity.errors.messages
end
end
|
#push ⇒ Object
43
44
45
46
47
48
49
|
# File 'app/services/comunit/network/handler.rb', line 43
def push
return if entity.nil?
log_info("Pushing #{entity.class} #{entity.uuid}")
path = "#{MAIN_HOST}/comunit/#{entity.class.table_name}/#{entity.uuid}"
rest(:put, path, data: prepare_model_data)
end
|