ruby-lokalise-api 7.2.0 → 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 -185
  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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9945f7943aac029086c1140f091b893f0ee19daad1658025d6bcf8df80a1b2ea
4
- data.tar.gz: 15e487ca72e3bd5a06569885a7cb01082330ba4ccf68545e116873582596d918
3
+ metadata.gz: b7f5c5e9ee2007a925415b1b823a2d1292c2875a4745faf226090796b4762d40
4
+ data.tar.gz: 95adfa6da4239686f103ead65616d29d2ad96f4c3df25c402aeb21a0dc120baf
5
5
  SHA512:
6
- metadata.gz: 66da1dc1844fa17b05d27416b2e0b2760cbb439cb0b9a22163f07d310127a9d1ca52d129d3b6c64249ef25c27501035c8c216f80b0605d45be62f744ccbe0d71
7
- data.tar.gz: a9f471a3f5341c9ac99f50cb1222889d56cee9f26a57a867655f4a3c9d3bbed8f0df76efc0a3b8eaa76372deecc9e3d06b60f791baf4e1d8383c981bbc732920
6
+ metadata.gz: 6b8b417199e57e73453a065707529422f4cdcd4b4f173138b01fed63ecc404bf7df9c78c374da40fa4551174406369c7c2b213d2dd557c9465874c15ac87c598
7
+ data.tar.gz: bfba53bd1b7e8a65bb8e778de4f379f05a4415a43bf16405b2af7f2e6fdece3adacac4a086e20d029523a196248adbaaca04e9ebc71c40fe3a9775cf945c75f8
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLokaliseApi
4
+ # This class contains the base client. Inherited by Client (regular API client)
5
+ # and OAuth2Client (used for OAuth-2 based authentication)
4
6
  class BaseClient
5
7
  attr_reader :token, :token_header
6
8
  attr_accessor :timeout, :open_timeout
@@ -11,39 +13,5 @@ module RubyLokaliseApi
11
13
  @open_timeout = params.fetch(:open_timeout, nil)
12
14
  @token_header = ''
13
15
  end
14
-
15
- # rubocop:disable Metrics/ParameterLists
16
- # Constructs request to perform the specified action
17
- # @param klass The actual class to call the method upon
18
- # @param method [Symbol] The method to call (:new, :update, :create etc)
19
- # @param endpoint_ids [Array, Hash] IDs that are used to generate the proper path to the endpoint
20
- # @param params [Array, Hash] Request parameters
21
- # @param object_key [String, Symbol] Key that should be used to wrap parameters into
22
- # @param initial_ids [Array] IDs that should be used to generate base endpoint path.
23
- # The base path is used for method chaining
24
- def construct_request(klass, method, endpoint_ids, params = {}, object_key = nil, initial_ids = nil)
25
- path = klass.endpoint(*endpoint_ids)
26
- formatted_params = format_params(params, object_key)
27
- formatted_params[:_initial_path] = klass.endpoint(*initial_ids) if initial_ids
28
- klass.send method, self, path, formatted_params
29
- end
30
- # rubocop:enable Metrics/ParameterLists
31
-
32
- # Converts `params` to hash with arrays under the `object_key` key.
33
- # Used in bulk operations
34
- #
35
- # @return [Hash]
36
- def format_params(params, object_key)
37
- return params unless object_key
38
-
39
- params = [params] unless params.is_a?(Array)
40
- {object_key => params}
41
- end
42
-
43
- def base_url; end
44
-
45
- def compression?; end
46
-
47
- alias c_r construct_request
48
16
  end
49
17
  end
@@ -1,43 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLokaliseApi
4
+ # Regular API client used to perform requests with a basic API token
4
5
  class Client < BaseClient
