-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathsynchronize_source_service.rb
61 lines (50 loc) · 1.7 KB
/
synchronize_source_service.rb
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class SynchronizeSourceService
def initialize(source, auto_accept = false)
@source = source
@auto_accept = auto_accept
end
def execute
synchronize_localizations
update_timestamps
end
private
def synchronize_localizations
after_date = @source.last_updated_at&.to_fs(:db)
result = interactor.send_request Lit::Source::LOCALIZATIONS_PATH, after: after_date
return unless result&.is_a?(Array)
result.each { |loc| synchronize_localization loc }
end
def synchronize_localization(loc)
inc_loc = find_incomming_localization(loc)
inc_loc.source = @source
inc_loc.locale_str = loc['locale_str']
inc_loc.locale = Lit::Locale.find_by(locale: loc['locale_str'])
inc_loc.localization_key_str = loc['localization_key_str']
inc_loc.localization_key_is_deleted = localization_key_deleted?(loc)
inc_loc.localization_key = find_localization_key(inc_loc)
inc_loc.translated_value = loc['value']
return if inc_loc.duplicated?(loc['value'])
inc_loc.save!
inc_loc.accept if @auto_accept
end
def find_incomming_localization(localization)
Lit::IncommingLocalization.find_or_initialize_by(incomming_id: localization['id'])
end
def find_localization_key(inc_loc)
Lit::LocalizationKey.find_by(localization_key: inc_loc.localization_key_str)
end
def localization_key_deleted?(loc)
loc['localization_key_is_deleted'] || false
end
def update_timestamps
@source.assign_last_updated_at(fetch_last_change)
@source.sync_complete = true
@source.save!
end
def fetch_last_change
interactor.send_request(Lit::Source::LAST_CHANGE_PATH)['last_change']
end
def interactor
RemoteInteractorService.new(@source)
end
end