crowdin-api 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +96 -31
- data/README.md +8 -6
- data/bin/crowdin-console +1 -1
- data/crowdin-api.gemspec +1 -2
- data/lib/crowdin-api/{api-resources → api_resources}/dictionaries.rb +8 -10
- data/lib/crowdin-api/{api-resources → api_resources}/distributions.rb +25 -32
- data/lib/crowdin-api/{api-resources → api_resources}/glossaries.rb +64 -82
- data/lib/crowdin-api/{api-resources → api_resources}/labels.rb +28 -47
- data/lib/crowdin-api/api_resources/languages.rb +61 -0
- data/lib/crowdin-api/api_resources/machine_translation_engines.rb +79 -0
- data/lib/crowdin-api/api_resources/projects.rb +124 -0
- data/lib/crowdin-api/api_resources/reports.rb +120 -0
- data/lib/crowdin-api/{api-resources → api_resources}/screenshots.rb +47 -61
- data/lib/crowdin-api/{api-resources → api_resources}/source_files.rb +68 -131
- data/lib/crowdin-api/{api-resources → api_resources}/source_strings.rb +19 -24
- data/lib/crowdin-api/api_resources/storages.rb +54 -0
- data/lib/crowdin-api/{api-resources → api_resources}/string_comments.rb +18 -23
- data/lib/crowdin-api/{api-resources → api_resources}/string_translations.rb +63 -90
- data/lib/crowdin-api/{api-resources → api_resources}/tasks.rb +30 -41
- data/lib/crowdin-api/api_resources/teams.rb +135 -0
- data/lib/crowdin-api/{api-resources → api_resources}/translation_memory.rb +38 -52
- data/lib/crowdin-api/{api-resources → api_resources}/translation_status.rb +24 -30
- data/lib/crowdin-api/{api-resources → api_resources}/translations.rb +40 -58
- data/lib/crowdin-api/api_resources/users.rb +161 -0
- data/lib/crowdin-api/api_resources/vendors.rb +21 -0
- data/lib/crowdin-api/{api-resources → api_resources}/webhooks.rb +19 -24
- data/lib/crowdin-api/api_resources/workflows.rb +59 -0
- data/lib/crowdin-api/client/client.rb +51 -42
- data/lib/crowdin-api/client/configuration.rb +12 -12
- data/lib/crowdin-api/client/version.rb +1 -1
- data/lib/crowdin-api/core/errors_raisers.rb +1 -1
- data/lib/crowdin-api/core/request.rb +53 -88
- data/lib/crowdin-api/core/send_request.rb +67 -0
- data/lib/crowdin-api.rb +17 -24
- data/spec/api_resources/dictionaries_spec.rb +23 -0
- data/spec/api_resources/distributions_spec.rb +71 -0
- data/spec/api_resources/glossaries_spec.rb +167 -0
- data/spec/api_resources/labels_spec.rb +71 -0
- data/spec/api_resources/languages_spec.rb +51 -0
- data/spec/api_resources/machine_translation_engines_spec.rb +63 -0
- data/spec/api_resources/projects_spec.rb +215 -0
- data/spec/api_resources/reports_spec.rb +95 -0
- data/spec/api_resources/screenshots_spec.rb +134 -0
- data/spec/api_resources/source_files_spec.rb +13 -0
- data/spec/api_resources/source_strings_spec.rb +51 -0
- data/spec/api_resources/storages_spec.rb +13 -0
- data/spec/api_resources/string_comments_spec.rb +13 -0
- data/spec/api_resources/string_translations_spec.rb +13 -0
- data/spec/api_resources/tasks_spec.rb +79 -0
- data/spec/api_resources/teams_spec.rb +13 -0
- data/spec/api_resources/translation_memory_spec.rb +13 -0
- data/spec/api_resources/translation_status_spec.rb +15 -0
- data/spec/api_resources/translations_spec.rb +107 -0
- data/spec/api_resources/users_spec.rb +117 -0
- data/spec/api_resources/vendors_spec.rb +13 -0
- data/spec/api_resources/webhooks_spec.rb +51 -0
- data/spec/api_resources/workflows_spec.rb +41 -0
- data/spec/spec_helper.rb +20 -2
- data/spec/unit/client_spec.rb +85 -0
- metadata +64 -43
- data/bin/setup +0 -6
- data/lib/crowdin-api/api-resources/languages.rb +0 -82
- data/lib/crowdin-api/api-resources/machine_translation_engines.rb +0 -74
- data/lib/crowdin-api/api-resources/projects.rb +0 -148
- data/lib/crowdin-api/api-resources/reports.rb +0 -138
- data/lib/crowdin-api/api-resources/storages.rb +0 -106
- data/lib/crowdin-api/api-resources/teams.rb +0 -144
- data/lib/crowdin-api/api-resources/users.rb +0 -129
- data/lib/crowdin-api/api-resources/vendors.rb +0 -21
- data/lib/crowdin-api/api-resources/workflows.rb +0 -62
- data/lib/crowdin-api/core/utils.rb +0 -10
- data/spec/client/client-instance_spec.rb +0 -14
- data/spec/client/configuration-instance_spec.rb +0 -72
@@ -7,26 +7,24 @@ module Crowdin
|
|
7
7
|
project_id || raise_project_id_is_required_error
|
8
8
|
|
9
9
|
request = Web::Request.new(
|
10
|
-
|
10
|
+
connection,
|
11
11
|
:get,
|
12
|
-
"/projects/#{project_id}/webhooks",
|
13
|
-
query
|
12
|
+
"#{config.target_api_url}/projects/#{project_id}/webhooks",
|
13
|
+
{ params: query }
|
14
14
|
)
|
15
|
-
|
16
|
-
request.perform
|
15
|
+
Web::SendRequest.new(request).perform
|
17
16
|
end
|
18
17
|
|
19
18
|
def add_webhook(query = {}, project_id = config.project_id)
|
20
19
|
project_id || raise_project_id_is_required_error
|
21
20
|
|
22
21
|
request = Web::Request.new(
|
23
|
-
|
22
|
+
connection,
|
24
23
|
:post,
|
25
|
-
"/projects/#{project_id}/webhooks",
|
26
|
-
query
|
24
|
+
"#{config.target_api_url}/projects/#{project_id}/webhooks",
|
25
|
+
{ params: query }
|
27
26
|
)
|
28
|
-
|
29
|
-
request.perform
|
27
|
+
Web::SendRequest.new(request).perform
|
30
28
|
end
|
31
29
|
|
32
30
|
def get_webhook(webhook_id = nil, project_id = config.project_id)
|
@@ -34,12 +32,11 @@ module Crowdin
|
|
34
32
|
project_id || raise_project_id_is_required_error
|
35
33
|
|
36
34
|
request = Web::Request.new(
|
37
|
-
|
35
|
+
connection,
|
38
36
|
:get,
|
39
|
-
"/projects/#{project_id}/webhooks/#{webhook_id}"
|
37
|
+
"#{config.target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}"
|
40
38
|
)
|
41
|
-
|
42
|
-
request.perform
|
39
|
+
Web::SendRequest.new(request).perform
|
43
40
|
end
|
44
41
|
|
45
42
|
def delete_webhook(webhook_id = nil, project_id = config.project_id)
|
@@ -47,26 +44,24 @@ module Crowdin
|
|
47
44
|
project_id || raise_project_id_is_required_error
|
48
45
|
|
49
46
|
request = Web::Request.new(
|
50
|
-
|
47
|
+
connection,
|
51
48
|
:delete,
|
52
|
-
"/projects/#{project_id}/webhooks/#{webhook_id}"
|
49
|
+
"#{config.target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}"
|
53
50
|
)
|
54
|
-
|
55
|
-
request.perform
|
51
|
+
Web::SendRequest.new(request).perform
|
56
52
|
end
|
57
53
|
|
58
|
-
def
|
54
|
+
def edit_webhook(webhook_id = nil, query = {}, project_id = config.project_id)
|
59
55
|
webhook_id || raise_parameter_is_required_error(:webhook_id)
|
60
56
|
project_id || raise_project_id_is_required_error
|
61
57
|
|
62
58
|
request = Web::Request.new(
|
63
|
-
|
59
|
+
connection,
|
64
60
|
:patch,
|
65
|
-
"/projects/#{project_id}/webhooks/#{webhook_id}",
|
66
|
-
query
|
61
|
+
"#{config.target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}",
|
62
|
+
{ params: query }
|
67
63
|
)
|
68
|
-
|
69
|
-
request.perform
|
64
|
+
Web::SendRequest.new(request).perform
|
70
65
|
end
|
71
66
|
end
|
72
67
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Crowdin
|
4
|
+
module ApiResources
|
5
|
+
module Workflows
|
6
|
+
# -- For Enterprise mode only --
|
7
|
+
|
8
|
+
def list_workflow_steps(query = {}, project_id = config.project_id)
|
9
|
+
enterprise_mode? || raise_only_for_enterprise_mode_error
|
10
|
+
project_id || raise_project_id_is_required_error
|
11
|
+
|
12
|
+
request = Web::Request.new(
|
13
|
+
connection,
|
14
|
+
:get,
|
15
|
+
"#{config.target_api_url}/projects/#{project_id}/workflow-steps",
|
16
|
+
{ params: query }
|
17
|
+
)
|
18
|
+
Web::SendRequest.new(request).perform
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_workflow_step(step_id = nil, project_id = config.project_id)
|
22
|
+
enterprise_mode? || raise_only_for_enterprise_mode_error
|
23
|
+
step_id || raise_parameter_is_required_error(:step_id)
|
24
|
+
project_id || raise_project_id_is_required_error
|
25
|
+
|
26
|
+
request = Web::Request.new(
|
27
|
+
connection,
|
28
|
+
:get,
|
29
|
+
"#{config.target_api_url}/projects/#{project_id}/workflow-steps/#{step_id}"
|
30
|
+
)
|
31
|
+
Web::SendRequest.new(request).perform
|
32
|
+
end
|
33
|
+
|
34
|
+
def list_workflow_templates(query = {})
|
35
|
+
enterprise_mode? || raise_only_for_enterprise_mode_error
|
36
|
+
|
37
|
+
request = Web::Request.new(
|
38
|
+
connection,
|
39
|
+
:get,
|
40
|
+
"#{config.target_api_url}/workflow-templates",
|
41
|
+
{ params: query }
|
42
|
+
)
|
43
|
+
Web::SendRequest.new(request).perform
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_workflow_template(template_id = nil)
|
47
|
+
enterprise_mode? || raise_only_for_enterprise_mode_error
|
48
|
+
template_id || raise_parameter_is_required_error(:template_id)
|
49
|
+
|
50
|
+
request = Web::Request.new(
|
51
|
+
connection,
|
52
|
+
:get,
|
53
|
+
"#{config.target_api_url}/workflow-templates/#{template_id}"
|
54
|
+
)
|
55
|
+
Web::SendRequest.new(request).perform
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -20,76 +20,85 @@ module Crowdin
|
|
20
20
|
# crowdin.list_projects
|
21
21
|
#
|
22
22
|
class Client
|
23
|
-
|
24
|
-
|
25
|
-
# API Resources modules
|
26
|
-
API_RESOURCES_MODULES = %i[Storages Languages Projects Workflows SourceFiles Translations SourceStrings
|
27
|
-
StringTranslations StringComments Screenshots Glossaries TranslationMemory
|
28
|
-
MachineTranslationEngines Reports Tasks Users Teams Vendors Webhooks
|
29
|
-
Dictionaries Distributions Labels TranslationStatus].freeze
|
30
|
-
|
31
|
-
# Error Raisers modules
|
32
|
-
ERROR_RAISERS_MODULES = %i[ApiErrorsRaiser ClientErrorsRaiser].freeze
|
33
|
-
|
34
|
-
# Processing all API Resources modules to include them to the Client
|
23
|
+
# Processing API Resources modules to include them to the Client
|
35
24
|
API_RESOURCES_MODULES.each do |module_name|
|
36
|
-
Client.send(:include,
|
25
|
+
Client.send(:include, Object.const_get("Crowdin::ApiResources::#{module_name}"))
|
37
26
|
end
|
38
27
|
|
39
|
-
# Processing
|
28
|
+
# Processing Error Raisers modules to include them to the Client
|
40
29
|
ERROR_RAISERS_MODULES.each do |module_name|
|
41
|
-
Client.send(:include,
|
30
|
+
Client.send(:include, Object.const_get("Crowdin::Errors::#{module_name}"))
|
42
31
|
end
|
43
32
|
|
33
|
+
# Config instance that includes configuration options for the Client
|
44
34
|
attr_reader :config
|
35
|
+
# Instance with established connection through RestClient to the Crowdin API
|
45
36
|
attr_reader :connection
|
37
|
+
# Instance with options and headers for RestClient connection
|
46
38
|
attr_reader :options
|
39
|
+
# Logger instance
|
47
40
|
attr_reader :logger
|
48
41
|
|
49
42
|
def initialize(&block)
|
50
43
|
build_configuration(&block)
|
51
44
|
|
52
|
-
|
53
|
-
|
45
|
+
update_logger
|
46
|
+
update_rest_client_proxy
|
54
47
|
|
55
48
|
build_connection
|
56
49
|
end
|
57
50
|
|
58
|
-
def log
|
59
|
-
!
|
51
|
+
def log(message)
|
52
|
+
!logger_enabled? || logger.debug(message)
|
60
53
|
end
|
61
54
|
|
62
55
|
def logger=(logger)
|
63
|
-
raise_logger_are_not_enabled_error unless
|
64
|
-
@logger = logger
|
65
|
-
end
|
66
|
-
|
67
|
-
private
|
56
|
+
raise_logger_are_not_enabled_error unless logger_enabled?
|
68
57
|
|
69
|
-
|
70
|
-
|
71
|
-
yield config if block_given?
|
72
|
-
end
|
73
|
-
|
74
|
-
def build_connection
|
75
|
-
@connection ||= ::RestClient::Resource.new(config.base_url, build_options)
|
58
|
+
@logger = logger
|
59
|
+
update_rest_client_logger
|
76
60
|
end
|
77
61
|
|
78
|
-
def
|
79
|
-
|
62
|
+
def enterprise_mode?
|
63
|
+
!!config.organization_domain
|
80
64
|
end
|
81
65
|
|
82
|
-
def
|
83
|
-
|
84
|
-
@logger ||= Logger.new($stderr)
|
66
|
+
def logger_enabled?
|
67
|
+
config.logger_enabled?
|
85
68
|
end
|
86
69
|
|
87
|
-
|
88
|
-
ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false
|
89
|
-
end
|
70
|
+
private
|
90
71
|
|
91
|
-
|
92
|
-
|
93
|
-
|
72
|
+
def build_configuration
|
73
|
+
@config = Crowdin::Configuration.new
|
74
|
+
yield config if block_given?
|
75
|
+
end
|
76
|
+
|
77
|
+
def build_connection
|
78
|
+
build_options
|
79
|
+
@connection ||= ::RestClient::Resource.new(config.base_url, options)
|
80
|
+
end
|
81
|
+
|
82
|
+
def build_options
|
83
|
+
@options ||= config.options.merge(headers: config.headers)
|
84
|
+
end
|
85
|
+
|
86
|
+
def set_default_logger
|
87
|
+
require 'logger'
|
88
|
+
@logger ||= Logger.new($stdout)
|
89
|
+
update_rest_client_logger
|
90
|
+
end
|
91
|
+
|
92
|
+
def update_rest_client_logger
|
93
|
+
::RestClient.log = @logger
|
94
|
+
end
|
95
|
+
|
96
|
+
def update_rest_client_proxy
|
97
|
+
ENV['http_proxy'] ? ::RestClient.proxy = ENV.fetch('http_proxy') : false
|
98
|
+
end
|
99
|
+
|
100
|
+
def update_logger
|
101
|
+
config.logger_enabled? ? set_default_logger : config.enable_logger = false
|
102
|
+
end
|
94
103
|
end
|
95
104
|
end
|
@@ -5,12 +5,12 @@ module Crowdin
|
|
5
5
|
attr_accessor :api_token
|
6
6
|
attr_accessor :project_id
|
7
7
|
attr_accessor :organization_domain
|
8
|
+
|
8
9
|
attr_accessor :enable_logger
|
10
|
+
alias logger_enabled? enable_logger
|
9
11
|
|
10
12
|
attr_reader :target_api_url
|
11
13
|
|
12
|
-
alias logger_enabled? enable_logger
|
13
|
-
|
14
14
|
def initialize
|
15
15
|
@target_api_url = '/api/v2'
|
16
16
|
end
|
@@ -33,16 +33,16 @@ module Crowdin
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def base_url
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
36
|
+
@base_url ||=
|
37
|
+
if !!organization_domain
|
38
|
+
if organization_domain.include?('.com')
|
39
|
+
"https://#{organization_domain}"
|
40
|
+
else
|
41
|
+
"https://#{organization_domain}.api.crowdin.com"
|
42
|
+
end
|
43
|
+
else
|
44
|
+
'https://api.crowdin.com'
|
45
|
+
end
|
45
46
|
end
|
46
|
-
alias enterprise_mode? organization_domain?
|
47
47
|
end
|
48
48
|
end
|
@@ -6,7 +6,7 @@ module Crowdin
|
|
6
6
|
module ClientErrorsRaiser
|
7
7
|
def raise_logger_are_not_enabled_error
|
8
8
|
raise(LoggerAreNotEnabledError, 'Logger are not enabled in your Client configuration, enable it ' \
|
9
|
-
'before setting your
|
9
|
+
'before setting your logger')
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -3,116 +3,81 @@
|
|
3
3
|
module Crowdin
|
4
4
|
module Web
|
5
5
|
class Request
|
6
|
-
attr_reader :
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@destination = destination
|
15
|
-
@errors = []
|
6
|
+
attr_reader :connection
|
7
|
+
attr_reader :method
|
8
|
+
attr_reader :payload
|
9
|
+
|
10
|
+
def initialize(connection, method, url, payload = {})
|
11
|
+
@connection = connection[url]
|
12
|
+
@method = method
|
13
|
+
@payload = build_payload(payload)
|
16
14
|
end
|
17
15
|
|
18
|
-
def
|
19
|
-
|
20
|
-
process_response!
|
16
|
+
def get?
|
17
|
+
method.eql?(:get)
|
21
18
|
end
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
def process_request!
|
26
|
-
return @response = client.connection[@full_path].delete if delete_request?
|
27
|
-
return @response = client.connection[@full_path].get(@payload) if get_request?
|
28
|
-
|
29
|
-
client.connection[@full_path].send(@method, @payload, @headers) { |response, _, _| @response = response }
|
30
|
-
rescue StandardError => error
|
31
|
-
client.log! error
|
32
|
-
|
33
|
-
@errors << "Something went wrong while request proccessing. Details - #{error.class}"
|
20
|
+
def get
|
21
|
+
connection.get(prepare_payload(payload[:params]))
|
34
22
|
end
|
35
23
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
begin
|
40
|
-
if @response
|
41
|
-
client.log! "args: #{@response.request.args}"
|
42
|
-
|
43
|
-
if @response.body.empty?
|
44
|
-
@response.code
|
45
|
-
else
|
46
|
-
doc = JSON.parse(@response.body)
|
47
|
-
|
48
|
-
client.log! "body: #{doc}"
|
49
|
-
|
50
|
-
data = fetch_response_data(doc)
|
51
|
-
|
52
|
-
@errors.any? ? fetch_errors : data
|
53
|
-
end
|
54
|
-
end
|
55
|
-
rescue StandardError => error
|
56
|
-
client.log! error
|
57
|
-
|
58
|
-
@errors << "Something went wrong while response proccessing. Details - #{error.class}"
|
59
|
-
|
60
|
-
fetch_errors
|
61
|
-
end
|
24
|
+
def delete?
|
25
|
+
method.eql?(:delete)
|
62
26
|
end
|
63
27
|
|
64
|
-
def
|
65
|
-
|
66
|
-
|
67
|
-
get_request? ? { params: fetch_cleared_query(query) } : fetch_cleared_query(query).to_json
|
28
|
+
def delete
|
29
|
+
connection.delete
|
68
30
|
end
|
69
31
|
|
70
|
-
def
|
71
|
-
|
72
|
-
|
73
|
-
.match(/filename=("?)(.+)\1/)[2]
|
32
|
+
def patch?
|
33
|
+
method.eql?(:patch)
|
34
|
+
end
|
74
35
|
|
75
|
-
|
36
|
+
def patch
|
37
|
+
connection.patch(prepare_payload(payload[:params]), payload[:headers]) { |response, _, _| response }
|
38
|
+
end
|
76
39
|
|
77
|
-
|
78
|
-
|
79
|
-
|
40
|
+
def post?
|
41
|
+
method.eql?(:post)
|
42
|
+
end
|
80
43
|
|
81
|
-
|
44
|
+
def post
|
45
|
+
connection.post(prepare_payload(payload[:params]), payload[:headers]) { |response, _, _| response }
|
82
46
|
end
|
83
47
|
|
84
|
-
def
|
85
|
-
|
48
|
+
def put?
|
49
|
+
method.eql?(:put)
|
86
50
|
end
|
87
51
|
|
88
|
-
def
|
89
|
-
|
90
|
-
download_file(doc['data']['url'])
|
91
|
-
else
|
92
|
-
doc
|
93
|
-
end
|
52
|
+
def put
|
53
|
+
connection.put(prepare_payload(payload[:params]), payload[:headers]) { |response, _, _| response }
|
94
54
|
end
|
95
55
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
query.reject { |_, value| value.nil? }
|
104
|
-
else
|
105
|
-
query
|
56
|
+
private
|
57
|
+
|
58
|
+
def build_payload(payload)
|
59
|
+
%i[params headers].each do |key|
|
60
|
+
payload[key] ||= {}
|
61
|
+
end
|
62
|
+
payload
|
106
63
|
end
|
107
|
-
end
|
108
64
|
|
109
|
-
|
110
|
-
|
111
|
-
end
|
65
|
+
def prepare_payload(params)
|
66
|
+
return params if params.is_a?(File)
|
112
67
|
|
113
|
-
|
114
|
-
|
115
|
-
|
68
|
+
get? ? { params: fetch_cleared_params(params) } : fetch_cleared_params(params).to_json
|
69
|
+
end
|
70
|
+
|
71
|
+
def fetch_cleared_params(params)
|
72
|
+
case params
|
73
|
+
when Array
|
74
|
+
params.map { |el| el.reject { |_, value| value.nil? } }.reject(&:empty?)
|
75
|
+
when Hash
|
76
|
+
params.reject { |_, value| value.nil? }
|
77
|
+
else
|
78
|
+
params
|
79
|
+
end
|
80
|
+
end
|
116
81
|
end
|
117
82
|
end
|
118
83
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Crowdin
|
4
|
+
module Web
|
5
|
+
class SendRequest
|
6
|
+
attr_reader :request
|
7
|
+
|
8
|
+
def initialize(request, file_destination = nil)
|
9
|
+
@request = request
|
10
|
+
@file_destination = file_destination
|
11
|
+
@errors = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def perform
|
15
|
+
parse_response(process_request)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def process_request
|
21
|
+
request.send(request.method)
|
22
|
+
rescue StandardError => e
|
23
|
+
@errors << "Something went wrong while request processing. Details - #{e.message}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse_response(response)
|
27
|
+
return @errors.join('; ') if @errors.any?
|
28
|
+
|
29
|
+
begin
|
30
|
+
if response
|
31
|
+
if response.body.empty?
|
32
|
+
response.code
|
33
|
+
else
|
34
|
+
parsed_body = JSON.parse(response.body)
|
35
|
+
parsed_response = fetch_response_data(parsed_body)
|
36
|
+
|
37
|
+
@errors.any? ? @errors.join('; ') : parsed_response
|
38
|
+
end
|
39
|
+
end
|
40
|
+
rescue StandardError => e
|
41
|
+
@errors << "Something went wrong while response processing. Details - #{e.message}"
|
42
|
+
@errors.join('; ')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def fetch_response_data(doc)
|
47
|
+
if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].include?('response-content-disposition')
|
48
|
+
download_file(doc['data']['url'])
|
49
|
+
else
|
50
|
+
doc
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def download_file(url)
|
55
|
+
download = URI.parse(url).open
|
56
|
+
destination = @file_destination || download.meta['content-disposition']
|
57
|
+
.match(/filename=("?)(.+)\1/)[2]
|
58
|
+
|
59
|
+
IO.copy_stream(download, destination)
|
60
|
+
|
61
|
+
destination
|
62
|
+
rescue StandardError => e
|
63
|
+
@errors << "Something went wrong while downloading file. Details - #{e.message}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/crowdin-api.rb
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module Crowdin
|
4
|
+
# API Resources modules
|
5
|
+
API_RESOURCES_MODULES = %i[Storages Languages Projects Workflows SourceFiles Translations SourceStrings
|
6
|
+
StringTranslations StringComments Screenshots Glossaries TranslationMemory
|
7
|
+
MachineTranslationEngines Reports Tasks Users Teams Vendors Webhooks
|
8
|
+
Dictionaries Distributions Labels TranslationStatus].freeze
|
9
|
+
|
10
|
+
# Error Raisers modules
|
11
|
+
ERROR_RAISERS_MODULES = %i[ApiErrorsRaiser ClientErrorsRaiser].freeze
|
12
|
+
end
|
13
|
+
|
3
14
|
# Libs
|
4
15
|
require 'json'
|
5
16
|
require 'open-uri'
|
@@ -9,32 +20,14 @@ require 'rest-client'
|
|
9
20
|
require 'crowdin-api/core/errors'
|
10
21
|
require 'crowdin-api/core/errors_raisers'
|
11
22
|
require 'crowdin-api/core/request'
|
12
|
-
require 'crowdin-api/core/
|
23
|
+
require 'crowdin-api/core/send_request'
|
13
24
|
|
14
25
|
# API modules
|
15
|
-
|
16
|
-
require
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
require 'crowdin-api/api-resources/translations'
|
21
|
-
require 'crowdin-api/api-resources/source_strings'
|
22
|
-
require 'crowdin-api/api-resources/string_translations'
|
23
|
-
require 'crowdin-api/api-resources/string_comments'
|
24
|
-
require 'crowdin-api/api-resources/screenshots'
|
25
|
-
require 'crowdin-api/api-resources/glossaries'
|
26
|
-
require 'crowdin-api/api-resources/translation_memory'
|
27
|
-
require 'crowdin-api/api-resources/machine_translation_engines'
|
28
|
-
require 'crowdin-api/api-resources/reports'
|
29
|
-
require 'crowdin-api/api-resources/tasks'
|
30
|
-
require 'crowdin-api/api-resources/users'
|
31
|
-
require 'crowdin-api/api-resources/teams'
|
32
|
-
require 'crowdin-api/api-resources/vendors'
|
33
|
-
require 'crowdin-api/api-resources/webhooks'
|
34
|
-
require 'crowdin-api/api-resources/dictionaries'
|
35
|
-
require 'crowdin-api/api-resources/distributions'
|
36
|
-
require 'crowdin-api/api-resources/labels'
|
37
|
-
require 'crowdin-api/api-resources/translation_status'
|
26
|
+
Crowdin::API_RESOURCES_MODULES.each do |api_resource|
|
27
|
+
require "crowdin-api/api_resources/#{api_resource.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase}"
|
28
|
+
rescue LoadError
|
29
|
+
# Ignored
|
30
|
+
end
|
38
31
|
|
39
32
|
# Client modules
|
40
33
|
require 'crowdin-api/client/version'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe Crowdin::ApiResources::Dictionaries do
|
4
|
+
describe 'Default endpoints' do
|
5
|
+
describe '#list_dictionaries' do
|
6
|
+
it 'when request are valid', :default do
|
7
|
+
stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/dictionaries")
|
8
|
+
list_dictionaries = @crowdin.list_dictionaries({}, project_id)
|
9
|
+
expect(list_dictionaries).to eq(200)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#edit_dictionary' do
|
14
|
+
let(:language_id) { 1 }
|
15
|
+
|
16
|
+
it 'when request are valid', :default do
|
17
|
+
stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/dictionaries/#{language_id}")
|
18
|
+
edit_dictionary = @crowdin.edit_dictionary(language_id, {}, project_id)
|
19
|
+
expect(edit_dictionary).to eq(200)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|