5
- include RubyLokaliseApi::Rest::Branches
6
- include RubyLokaliseApi::Rest::Comments
7
- include RubyLokaliseApi::Rest::Contributors
8
- include RubyLokaliseApi::Rest::CustomTranslationStatuses
9
- include RubyLokaliseApi::Rest::Files
10
- include RubyLokaliseApi::Rest::Jwt
11
- include RubyLokaliseApi::Rest::Keys
12
- include RubyLokaliseApi::Rest::Languages
13
- include RubyLokaliseApi::Rest::Orders
14
- include RubyLokaliseApi::Rest::PaymentCards
15
- include RubyLokaliseApi::Rest::Projects
16
- include RubyLokaliseApi::Rest::QueuedProcesses
17
- include RubyLokaliseApi::Rest::Screenshots
18
- include RubyLokaliseApi::Rest::Segments
19
- include RubyLokaliseApi::Rest::Snapshots
20
- include RubyLokaliseApi::Rest::Tasks
21
- include RubyLokaliseApi::Rest::TeamUserBillingDetails
22
- include RubyLokaliseApi::Rest::TeamUserGroups
23
- include RubyLokaliseApi::Rest::TeamUsers
24
- include RubyLokaliseApi::Rest::Teams
25
- include RubyLokaliseApi::Rest::TranslationProviders
26
- include RubyLokaliseApi::Rest::Translations
27
- include RubyLokaliseApi::Rest::Webhooks
6
+ include RubyLokaliseApi::Rest
28
7
 
29
8
  def initialize(token, params = {})
30
9
  super(token, params)
31
10
 
32
11
  @token_header = 'x-api-token'
33
12
  end
34
-
35
- def base_url
36
- 'https://api.lokalise.com/api2/'
37
- end
38
-
39
- def compression?
40
- true
41
- end
42
13
  end
43
14
  end
@@ -2,116 +2,143 @@
2
2
 
3
3
  module RubyLokaliseApi
4
4
  module Collections
5
+ # Base collection. Collection is an array of resources. The actual resources can be found
6
+ # by calling the `.collection` method
5
7
  class Base
6
- using RubyLokaliseApi::Utils::StringUtils
7
-
8
- extend RubyLokaliseApi::Request
9
- extend RubyLokaliseApi::Utils::AttributeHelpers
10
- include RubyLokaliseApi::Utils::AttributeHelpers
11
- extend RubyLokaliseApi::Utils::EndpointHelpers
12
-
13
- attr_reader :total_pages, :total_results, :results_per_page, :current_page, :collection,
14
- :project_id, :team_id, :request_params, :client, :path, :branch, :user_id
15
-
16
- # Initializes a new collection based on the response
17
- #
18
- # @param response [Hash]
19
- # @param params [Hash]
20
- # @return [RubyLokaliseApi::Collections::Base]
21
- def initialize(response, params = {})
22
- produce_collection_for response
23
- populate_pagination_data_for response
24
- @request_params = params
25
- popular_common_attrs response
8
+ include Enumerable
9
+ extend Forwardable
10
+
11
+ using RubyLokaliseApi::Utils::Classes
12
+ extend RubyLokaliseApi::Utils::Attributes
13
+ extend RubyLokaliseApi::Concerns::AttrsLoadable
14
+ include RubyLokaliseApi::Utils::Keys
15
+
16
+ ATTRS_FILENAME = 'collection_attributes.yml'
17
+
18
+ def_delegators :collection, :[], :last, :each
19
+
20
+ attr_reader :total_pages, :total_results, :results_per_page, :current_page,
21
+ :collection
22
+
23
+ def initialize(response)
24
+ @self_endpoint = response.endpoint
25
+
26
+ populate_common_attrs_from response
27
+ produce_collection_from response
26
28
  end
27
29
 
28
- class << self
29
- # Performs a batch query fetching multiple records
30
- def all(client, path, params = {})
31
- new get(path, client, params),
32
- params
33
- end
30
+ # Tries to fetch the next page for paginated collection
31
+ # Returns a new collection or nil if the next page is not available
32
+ def next_page
33
+ return nil if last_page?
34
+
35
+ self.class.new(
36
+ reinit_endpoint(
37
+ override_req_params: { page: current_page + 1 }
38
+ ).do_get
39
+ )
40
+ end
41
+
42
+ # Tries to fetch the previous page for paginated collection
43
+ # Returns a new collection or nil if the previous page is not available
44
+ def prev_page
45
+ return nil if first_page?
46
+
47
+ self.class.new(
48
+ reinit_endpoint(
49
+ override_req_params: { page: current_page - 1 }
50
+ ).do_get
51
+ )
34
52
  end
35
53
 
