-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.rb
107 lines (87 loc) · 3.14 KB
/
base.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true
require 'ruby-lokalise-api'
require 'pathname'
module LokaliseManager
module TaskDefinitions
class Base
using LokaliseManager::Utils::HashUtils
attr_accessor :config
# Creates a new importer or exporter. It accepts custom config and merges it
# with the global config (custom config take precendence)
#
# @param custom_opts [Hash]
# @param global_config [Object]
def initialize(custom_opts = {}, global_config = LokaliseManager::GlobalConfig)
primary_opts = global_config.
singleton_methods.
filter { |m| m.to_s.end_with?('=') }.
each_with_object({}) do |method, opts|
reader = method.to_s.delete_suffix('=')
opts[reader.to_sym] = global_config.send(reader)
end
all_opts = primary_opts.deep_merge(custom_opts)
config_klass = Struct.new(*all_opts.keys, keyword_init: true)
@config = config_klass.new all_opts
end
# Creates a Lokalise API client
#
# @return [Lokalise::Client]
def api_client
client_opts = [config.api_token, {enable_compression: true}.merge(config.timeouts)]
client_method = config.use_oauth2_token ? :oauth_client : :client
@api_client = ::Lokalise.send(client_method, *client_opts)
end
# Resets API client
def reset_api_client!
::Lokalise.reset_client!
::Lokalise.reset_oauth_client!
@api_client = nil
end
private
# Checks task options
#
# @return Array
def check_options_errors!
errors = []
errors << 'Project ID is not set!' if config.project_id.nil? || config.project_id.empty?
errors << 'Lokalise API token is not set!' if config.api_token.nil? || config.api_token.empty?
raise(LokaliseManager::Error, errors.join(' ')) if errors.any?
end
# Checks whether the provided file has a proper extension as dictated by the `file_ext_regexp` option
#
# @return Boolean
# @param raw_path [String, Pathname]
def proper_ext?(raw_path)
path = raw_path.is_a?(Pathname) ? raw_path : Pathname.new(raw_path)
config.file_ext_regexp.match? path.extname
end
# Returns directory and filename for the given entry
#
# @return Array
# @param entry [String]
def subdir_and_filename_for(entry)
Pathname.new(entry).split
end
# Returns Lokalise project ID and branch, semicolumn separated
#
# @return [String]
def project_id_with_branch
return config.project_id.to_s if config.branch.to_s.strip.empty?
"#{config.project_id}:#{config.branch}"
end
# Sends request with exponential backoff mechanism
def with_exp_backoff(max_retries)
return unless block_given?
retries = 0
begin
yield
rescue Lokalise::Error::TooManyRequests => e
raise(e.class, "Gave up after #{retries} retries") if retries >= max_retries
sleep 2**retries
retries += 1
retry
end
end
end
end
end