ruby-lokalise-api 7.1.1 → 8.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby_lokalise_api/base_client.rb +2 -34
  3. data/lib/ruby_lokalise_api/client.rb +2 -31
  4. data/lib/ruby_lokalise_api/collections/base.rb +97 -70
  5. data/lib/ruby_lokalise_api/collections/branches.rb +11 -0
  6. data/lib/ruby_lokalise_api/collections/contributors.rb +10 -0
  7. data/lib/ruby_lokalise_api/collections/custom_translation_statuses.rb +10 -0
  8. data/lib/ruby_lokalise_api/collections/files.rb +10 -0
  9. data/lib/ruby_lokalise_api/collections/key_comments.rb +11 -0
  10. data/lib/ruby_lokalise_api/collections/keys.rb +10 -0
  11. data/lib/ruby_lokalise_api/collections/orders.rb +10 -0
  12. data/lib/ruby_lokalise_api/collections/payment_cards.rb +10 -0
  13. data/lib/ruby_lokalise_api/collections/project_comments.rb +12 -0
  14. data/lib/ruby_lokalise_api/collections/project_languages.rb +11 -0
  15. data/lib/ruby_lokalise_api/collections/projects.rb +10 -0
  16. data/lib/ruby_lokalise_api/collections/queued_processes.rb +11 -0
  17. data/lib/ruby_lokalise_api/collections/screenshots.rb +10 -0
  18. data/lib/ruby_lokalise_api/collections/segments.rb +10 -0
  19. data/lib/ruby_lokalise_api/collections/snapshots.rb +10 -0
  20. data/lib/ruby_lokalise_api/collections/system_languages.rb +11 -0
  21. data/lib/ruby_lokalise_api/collections/tasks.rb +10 -0
  22. data/lib/ruby_lokalise_api/collections/team_user_groups.rb +11 -0
  23. data/lib/ruby_lokalise_api/collections/team_users.rb +10 -0
  24. data/lib/ruby_lokalise_api/collections/teams.rb +10 -0
  25. data/lib/ruby_lokalise_api/collections/translation_providers.rb +10 -0
  26. data/lib/ruby_lokalise_api/collections/translations.rb +10 -0
  27. data/lib/ruby_lokalise_api/collections/webhooks.rb +10 -0
  28. data/lib/ruby_lokalise_api/concerns/attrs_loadable.rb +31 -0
  29. data/lib/ruby_lokalise_api/concerns/hash_accessible.rb +16 -0
  30. data/lib/ruby_lokalise_api/concerns/unsupportable.rb +16 -0
  31. data/lib/ruby_lokalise_api/connection.rb +18 -9
  32. data/lib/ruby_lokalise_api/data/collection_attributes.yml +52 -0
  33. data/lib/ruby_lokalise_api/data/{attributes.yml → resource_attributes.yml} +64 -6
  34. data/lib/ruby_lokalise_api/endpoints/base_endpoint.rb +66 -0
  35. data/lib/ruby_lokalise_api/endpoints/branches_endpoint.rb +16 -0
  36. data/lib/ruby_lokalise_api/endpoints/contributors_endpoint.rb +16 -0
  37. data/lib/ruby_lokalise_api/endpoints/custom_translation_statuses_endpoint.rb +16 -0
  38. data/lib/ruby_lokalise_api/endpoints/files_endpoint.rb +16 -0
  39. data/lib/ruby_lokalise_api/endpoints/jwts_endpoint.rb +15 -0
  40. data/lib/ruby_lokalise_api/endpoints/key_comments_endpoint.rb +17 -0
  41. data/lib/ruby_lokalise_api/endpoints/keys_endpoint.rb +16 -0
  42. data/lib/ruby_lokalise_api/endpoints/main_endpoint.rb +25 -0
  43. data/lib/ruby_lokalise_api/endpoints/oauth2/oauth2_endpoint.rb +33 -0
  44. data/lib/ruby_lokalise_api/endpoints/orders_endpoint.rb +16 -0
  45. data/lib/ruby_lokalise_api/endpoints/payment_cards_endpoint.rb +15 -0
  46. data/lib/ruby_lokalise_api/endpoints/project_comments_endpoint.rb +15 -0
  47. data/lib/ruby_lokalise_api/endpoints/project_languages_endpoint.rb +16 -0
  48. data/lib/ruby_lokalise_api/endpoints/projects_endpoint.rb +15 -0
  49. data/lib/ruby_lokalise_api/endpoints/queued_processes_endpoint.rb +16 -0
  50. data/lib/ruby_lokalise_api/endpoints/screenshots_endpoint.rb +16 -0
  51. data/lib/ruby_lokalise_api/endpoints/segments_endpoint.rb +17 -0
  52. data/lib/ruby_lokalise_api/endpoints/snapshots_endpoint.rb +16 -0
  53. data/lib/ruby_lokalise_api/endpoints/system_languages_endpoint.rb +15 -0
  54. data/lib/ruby_lokalise_api/endpoints/tasks_endpoint.rb +16 -0
  55. data/lib/ruby_lokalise_api/endpoints/team_user_billing_details_endpoint.rb +15 -0
  56. data/lib/ruby_lokalise_api/endpoints/team_user_groups_endpoint.rb +16 -0
  57. data/lib/ruby_lokalise_api/endpoints/team_users_endpoint.rb +16 -0
  58. data/lib/ruby_lokalise_api/endpoints/teams_endpoint.rb +13 -0
  59. data/lib/ruby_lokalise_api/endpoints/translation_providers_endpoint.rb +16 -0
  60. data/lib/ruby_lokalise_api/endpoints/translations_endpoint.rb +16 -0
  61. data/lib/ruby_lokalise_api/endpoints/webhooks_endpoint.rb +16 -0
  62. data/lib/ruby_lokalise_api/error.rb +1 -0
  63. data/lib/ruby_lokalise_api/generics.rb +25 -0
  64. data/lib/ruby_lokalise_api/json_handler.rb +1 -2
  65. data/lib/ruby_lokalise_api/oauth2/auth.rb +33 -39
  66. data/lib/ruby_lokalise_api/oauth2_client.rb +3 -0
  67. data/lib/ruby_lokalise_api/request.rb +66 -18
  68. data/lib/ruby_lokalise_api/resources/base.rb +41 -181
  69. data/lib/ruby_lokalise_api/resources/branch.rb +2 -21
  70. data/lib/ruby_lokalise_api/resources/comment.rb +11 -0
  71. data/lib/ruby_lokalise_api/resources/contributor.rb +1 -9
  72. data/lib/ruby_lokalise_api/resources/custom_translation_status.rb +1 -13
  73. data/lib/ruby_lokalise_api/resources/file.rb +2 -16
  74. data/lib/ruby_lokalise_api/resources/jwt.rb +2 -5
  75. data/lib/ruby_lokalise_api/resources/key.rb +5 -8
  76. data/lib/ruby_lokalise_api/resources/oauth2_refreshed_token.rb +9 -0
  77. data/lib/ruby_lokalise_api/resources/oauth2_token.rb +9 -0
  78. data/lib/ruby_lokalise_api/resources/order.rb +2 -9
  79. data/lib/ruby_lokalise_api/resources/payment_card.rb +2 -8
  80. data/lib/ruby_lokalise_api/resources/project.rb +85 -16
  81. data/lib/ruby_lokalise_api/resources/project_language.rb +2 -10
  82. data/lib/ruby_lokalise_api/resources/queued_process.rb +3 -10
  83. data/lib/ruby_lokalise_api/resources/screenshot.rb +1 -9
  84. data/lib/ruby_lokalise_api/resources/segment.rb +2 -11
  85. data/lib/ruby_lokalise_api/resources/snapshot.rb +3 -18
  86. data/lib/ruby_lokalise_api/resources/system_language.rb +2 -1
  87. data/lib/ruby_lokalise_api/resources/task.rb +1 -9
  88. data/lib/ruby_lokalise_api/resources/team.rb +2 -0
  89. data/lib/ruby_lokalise_api/resources/team_user.rb +1 -9
  90. data/lib/ruby_lokalise_api/resources/team_user_billing_details.rb +10 -5
  91. data/lib/ruby_lokalise_api/resources/team_user_group.rb +6 -14
  92. data/lib/ruby_lokalise_api/resources/translation.rb +2 -9
  93. data/lib/ruby_lokalise_api/resources/translation_provider.rb +2 -9
  94. data/lib/ruby_lokalise_api/resources/webhook.rb +2 -16
  95. data/lib/ruby_lokalise_api/response.rb +50 -0
  96. data/lib/ruby_lokalise_api/rest/branches.rb +52 -27
  97. data/lib/ruby_lokalise_api/rest/comments.rb +48 -23
  98. data/lib/ruby_lokalise_api/rest/contributors.rb +47 -23
  99. data/lib/ruby_lokalise_api/rest/custom_translation_statuses.rb +60 -36
  100. data/lib/ruby_lokalise_api/rest/files.rb +43 -24
  101. data/lib/ruby_lokalise_api/rest/jwts.rb +21 -0
  102. data/lib/ruby_lokalise_api/rest/keys.rb +67 -34
  103. data/lib/ruby_lokalise_api/rest/languages.rb +57 -28
  104. data/lib/ruby_lokalise_api/rest/orders.rb +36 -14
  105. data/lib/ruby_lokalise_api/rest/payment_cards.rb +29 -12
  106. data/lib/ruby_lokalise_api/rest/projects.rb +55 -32
  107. data/lib/ruby_lokalise_api/rest/queued_processes.rb +21 -13
  108. data/lib/ruby_lokalise_api/rest/screenshots.rb +45 -20
  109. data/lib/ruby_lokalise_api/rest/segments.rb +31 -18
  110. data/lib/ruby_lokalise_api/rest/snapshots.rb +34 -14
  111. data/lib/ruby_lokalise_api/rest/tasks.rb +41 -19
  112. data/lib/ruby_lokalise_api/rest/team_user_billing_details.rb +33 -12
  113. data/lib/ruby_lokalise_api/rest/team_user_groups.rb +82 -50
  114. data/lib/ruby_lokalise_api/rest/team_users.rb +35 -18
  115. data/lib/ruby_lokalise_api/rest/teams.rb +10 -5
  116. data/lib/ruby_lokalise_api/rest/translation_providers.rb +24 -9
  117. data/lib/ruby_lokalise_api/rest/translations.rb +28 -15
  118. data/lib/ruby_lokalise_api/rest/webhooks.rb +54 -30
  119. data/lib/ruby_lokalise_api/rest.rb +31 -0
  120. data/lib/ruby_lokalise_api/utils/attributes.rb +37 -0
  121. data/lib/ruby_lokalise_api/utils/classes.rb +24 -0
  122. data/lib/ruby_lokalise_api/utils/keys.rb +29 -0
  123. data/lib/ruby_lokalise_api/utils/loaders.rb +24 -0
  124. data/lib/ruby_lokalise_api/utils/strings.rb +20 -0
  125. data/lib/ruby_lokalise_api/version.rb +1 -1
  126. data/lib/ruby_lokalise_api.rb +6 -2
  127. data/ruby-lokalise-api.gemspec +2 -2
  128. metadata +76 -41
  129. data/lib/ruby_lokalise_api/base_request.rb +0 -73
  130. data/lib/ruby_lokalise_api/collections/branch.rb +0 -15
  131. data/lib/ruby_lokalise_api/collections/contributor.rb +0 -13
  132. data/lib/ruby_lokalise_api/collections/custom_translation_status.rb +0 -15
  133. data/lib/ruby_lokalise_api/collections/file.rb +0 -13
  134. data/lib/ruby_lokalise_api/collections/key.rb +0 -20
  135. data/lib/ruby_lokalise_api/collections/key_comment.rb +0 -14
  136. data/lib/ruby_lokalise_api/collections/order.rb +0 -13
  137. data/lib/ruby_lokalise_api/collections/payment_card.rb +0 -13
  138. data/lib/ruby_lokalise_api/collections/project.rb +0 -13
  139. data/lib/ruby_lokalise_api/collections/project_comment.rb +0 -13
  140. data/lib/ruby_lokalise_api/collections/project_language.rb +0 -13
  141. data/lib/ruby_lokalise_api/collections/queued_process.rb +0 -15
  142. data/lib/ruby_lokalise_api/collections/screenshot.rb +0 -13
  143. data/lib/ruby_lokalise_api/collections/segment.rb +0 -15
  144. data/lib/ruby_lokalise_api/collections/snapshot.rb +0 -13
  145. data/lib/ruby_lokalise_api/collections/system_language.rb +0 -13
  146. data/lib/ruby_lokalise_api/collections/task.rb +0 -13
  147. data/lib/ruby_lokalise_api/collections/team.rb +0 -13
  148. data/lib/ruby_lokalise_api/collections/team_user.rb +0 -13
  149. data/lib/ruby_lokalise_api/collections/team_user_group.rb +0 -15
  150. data/lib/ruby_lokalise_api/collections/translation.rb +0 -13
  151. data/lib/ruby_lokalise_api/collections/translation_provider.rb +0 -13
  152. data/lib/ruby_lokalise_api/collections/webhook.rb +0 -13
  153. data/lib/ruby_lokalise_api/oauth2/refresh.rb +0 -16
  154. data/lib/ruby_lokalise_api/oauth2/token.rb +0 -16
  155. data/lib/ruby_lokalise_api/resources/key_comment.rb +0 -19
  156. data/lib/ruby_lokalise_api/resources/project_comment.rb +0 -10
  157. data/lib/ruby_lokalise_api/rest/jwt.rb +0 -15
  158. data/lib/ruby_lokalise_api/utils/attribute_helpers.rb +0 -86
  159. data/lib/ruby_lokalise_api/utils/endpoint_helpers.rb +0 -14
  160. data/lib/ruby_lokalise_api/utils/string_utils.rb +0 -33
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class SegmentsEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(project_id, key_id, language_iso, segment_number = nil)
9
+ {
10
+ projects: project_id,
11
+ keys: key_id,
12
+ segments: [language_iso, segment_number]
13
+ }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class SnapshotsEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(project_id, snapshot_id = nil)
9
+ {
10
+ projects: project_id,
11
+ snapshots: snapshot_id
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class SystemLanguagesEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(*_args)
9
+ {
10
+ system: :languages
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class TasksEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(project_id, task_id = nil)
9
+ {
10
+ projects: project_id,
11
+ tasks: task_id
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class TeamUserBillingDetailsEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(team_id)
9
+ {
10
+ teams: [team_id, :billing_details]
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class TeamUserGroupsEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(team_id, group_id = nil, *actions)
9
+ {
10
+ teams: team_id,
11
+ groups: [group_id, actions]
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class TeamUsersEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(team_id, user_id = nil)
9
+ {
10
+ teams: team_id,
11
+ users: user_id
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class TeamsEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(*_args)
9
+ [:teams]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class TranslationProvidersEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(team_id, provider_id = nil)
9
+ {
10
+ teams: team_id,
11
+ translation_providers: provider_id
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class TranslationsEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(project_id, translation_id = nil)
9
+ {
10
+ projects: project_id,
11
+ translations: translation_id
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Endpoints
5
+ class WebhooksEndpoint < MainEndpoint
6
+ private
7
+
8
+ def base_query(project_id, webhook_id = nil, *actions)
9
+ {
10
+ projects: project_id,
11
+ webhooks: [webhook_id, actions]
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLokaliseApi
4
+ # Standard API errors
4
5
  class Error < StandardError
5
6
  ClientError = Class.new(self)
6
7
  ServerError = Class.new(self)
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ # Simple generic objects to decorate certain responses (usually, related to data deletion)
5
+ class Generics
6
+ include RubyLokaliseApi::Concerns::HashAccessible
7
+
8
+ DeletedResource = Class.new(self)
9
+ EmptiedResource = Class.new(self)
10
+ CustomStatusAvailableColors = Class.new(self)
11
+ DownloadBundle = Class.new(self)
12
+ MergedBranches = Class.new(self)
13
+ RegeneratedWebhookSecret = Class.new(self)
14
+
15
+ def initialize(raw_data)
16
+ raw_data.each do |att, val|
17
+ instance_variable_set "@#{att}", val
18
+
19
+ self.class.class_exec do
20
+ attr_reader att.to_sym
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -3,9 +3,8 @@
3
3
  require 'json'
4
4
 
5
5
  module RubyLokaliseApi
6
+ # JSON custom parser. Uses built-in JSON by default but can be overridden to any other parser
6
7
  module JsonHandler
7
- # JSON custom parser. Uses built-in JSON by default but can be overridden to any other parser
8
-
9
8
  # Converts Ruby object to JSON
10
9
  def custom_dump(obj)
11
10
  JSON.dump obj
@@ -3,10 +3,10 @@
3
3
  module RubyLokaliseApi
4
4
  module OAuth2
5
5
  class Auth
6
- include RubyLokaliseApi::BaseRequest
7
-
8
6
  attr_reader :client_id, :client_secret, :timeout, :open_timeout
9
7
 
8
+ OAUTH2_ENDPOINT = RubyLokaliseApi::Endpoints::OAuth2::OAuth2Endpoint
9
+
10
10
  def initialize(client_id, client_secret, params = {})
11
11
  @client_id = client_id
12
12
  @client_secret = client_secret
@@ -14,61 +14,55 @@ module RubyLokaliseApi
14
14
  @open_timeout = params[:open_timeout]
15
15
  end
16
16
 
17
- def auth(scope:, redirect_uri: nil, state: nil)
18
- scope = scope.join(' ') if scope.is_a?(Array)
17
+ def oauth2_endpoint
18
+ self.class.const_get(:OAUTH2_ENDPOINT)
19
+ end
19
20
 
20
- params = {
21
+ def auth(scope:, redirect_uri: nil, state: nil)
22
+ get_params = {
21
23
  client_id: client_id,
22
- scope: scope
24
+ scope: (scope.is_a?(Array) ? scope.join(' ') : scope),
25
+ state: state,
26
+ redirect_uri: redirect_uri
23
27
  }
24
- params[:state] = state unless state.nil?
25
- params[:redirect_uri] = redirect_uri unless redirect_uri.nil?
26
28
 
27
- _build_url_from params
29
+ oauth2_endpoint.new(self, query: 'auth', get: get_params).full_uri
28
30
  end
29
31
 
30
32
  def token(code)
31
- params = base_params.merge({
32
- code: code,
33
- grant_type: 'authorization_code'
34
- })
35
-
36
- RubyLokaliseApi::OAuth2::Token.new post('token', self, params)
37
- end
38
-
39
- def refresh(token)
40
- params = base_params.merge({
41
- refresh_token: token,
42
- grant_type: 'refresh_token'
43
- })
33
+ endpoint = oauth2_endpoint.new(
34
+ self,
35
+ query: :token,
36
+ req: common_params.merge(
37
+ grant_type: 'authorization_code',
38
+ code: code
39
+ )
40
+ )
44
41
 
45
- RubyLokaliseApi::OAuth2::Refresh.new post('token', self, params)
42
+ RubyLokaliseApi::Resources::OAuth2Token.new endpoint.do_post
46
43
  end
47
44
 
48
- def base_url
49
- URI('https://app.lokalise.com/oauth2/')
50
- end
45
+ def refresh(refresh_token)
46
+ endpoint = oauth2_endpoint.new(
47
+ self,
48
+ query: :token,
49
+ req: common_params.merge(
50
+ grant_type: 'refresh_token',
51
+ refresh_token: refresh_token
52
+ )
53
+ )
51
54
 
52
- def compression?
53
- false
55
+ RubyLokaliseApi::Resources::OAuth2RefreshedToken.new endpoint.do_post
54
56
  end
55
57
 
56
58
  private
57
59
 
58
- def base_params
60
+ def common_params
59
61
  {
60
- client_id: client_id,
61
- client_secret: client_secret
62
+ client_id: @client_id,
63
+ client_secret: @client_secret
62
64
  }
63
65
  end
64
-
65
- def _build_url_from(params)
66
- URI::HTTPS.build(
67
- host: base_url.host,
68
- path: "#{base_url.path}auth",
69
- query: URI.encode_www_form(params)
70
- ).to_s
71
- end
72
66
  end
73
67
  end
74
68
  end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLokaliseApi
4
+ # Client used to perform API requests with an OAuth2 access token
4
5
  class OAuth2Client < Client
6
+ include RubyLokaliseApi::Rest
7
+
5
8
  def initialize(token, params = {})
6
9
  super(token, params)
7
10
  @token_header = 'Authorization'
@@ -1,31 +1,79 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLokaliseApi
4
+ # Contains methods to perform the actual HTTP requests
4
5
  module Request
5
- include RubyLokaliseApi::BaseRequest
6
+ include RubyLokaliseApi::JsonHandler
7
+ include RubyLokaliseApi::Connection
6
8
 
7
- # Lokalise returns pagination info in special headers
8
- PAGINATION_HEADERS = %w[x-pagination-total-count x-pagination-page-count x-pagination-limit
9
- x-pagination-page].freeze
9
+ # Sends a GET request
10
+ def get(endpoint)
11
+ respond_with(
12
+ connection(endpoint).get(prepare(endpoint.uri), endpoint.req_params),
13
+ endpoint
14
+ )
15
+ end
16
+
17
+ # Sends a POST request
18
+ def post(endpoint)
19
+ respond_with(
20
+ connection(endpoint).post(prepare(endpoint.uri), custom_dump(endpoint.req_params)),
21
+ endpoint
22
+ )
23
+ end
24
+
25
+ # Sends a PUT request
26
+ def put(endpoint)
27
+ respond_with(
28
+ connection(endpoint).put(prepare(endpoint.uri), custom_dump(endpoint.req_params)),
29
+ endpoint
30
+ )
31
+ end
32
+
33
+ # Sends a PATCH request
34
+ def patch(endpoint)
35
+ respond_with(
36
+ connection(endpoint).patch(prepare(endpoint.uri),
37
+ endpoint.req_params.nil? ? nil : custom_dump(endpoint.req_params)),
38
+ endpoint
39
+ )
40
+ end
41
+
42
+ # Sends a DELETE request
43
+ def delete(endpoint)
44
+ respond_with(
45
+ connection(endpoint).delete(prepare(endpoint.uri)) do |req|
46
+ req.body = custom_dump endpoint.req_params
47
+ end,
48
+ endpoint
49
+ )
50
+ end
10
51
 
11
52
  private
12
53
 
13
- def respond_with(response, client)
54
+ # Get rid of double slashes in the `path`, leading and trailing slash
55
+ def prepare(path)
56
+ path.delete_prefix('/').gsub(%r{//}, '/').gsub(%r{/+\z}, '')
57
+ end
58
+
59
+ def raise_on_error!(response, body)
60
+ return unless !response.success? || (body.respond_to?(:has_key?) && body.key?('error'))
61
+
62
+ respond_with_error(response.status, body)
63
+ end
64
+
65
+ def respond_with(response, endpoint)
14
66
  body = custom_load response.body
15
- uri = Addressable::URI.parse response.env.url
67
+
16
68
  raise_on_error! response, body
17
- extract_headers_from(response).
18
- merge('content' => body,
19
- 'client' => client,
20
- 'path' => uri.path.gsub(%r{/api2/}, ''))
21
- end
22
-
23
- # Keep only pagination headers
24
- def extract_headers_from(response)
25
- response.
26
- headers.
27
- to_h.
28
- keep_if { |k, _v| PAGINATION_HEADERS.include?(k) }
69
+
70
+ RubyLokaliseApi::Response.new(body, endpoint, response.headers)
71
+ end
72
+
73
+ def respond_with_error(code, body)
74
+ raise(RubyLokaliseApi::Error, body['error'] || body) unless RubyLokaliseApi::Error::ERRORS.key? code
75
+
76
+ raise RubyLokaliseApi::Error::ERRORS[code].from_response(body)
29
77
  end
30
78
  end
31
79
  end