54
+ # Checks whether the next page is available
36
55
  # @return [Boolean]
37
56
  def next_page?
38
- @current_page.positive? && @current_page < @total_pages
57
+ current_page.positive? && current_page < total_pages
39
58
  end
40
59
 
60
+ # Checks whether the current page is the last one
41
61
  # @return [Boolean]
42
62
  def last_page?
43
63
  !next_page?
44
64
  end
45
65
 
66
+ # Checks whether the previous page is available
46
67
  # @return [Boolean]
47
68
  def prev_page?
48
- @current_page > 1
69
+ current_page > 1
49
70
  end
50
71
 
72
+ # Checks whether the current page is the first one
51
73
  # @return [Boolean]
52
74
  def first_page?
53
75
  !prev_page?
54
76
  end
55
77
 
56
- # @return [Integer]
57
- def next_page
58
- return nil if last_page?
78
+ private
59
79
 
60
- fetch_page @current_page + 1
80
+ # This method is utilized to recreate an endpoint for the current collection
81
+ def reinit_endpoint(req_params: @self_endpoint.req_params, override_req_params: {})
82
+ @self_endpoint.reinitialize(
83
+ req_params: req_params.merge(override_req_params)
84
+ )
61
85
  end
62
86
 
63
- # @return [Integer]
64
- def prev_page
65
- return nil if first_page?
87
+ def populate_common_attrs_from(response)
88
+ supported_attrs.each do |attrib|
89
+ instance_variable_set "@#{attrib}", response.content[attrib]
90
+ end
91
+
92
+ headers = response.headers
66
93
 
67
- fetch_page @current_page - 1
94
+ return unless headers.any?
95
+
96
+ @total_results = headers[:'x-pagination-total-count']
97
+ @total_pages = headers[:'x-pagination-page-count']
98
+ @results_per_page = headers[:'x-pagination-limit']
99
+ @current_page = headers[:'x-pagination-page']
68
100
  end
69
101
 
70
- private
102
+ def produce_collection_from(response)
103
+ content = response.content
104
+ return unless content
105
+
106
+ data_key_plural = collection_key_for klass: self.class.base_name
107
+
108
+ resources_data = content[data_key_plural]
109
+ other_data = content.reject { |key, _| key == data_key_plural }
71
110
 
72
- def populate_pagination_data_for(response)
73
- @total_results = response['x-pagination-total-count'].to_i
74
- @total_pages = response['x-pagination-page-count'].to_i
75
- @results_per_page = response['x-pagination-limit'].to_i
76
- @current_page = response['x-pagination-page'].to_i
111
+ @collection = build_collection resources_data, other_data
77
112
  end
78
113
 
79
- # Gets the specified page
80
- def fetch_page(page_num)
81
- self.class.all @client,
82
- @path,
83
- @request_params.merge(page: page_num)
114
+ def build_collection(resources_data, other_data)
115
+ resources_data.map do |raw_resource|
116
+ self.class.const_get(:RESOURCE).new(resource_data(raw_resource, other_data))
117
+ end
84
118
  end
85
119
 
86
- # Dynamically produces collection of resources based on the given response
87
- # Collection example: `{ "content": {"comments": [ ... ]} }`
88
- def produce_collection_for(response)
89
- return unless response['content']
120
+ def resource_data(raw_resource, other_data)
121
+ RubyLokaliseApi::Response.new(
122
+ raw_resource.merge(other_data),
123
+ resource_endpoint.new(
124
+ @self_endpoint.client,
125
+ query: query_for(raw_resource, other_data)
126
+ )
127
+ )
128
+ end
90
129
 
91
- model_class = self.class.name.base_class_name
92
- data_key_plural = data_key_for model_class: model_class, plural: true, collection: true
130
+ def query_for(raw_resource, other_data)
131
+ main_params = self.class.const_get(:RESOURCE).const_get(:MAIN_PARAMS).to_array
93
132
 
94
- # Fetch collection data and instantiate an individual resource for each object
95
- # We also preserve the `client` to be able to chain API methods later
96
- @collection = response['content'][data_key_plural].map do |raw_model|
97
- Module.const_get("RubyLokaliseApi::Resources::#{model_class}").new 'content' => raw_model,
98
- 'client' => response['client'],
99
- 'base_path' => response['path']
133
+ main_params.map do |param|
134
+ other_data[param.to_s] || raw_resource[param.to_s] || nil
100
135
  end
101
136
  end
102
137
 
103
- def popular_common_attrs(response)
104
- @client = response['client']
105
- @path = response['path']
106
-
107
- return unless response['content']
138
+ def resource_endpoint
139
+ klass = self.class
108
140
 
109
- content = response['content']
110
- # Project, team id, user id, and branch may not be present in some cases
111
- @project_id = content['project_id']
112
- @team_id = content['team_id']
113
- @user_id = content['user_id']
114
- @branch = content['branch']
141
+ klass.const_defined?(:RESOURCES_ENDPOINT) ? klass.const_get(:RESOURCES_ENDPOINT) : klass.const_get(:ENDPOINT)
115
142
  end
116
143
  end
117
144
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Branches < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::BranchesEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Branch
8
+ DATA_KEY = 'branches'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Contributors < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::ContributorsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Contributor
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class CustomTranslationStatuses < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::CustomTranslationStatusesEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::CustomTranslationStatus
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Files < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::FilesEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::File
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class KeyComments < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::KeyCommentsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Comment
8
+ DATA_KEY = 'comments'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Keys < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::KeysEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Key
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Orders < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::OrdersEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Order
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class PaymentCards < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::PaymentCardsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::PaymentCard
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class ProjectComments < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::ProjectCommentsEndpoint
7
+ RESOURCES_ENDPOINT = RubyLokaliseApi::Endpoints::KeyCommentsEndpoint
8
+ RESOURCE = RubyLokaliseApi::Resources::Comment
9
+ DATA_KEY = 'comments'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class ProjectLanguages < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::ProjectLanguagesEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::ProjectLanguage
8
+ DATA_KEY = 'languages'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Projects < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::ProjectsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Project
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class QueuedProcesses < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::QueuedProcessesEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::QueuedProcess
8
+ DATA_KEY = 'processes'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Screenshots < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::ScreenshotsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Screenshot
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Segments < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::SegmentsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Segment
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Snapshots < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::SnapshotsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Snapshot
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class SystemLanguages < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::SystemLanguagesEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::SystemLanguage
8
+ DATA_KEY = 'languages'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Tasks < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::TasksEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Task
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class TeamUserGroups < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::TeamUserGroupsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::TeamUserGroup
8
+ DATA_KEY = 'user_groups'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class TeamUsers < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::TeamUsersEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::TeamUser
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Teams < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::TeamsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Team
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class TranslationProviders < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::TranslationProvidersEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::TranslationProvider
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Translations < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::TranslationsEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Translation
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Collections
5
+ class Webhooks < Base
6
+ ENDPOINT = RubyLokaliseApi::Endpoints::WebhooksEndpoint
7
+ RESOURCE = RubyLokaliseApi::Resources::Webhook
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Concerns
5
+ module AttrsLoadable
6
+ class << self
7
+ def extended(klass)
8
+ klass.class_exec do
9
+ define_method :supported_attrs do
10
+ that_klass = self.class
11
+ that_klass.const_defined?(:ATTRS) ? that_klass.const_get(:ATTRS) : []
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ def inherited(subclass)
18
+ klass_attributes = attributes_for subclass, const_get(:ATTRS_FILENAME)
19
+
20
+ if klass_attributes&.any?
21
+ subclass.class_exec do
22
+ const_set :ATTRS, klass_attributes
23
+ attr_reader(*klass_attributes)
24
+ end
25
+ end
26
+
27
+ super
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Concerns
5
+ # Adds the `[]` method for invariant access
6
+ module HashAccessible
7
+ def [](raw_key_attr)
8
+ key_attr = :"@#{raw_key_attr}"
9
+
10
+ return nil unless instance_variables.include?(key_attr) && respond_to?(raw_key_attr.to_sym)
11
+
12
+ instance_variable_get(key_attr)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module Concerns
5
+ # Allows to undefine certain methods
6
+ module Unsupportable
7
+ def no_support_for(methods)
8
+ return unless methods.any?
9
+
10
+ methods.each do |method|
11
+ undef_method(method)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end