ruby-lokalise-api 9.0.1 → 9.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruby_lokalise_api/client.rb +1 -1
- data/lib/ruby_lokalise_api/collections/base.rb +36 -17
- data/lib/ruby_lokalise_api/collections/permission_templates.rb +11 -0
- data/lib/ruby_lokalise_api/connection.rb +25 -7
- data/lib/ruby_lokalise_api/data/resource_attributes.yml +11 -0
- data/lib/ruby_lokalise_api/endpoints/base_endpoint.rb +3 -3
- data/lib/ruby_lokalise_api/endpoints/main_endpoint.rb +1 -1
- data/lib/ruby_lokalise_api/endpoints/oauth2/oauth2_endpoint.rb +13 -3
- data/lib/ruby_lokalise_api/endpoints/permission_templates_endpoint.rb +15 -0
- data/lib/ruby_lokalise_api/error.rb +1 -1
- data/lib/ruby_lokalise_api/oauth2/auth.rb +29 -28
- data/lib/ruby_lokalise_api/oauth2_client.rb +1 -1
- data/lib/ruby_lokalise_api/request.rb +7 -5
- data/lib/ruby_lokalise_api/resources/base.rb +17 -9
- data/lib/ruby_lokalise_api/resources/permission_template.rb +10 -0
- data/lib/ruby_lokalise_api/response.rb +2 -11
- data/lib/ruby_lokalise_api/rest/permission_templates.rb +21 -0
- data/lib/ruby_lokalise_api/rest.rb +1 -0
- data/lib/ruby_lokalise_api/utils/attributes.rb +10 -6
- data/lib/ruby_lokalise_api/utils/classes.rb +11 -4
- data/lib/ruby_lokalise_api/utils/keys.rb +25 -14
- data/lib/ruby_lokalise_api/utils/loaders.rb +25 -6
- data/lib/ruby_lokalise_api/utils/strings.rb +8 -7
- data/lib/ruby_lokalise_api/version.rb +1 -1
- data/ruby-lokalise-api.gemspec +1 -1
- metadata +9 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69670957b958e42f7a92d8cd6f9b7a56e72a0ee82e83519aacebb247f39cef4d
|
4
|
+
data.tar.gz: def7256b54391d38d2657cb816c8be7183f773617fdd5bfcc03b68d7a4656c89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a887ca5c684958a890ed292b709675cd8f404eaf8c6257356b8bb9af8d0b5075f06b837f396ab19c7bc47d856dc442dcad31d17bc90184c057250ffc8956f81f
|
7
|
+
data.tar.gz: a26be455d40924cc3c9efbb3ca84c4c5f14a19ce88ef8296610a4a2f371d146802dcea1b34e76da114ffe802363cd632d29ebe900eb8b8af4d6fa4ad3113edb0
|
@@ -18,7 +18,7 @@ module RubyLokaliseApi
|
|
18
18
|
def_delegators :collection, :[], :last, :each
|
19
19
|
|
20
20
|
attr_reader :total_pages, :total_results, :results_per_page, :current_page,
|
21
|
-
:collection
|
21
|
+
:collection, :next_cursor
|
22
22
|
|
23
23
|
def initialize(response)
|
24
24
|
@self_endpoint = response.endpoint
|
@@ -32,11 +32,15 @@ module RubyLokaliseApi
|
|
32
32
|
def next_page
|
33
33
|
return nil if last_page?
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
fetch_page(current_page + 1)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Tries to fetch the next cursor for paginated collection
|
39
|
+
# Returns a new collection or nil if the next cursor is not available
|
40
|
+
def load_next_cursor
|
41
|
+
return nil unless next_cursor?
|
42
|
+
|
43
|
+
fetch_cursor(next_cursor)
|
40
44
|
end
|
41
45
|
|
42
46
|
# Tries to fetch the previous page for paginated collection
|
@@ -44,11 +48,7 @@ module RubyLokaliseApi
|
|
44
48
|
def prev_page
|
45
49
|
return nil if first_page?
|
46
50
|
|
47
|
-
|
48
|
-
reinit_endpoint(
|
49
|
-
override_req_params: { page: current_page - 1 }
|
50
|
-
).do_get
|
51
|
-
)
|
51
|
+
fetch_page(current_page - 1)
|
52
52
|
end
|
53
53
|
|
54
54
|
# Checks whether the next page is available
|
@@ -75,6 +75,12 @@ module RubyLokaliseApi
|
|
75
75
|
!prev_page?
|
76
76
|
end
|
77
77
|
|
78
|
+
# Checks whether the next cursor is available
|
79
|
+
# @return [Boolean]
|
80
|
+
def next_cursor?
|
81
|
+
!next_cursor.nil? && next_cursor != ''
|
82
|
+
end
|
83
|
+
|
78
84
|
private
|
79
85
|
|
80
86
|
# This method is utilized to recreate an endpoint for the current collection
|
@@ -89,14 +95,17 @@ module RubyLokaliseApi
|
|
89
95
|
instance_variable_set :"@#{attrib}", response.content[attrib]
|
90
96
|
end
|
91
97
|
|
92
|
-
|
98
|
+
extract_common_from_headers(response.headers)
|
99
|
+
end
|
93
100
|
|
101
|
+
def extract_common_from_headers(headers)
|
94
102
|
return unless headers.any?
|
95
103
|
|
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']
|
104
|
+
@total_results = headers[:'x-pagination-total-count'].to_i
|
105
|
+
@total_pages = headers[:'x-pagination-page-count'].to_i
|
106
|
+
@results_per_page = headers[:'x-pagination-limit'].to_i
|
107
|
+
@current_page = headers[:'x-pagination-page'].to_i
|
108
|
+
@next_cursor = headers[:'x-pagination-next-cursor']
|
100
109
|
end
|
101
110
|
|
102
111
|
def produce_collection_from(response)
|
@@ -106,7 +115,7 @@ module RubyLokaliseApi
|
|
106
115
|
data_key_plural = collection_key_for klass: self.class.base_name
|
107
116
|
|
108
117
|
resources_data = content[data_key_plural]
|
109
|
-
other_data = content.
|
118
|
+
other_data = content.except(data_key_plural)
|
110
119
|
|
111
120
|
@collection = build_collection resources_data, other_data
|
112
121
|
end
|
@@ -140,6 +149,16 @@ module RubyLokaliseApi
|
|
140
149
|
|
141
150
|
klass.const_defined?(:RESOURCES_ENDPOINT) ? klass.const_get(:RESOURCES_ENDPOINT) : klass.const_get(:ENDPOINT)
|
142
151
|
end
|
152
|
+
|
153
|
+
# Helper method to fetch a page for paginated collections
|
154
|
+
def fetch_page(page)
|
155
|
+
self.class.new(reinit_endpoint(override_req_params: { page: page }).do_get)
|
156
|
+
end
|
157
|
+
|
158
|
+
# Helper method to fetch the next cursor
|
159
|
+
def fetch_cursor(cursor)
|
160
|
+
self.class.new(reinit_endpoint(override_req_params: { cursor: cursor }).do_get)
|
161
|
+
end
|
143
162
|
end
|
144
163
|
end
|
145
164
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RubyLokaliseApi
|
4
|
+
module Collections
|
5
|
+
class PermissionTemplates < Base
|
6
|
+
ENDPOINT = RubyLokaliseApi::Endpoints::PermissionTemplatesEndpoint
|
7
|
+
RESOURCE = RubyLokaliseApi::Resources::PermissionTemplate
|
8
|
+
DATA_KEY = 'roles'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -4,10 +4,14 @@ module RubyLokaliseApi
|
|
4
4
|
# Module to setup connection using Faraday
|
5
5
|
module Connection
|
6
6
|
# Creates a new Faraday object with specified params
|
7
|
+
#
|
8
|
+
# @param endpoint [Object] the API endpoint
|
9
|
+
# @param params [Hash] additional connection parameters
|
10
|
+
# @return [Faraday::Connection] the Faraday connection object
|
7
11
|
def connection(endpoint, params = {})
|
8
12
|
Faraday.new(options(endpoint, params), request_params_for(endpoint.client)) do |faraday|
|
9
13
|
faraday.adapter Faraday.default_adapter
|
10
|
-
faraday.request
|
14
|
+
faraday.request :gzip
|
11
15
|
end
|
12
16
|
end
|
13
17
|
|
@@ -17,19 +21,33 @@ module RubyLokaliseApi
|
|
17
21
|
req_params = __base_options(endpoint)
|
18
22
|
client = endpoint.client
|
19
23
|
|
20
|
-
|
21
|
-
req_params[:headers][client.token_header] = client.token
|
22
|
-
end
|
24
|
+
add_token_header(req_params, client) if client_responds_to_token?(client)
|
23
25
|
|
24
|
-
#
|
25
|
-
|
26
|
-
req_params[:headers]['Content-type'] = 'application/json' if !params[:get_request] && endpoint.req_params
|
26
|
+
# Set Content-Type if required (skip for GET requests)
|
27
|
+
add_content_type_header(req_params, params, endpoint)
|
27
28
|
|
28
29
|
req_params[:headers][:accept_encoding] = 'gzip,deflate,br'
|
29
30
|
|
30
31
|
req_params
|
31
32
|
end
|
32
33
|
|
34
|
+
# Adds the token header to the request parameters if token is present
|
35
|
+
def add_token_header(req_params, client)
|
36
|
+
req_params[:headers][client.token_header] = client.token
|
37
|
+
end
|
38
|
+
|
39
|
+
# Checks if the client can respond to token and token header methods
|
40
|
+
def client_responds_to_token?(client)
|
41
|
+
client.respond_to?(:token) && client.respond_to?(:token_header)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Conditionally adds the Content-Type header
|
45
|
+
def add_content_type_header(req_params, params, endpoint)
|
46
|
+
return unless !params[:get_request] && endpoint.req_params
|
47
|
+
|
48
|
+
req_params[:headers]['Content-Type'] = 'application/json'
|
49
|
+
end
|
50
|
+
|
33
51
|
def __base_options(endpoint)
|
34
52
|
{
|
35
53
|
headers: {
|
@@ -28,6 +28,7 @@ contributor:
|
|
28
28
|
- is_reviewer
|
29
29
|
- languages
|
30
30
|
- admin_rights
|
31
|
+
- role_id
|
31
32
|
custom_translation_status:
|
32
33
|
- project_id
|
33
34
|
- branch
|
@@ -142,6 +143,15 @@ queued_process:
|
|
142
143
|
- created_at
|
143
144
|
- created_at_timestamp
|
144
145
|
- details
|
146
|
+
permission_template:
|
147
|
+
- id
|
148
|
+
- role
|
149
|
+
- permissions
|
150
|
+
- description
|
151
|
+
- tag
|
152
|
+
- tagColor
|
153
|
+
- tagInfo
|
154
|
+
- doesEnableAllReadOnlyLanguages
|
145
155
|
screenshot:
|
146
156
|
- project_id
|
147
157
|
- branch
|
@@ -256,6 +266,7 @@ team_user_group:
|
|
256
266
|
- team_id
|
257
267
|
- projects
|
258
268
|
- members
|
269
|
+
- role_id
|
259
270
|
translation:
|
260
271
|
- project_id
|
261
272
|
- branch
|
@@ -28,11 +28,11 @@ module RubyLokaliseApi
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def base_url
|
31
|
-
self.class
|
31
|
+
self.class::BASE_URL
|
32
32
|
end
|
33
33
|
|
34
34
|
def full_uri
|
35
|
-
base_url + uri
|
35
|
+
base_url + uri.to_s
|
36
36
|
end
|
37
37
|
|
38
38
|
# Creates methods like `do_post`, `do_get` that proxy calls to the
|
@@ -52,7 +52,7 @@ module RubyLokaliseApi
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def partial_uri_template
|
55
|
-
self.class
|
55
|
+
self.class::PARTIAL_URI_TEMPLATE
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
@@ -8,22 +8,32 @@ module RubyLokaliseApi
|
|
8
8
|
PARTIAL_URI_TEMPLATE = '{/segments*}{?query*}'
|
9
9
|
|
10
10
|
def initialize(client, params = {})
|
11
|
-
super
|
11
|
+
super
|
12
12
|
|
13
|
-
@uri =
|
13
|
+
@uri = build_uri(params.fetch(:get, []))
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
17
17
|
|
18
|
+
# Builds the complete URI for the OAuth2 endpoint
|
19
|
+
def build_uri(query)
|
20
|
+
partial_uri(base_query(*@query_params), query)
|
21
|
+
end
|
22
|
+
|
18
23
|
def partial_uri(segments, query)
|
19
24
|
template = super
|
20
25
|
|
21
26
|
template.expand(
|
22
27
|
segments: segments.to_a.flatten,
|
23
|
-
query: query
|
28
|
+
query: filter_query_params(query)
|
24
29
|
).to_s
|
25
30
|
end
|
26
31
|
|
32
|
+
# Filters out nil values from query parameters
|
33
|
+
def filter_query_params(query)
|
34
|
+
query.compact
|
35
|
+
end
|
36
|
+
|
27
37
|
def base_query(segment = nil)
|
28
38
|
[segment]
|
29
39
|
end
|
@@ -29,7 +29,7 @@ module RubyLokaliseApi
|
|
29
29
|
def auth(scope:, redirect_uri: nil, state: nil)
|
30
30
|
get_params = {
|
31
31
|
client_id: client_id,
|
32
|
-
scope: (scope
|
32
|
+
scope: scope_to_string(scope),
|
33
33
|
state: state,
|
34
34
|
redirect_uri: redirect_uri
|
35
35
|
}
|
@@ -37,41 +37,42 @@ module RubyLokaliseApi
|
|
37
37
|
oauth2_endpoint.new(self, query: 'auth', get: get_params).full_uri
|
38
38
|
end
|
39
39
|
|
40
|
-
# Requests OAuth2 access token
|
41
|
-
#
|
42
|
-
# @return [RubyLokaliseApi::Resources::OAuth2Token]
|
43
|
-
# @param code [String]
|
40
|
+
# Requests an OAuth2 access token using a code
|
41
|
+
# @param code [String] The authorization code
|
42
|
+
# @return [RubyLokaliseApi::Resources::OAuth2Token] The OAuth2 token resource
|
44
43
|
def token(code)
|
45
|
-
|
46
|
-
self,
|
47
|
-
query: :token,
|
48
|
-
req: common_params.merge(
|
49
|
-
grant_type: 'authorization_code',
|
50
|
-
code: code
|
51
|
-
)
|
52
|
-
)
|
53
|
-
|
54
|
-
RubyLokaliseApi::Resources::OAuth2Token.new endpoint.do_post
|
44
|
+
request_token(grant_type: 'authorization_code', code: code)
|
55
45
|
end
|
56
46
|
|
57
|
-
# Refreshes expired OAuth2
|
58
|
-
# @
|
59
|
-
# @
|
47
|
+
# Refreshes an expired OAuth2 token
|
48
|
+
# @param refresh_token [String] The refresh token
|
49
|
+
# @return [RubyLokaliseApi::Resources::OAuth2RefreshedToken] The refreshed token resource
|
60
50
|
def refresh(refresh_token)
|
61
|
-
|
62
|
-
self,
|
63
|
-
query: :token,
|
64
|
-
req: common_params.merge(
|
65
|
-
grant_type: 'refresh_token',
|
66
|
-
refresh_token: refresh_token
|
67
|
-
)
|
68
|
-
)
|
69
|
-
|
70
|
-
RubyLokaliseApi::Resources::OAuth2RefreshedToken.new endpoint.do_post
|
51
|
+
request_token(grant_type: 'refresh_token', refresh_token: refresh_token)
|
71
52
|
end
|
72
53
|
|
73
54
|
private
|
74
55
|
|
56
|
+
# Generalized method for requesting a token
|
57
|
+
# @param params [Hash] Request parameters including grant type
|
58
|
+
# @return [RubyLokaliseApi::Resources::OAuth2Token, RubyLokaliseApi::Resources::OAuth2RefreshedToken]
|
59
|
+
def request_token(params)
|
60
|
+
endpoint = oauth2_endpoint.new(self, query: :token, req: common_params.merge(params))
|
61
|
+
resource_class = if params[:grant_type] == 'authorization_code'
|
62
|
+
RubyLokaliseApi::Resources::OAuth2Token
|
63
|
+
else
|
64
|
+
RubyLokaliseApi::Resources::OAuth2RefreshedToken
|
65
|
+
end
|
66
|
+
resource_class.new(endpoint.do_post)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Converts scope to a space-separated string if it's an array
|
70
|
+
# @param scope [Array, String] The scope or scopes
|
71
|
+
# @return [String] The scope as a string
|
72
|
+
def scope_to_string(scope)
|
73
|
+
scope.is_a?(Array) ? scope.join(' ') : scope
|
74
|
+
end
|
75
|
+
|
75
76
|
def common_params
|
76
77
|
{
|
77
78
|
client_id: @client_id,
|
@@ -63,17 +63,19 @@ module RubyLokaliseApi
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def respond_with(response, endpoint)
|
66
|
-
|
67
|
-
body = custom_load response.body
|
68
|
-
rescue JSON::ParserError
|
69
|
-
respond_with_error(response.status, response.body)
|
70
|
-
end
|
66
|
+
body = parse_response_body(response)
|
71
67
|
|
72
68
|
raise_on_error! response, body
|
73
69
|
|
74
70
|
RubyLokaliseApi::Response.new(body, endpoint, response.headers)
|
75
71
|
end
|
76
72
|
|
73
|
+
def parse_response_body(response)
|
74
|
+
custom_load(response.body)
|
75
|
+
rescue JSON::ParserError
|
76
|
+
respond_with_error(response.status, response.body)
|
77
|
+
end
|
78
|
+
|
77
79
|
def respond_with_error(code, body)
|
78
80
|
raise(RubyLokaliseApi::Error, body['error'] || body) unless RubyLokaliseApi::Error::ERRORS.key? code
|
79
81
|
|
@@ -57,20 +57,28 @@ module RubyLokaliseApi
|
|
57
57
|
@self_endpoint.reinitialize(query_params: read_main_params, req_params: req_params)
|
58
58
|
end
|
59
59
|
|
60
|
+
# Populates attributes from the response content
|
60
61
|
def populate_attrs_from(content)
|
61
62
|
return unless content
|
62
63
|
|
63
|
-
data_key = data_key_for
|
64
|
+
data_key = data_key_for(klass: self.class.base_name)
|
65
|
+
supported_attrs.each { |attrib| set_instance_variable(attrib, content, data_key) }
|
66
|
+
end
|
64
67
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
68
|
+
# Sets the instance variable for a given attribute
|
69
|
+
def set_instance_variable(attrib, content, data_key)
|
70
|
+
value = if content_key_exists?(content, data_key, attrib)
|
71
|
+
content[data_key][attrib]
|
72
|
+
else
|
73
|
+
content[attrib]
|
74
|
+
end
|
71
75
|
|
72
|
-
|
73
|
-
|
76
|
+
instance_variable_set(:"@#{attrib}", value)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Checks if the content contains the specified key
|
80
|
+
def content_key_exists?(content, data_key, attrib)
|
81
|
+
content.key?(data_key) && content[data_key].is_a?(Hash) && content[data_key].key?(attrib)
|
74
82
|
end
|
75
83
|
end
|
76
84
|
end
|
@@ -5,7 +5,7 @@ module RubyLokaliseApi
|
|
5
5
|
class Response
|
6
6
|
# Lokalise returns pagination info in special headers
|
7
7
|
PAGINATION_HEADERS = %w[x-pagination-total-count x-pagination-page-count x-pagination-limit
|
8
|
-
x-pagination-page].freeze
|
8
|
+
x-pagination-page x-pagination-next-cursor].freeze
|
9
9
|
|
10
10
|
attr_reader :content, :endpoint, :headers
|
11
11
|
|
@@ -31,20 +31,11 @@ module RubyLokaliseApi
|
|
31
31
|
@endpoint = new_endpoint
|
32
32
|
end
|
33
33
|
|
34
|
-
# Returns an array of pagination headers
|
35
|
-
def pagination_headers
|
36
|
-
self.class.const_get(:PAGINATION_HEADERS)
|
37
|
-
end
|
38
|
-
|
39
34
|
private
|
40
35
|
|
41
36
|
# Keep only pagination headers
|
42
37
|
def extract_headers_from(raw_headers)
|
43
|
-
raw_headers.
|
44
|
-
to_h.
|
45
|
-
keep_if { |header, _value| pagination_headers.include?(header) }.
|
46
|
-
transform_keys(&:to_sym).
|
47
|
-
transform_values(&:to_i)
|
38
|
+
raw_headers.to_h.slice(*PAGINATION_HEADERS).transform_keys(&:to_sym)
|
48
39
|
end
|
49
40
|
end
|
50
41
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RubyLokaliseApi
|
4
|
+
module Rest
|
5
|
+
module PermissionTemplates
|
6
|
+
# Returns permission tempates for a team
|
7
|
+
#
|
8
|
+
# @see https://developers.lokalise.com/reference/list-all-permission-templates
|
9
|
+
# @return [RubyLokaliseApi::Collections::PermissionTemplates]
|
10
|
+
# @param team_id [Integer, String]
|
11
|
+
def permission_templates(team_id)
|
12
|
+
name = 'PermissionTemplates'
|
13
|
+
params = { query: team_id }
|
14
|
+
|
15
|
+
data = endpoint(name: name, params: params).do_get
|
16
|
+
|
17
|
+
collection name, data
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -14,11 +14,11 @@ module RubyLokaliseApi
|
|
14
14
|
#
|
15
15
|
# @return [Array<String>]
|
16
16
|
def attributes_for(klass, filename)
|
17
|
-
@attributes ||=
|
17
|
+
@attributes ||= load_attributes(filename)
|
18
18
|
|
19
19
|
name = unify klass.base_name.snakecase
|
20
20
|
|
21
|
-
@attributes
|
21
|
+
@attributes[name] || @attributes["#{name}s"]
|
22
22
|
end
|
23
23
|
|
24
24
|
# Unify some resources' names (eg, `ProjectComment` and `KeyComment` have the same
|
@@ -26,11 +26,15 @@ module RubyLokaliseApi
|
|
26
26
|
#
|
27
27
|
# @return [String]
|
28
28
|
def unify(name)
|
29
|
-
UNIFIED_RESOURCES.
|
30
|
-
|
31
|
-
end
|
29
|
+
UNIFIED_RESOURCES.find { |u_a| name.match?(/#{u_a}/) } || name
|
30
|
+
end
|
32
31
|
|
33
|
-
|
32
|
+
# Loads attributes from a YAML file
|
33
|
+
#
|
34
|
+
# @param filename [String] The filename to load attributes from
|
35
|
+
# @return [Hash] The loaded attributes hash
|
36
|
+
def load_attributes(filename)
|
37
|
+
YAML.load_file(File.expand_path("../data/#{filename}", __dir__)).freeze
|
34
38
|
end
|
35
39
|
end
|
36
40
|
end
|
@@ -5,18 +5,25 @@ module RubyLokaliseApi
|
|
5
5
|
module Utils
|
6
6
|
module Classes
|
7
7
|
refine Object do
|
8
|
-
#
|
8
|
+
# Extracts the base name of a class, removing any module nesting.
|
9
|
+
#
|
10
|
+
# @return [String] The base class name
|
9
11
|
def base_name
|
10
12
|
name.split('::').last
|
11
13
|
end
|
12
14
|
|
13
|
-
# Converts object to array unless it is already an array
|
15
|
+
# Converts the object to an array, unless it is already an array.
|
16
|
+
#
|
17
|
+
# @return [Array] The object wrapped in an array if not already an array
|
14
18
|
def to_array
|
15
19
|
is_a?(Array) ? self : [self]
|
16
20
|
end
|
17
21
|
|
18
|
-
# Converts object to array
|
19
|
-
#
|
22
|
+
# Converts the object to an array, then places this array inside a hash
|
23
|
+
# under the provided key.
|
24
|
+
#
|
25
|
+
# @param key [Symbol, String] The key under which to place the array
|
26
|
+
# @return [Hash] The hash with the array under the provided key
|
20
27
|
def to_array_obj(key)
|
21
28
|
return self if is_a?(Hash) && (key?(key) || key?(key.to_s))
|
22
29
|
|
@@ -5,25 +5,36 @@ module RubyLokaliseApi
|
|
5
5
|
module Keys
|
6
6
|
using RubyLokaliseApi::Utils::Strings
|
7
7
|
|
8
|
-
#
|
9
|
-
# in the API response
|
8
|
+
# Retrieves the DATA_KEY for resources.
|
9
|
+
# DATA_KEY specifies the key in the API response containing the actual data.
|
10
|
+
#
|
11
|
+
# @param klass [Class] The class of the resource
|
12
|
+
# @return [String] The snake-cased DATA_KEY
|
10
13
|
def data_key_for(klass:)
|
11
|
-
|
12
|
-
Module.const_get "RubyLokaliseApi::Resources::#{klass}::DATA_KEY"
|
13
|
-
else
|
14
|
-
klass
|
15
|
-
end
|
16
|
-
|
17
|
-
key.snakecase
|
14
|
+
retrieve_data_key("RubyLokaliseApi::Resources::#{klass}::DATA_KEY", klass)
|
18
15
|
end
|
19
16
|
|
20
|
-
#
|
21
|
-
# in the API response
|
17
|
+
# Retrieves the DATA_KEY for collections.
|
18
|
+
# DATA_KEY specifies the key in the API response containing the actual data.
|
19
|
+
#
|
20
|
+
# @param klass [Class] The class of the collection
|
21
|
+
# @return [String] The snake-cased DATA_KEY
|
22
22
|
def collection_key_for(klass:)
|
23
|
-
|
24
|
-
|
23
|
+
retrieve_data_key("RubyLokaliseApi::Collections::#{klass}::DATA_KEY", klass)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# Helper method to retrieve the DATA_KEY for a given class.
|
29
|
+
#
|
30
|
+
# @param constant_path [String] The constant path to check
|
31
|
+
# @param fallback [Class] The fallback class if the constant isn't defined
|
32
|
+
# @return [String] The snake-cased key
|
33
|
+
def retrieve_data_key(constant_path, fallback)
|
34
|
+
key = if Module.const_defined?(constant_path)
|
35
|
+
Module.const_get(constant_path)
|
25
36
|
else
|
26
|
-
|
37
|
+
fallback
|
27
38
|
end
|
28
39
|
|
29
40
|
key.snakecase
|
@@ -6,21 +6,40 @@ module RubyLokaliseApi
|
|
6
6
|
private
|
7
7
|
|
8
8
|
# Instantiates an endpoint
|
9
|
+
#
|
10
|
+
# @param name [String] The name of the endpoint
|
11
|
+
# @param client [Object] The client to associate with the endpoint (default: self)
|
12
|
+
# @param params [Hash] Additional parameters for the endpoint
|
13
|
+
# @return [Object] The instantiated endpoint
|
9
14
|
def endpoint(name:, client: self, params: {})
|
10
|
-
|
11
|
-
klass.new client, params
|
15
|
+
instantiate("Endpoints::#{name}Endpoint", client, params)
|
12
16
|
end
|
13
17
|
|
14
18
|
# Instantiates a resource
|
19
|
+
#
|
20
|
+
# @param name [String] The name of the resource
|
21
|
+
# @param data [Hash] The data to initialize the resource with
|
22
|
+
# @return [Object] The instantiated resource
|
15
23
|
def resource(name, data)
|
16
|
-
|
17
|
-
klass.new data
|
24
|
+
instantiate("Resources::#{name}", data)
|
18
25
|
end
|
19
26
|
|
20
27
|
# Instantiates a collection
|
28
|
+
#
|
29
|
+
# @param name [String] The name of the collection
|
30
|
+
# @param data [Hash] The data to initialize the collection with
|
31
|
+
# @return [Object] The instantiated collection
|
21
32
|
def collection(name, data)
|
22
|
-
|
23
|
-
|
33
|
+
instantiate("Collections::#{name}", data)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Helper method to instantiate a class by constant name
|
37
|
+
#
|
38
|
+
# @param constant_path [String] The constant path for the class
|
39
|
+
# @param args [Array] Arguments to initialize the class with
|
40
|
+
# @return [Object] The instantiated class
|
41
|
+
def instantiate(constant_path, *args)
|
42
|
+
RubyLokaliseApi.const_get(constant_path).new(*args)
|
24
43
|
end
|
25
44
|
end
|
26
45
|
end
|
@@ -4,15 +4,16 @@ module RubyLokaliseApi
|
|
4
4
|
module Utils
|
5
5
|
module Strings
|
6
6
|
refine String do
|
7
|
-
#
|
7
|
+
# Converts a string to snake_case format.
|
8
|
+
# Original code inspired by the Facets gem by Rubyworks:
|
8
9
|
# https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
|
9
10
|
def snakecase
|
10
|
-
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
11
|
-
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
12
|
-
tr('-', '_').
|
13
|
-
gsub(/\s/, '_').
|
14
|
-
gsub(/__+/, '_').
|
15
|
-
downcase
|
11
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). # Handle acronyms like 'HTMLParser'
|
12
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2'). # Handle camelCase to snake_case
|
13
|
+
tr('-', '_'). # Replace dashes with underscores
|
14
|
+
gsub(/\s/, '_'). # Replace spaces with underscores
|
15
|
+
gsub(/__+/, '_'). # Collapse multiple underscores
|
16
|
+
downcase # Convert everything to lowercase
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
data/ruby-lokalise-api.gemspec
CHANGED
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
|
|
34
34
|
spec.add_development_dependency 'rubocop', '~> 1.6'
|
35
35
|
spec.add_development_dependency 'rubocop-performance', '~> 1.5'
|
36
36
|
spec.add_development_dependency 'rubocop-rake', '~> 0.6'
|
37
|
-
spec.add_development_dependency 'rubocop-rspec', '~>
|
37
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 3.0'
|
38
38
|
spec.add_development_dependency 'simplecov', '~> 0.21'
|
39
39
|
spec.add_development_dependency 'simplecov-lcov', '~> 0.8'
|
40
40
|
spec.add_development_dependency 'webmock', '~> 3.14'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-lokalise-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 9.0
|
4
|
+
version: 9.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Krukowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -184,14 +184,14 @@ dependencies:
|
|
184
184
|
requirements:
|
185
185
|
- - "~>"
|
186
186
|
- !ruby/object:Gem::Version
|
187
|
-
version: '
|
187
|
+
version: '3.0'
|
188
188
|
type: :development
|
189
189
|
prerelease: false
|
190
190
|
version_requirements: !ruby/object:Gem::Requirement
|
191
191
|
requirements:
|
192
192
|
- - "~>"
|
193
193
|
- !ruby/object:Gem::Version
|
194
|
-
version: '
|
194
|
+
version: '3.0'
|
195
195
|
- !ruby/object:Gem::Dependency
|
196
196
|
name: simplecov
|
197
197
|
requirement: !ruby/object:Gem::Requirement
|
@@ -263,6 +263,7 @@ files:
|
|
263
263
|
- lib/ruby_lokalise_api/collections/keys.rb
|
264
264
|
- lib/ruby_lokalise_api/collections/orders.rb
|
265
265
|
- lib/ruby_lokalise_api/collections/payment_cards.rb
|
266
|
+
- lib/ruby_lokalise_api/collections/permission_templates.rb
|
266
267
|
- lib/ruby_lokalise_api/collections/project_comments.rb
|
267
268
|
- lib/ruby_lokalise_api/collections/project_languages.rb
|
268
269
|
- lib/ruby_lokalise_api/collections/projects.rb
|
@@ -296,6 +297,7 @@ files:
|
|
296
297
|
- lib/ruby_lokalise_api/endpoints/oauth2/oauth2_endpoint.rb
|
297
298
|
- lib/ruby_lokalise_api/endpoints/orders_endpoint.rb
|
298
299
|
- lib/ruby_lokalise_api/endpoints/payment_cards_endpoint.rb
|
300
|
+
- lib/ruby_lokalise_api/endpoints/permission_templates_endpoint.rb
|
299
301
|
- lib/ruby_lokalise_api/endpoints/project_comments_endpoint.rb
|
300
302
|
- lib/ruby_lokalise_api/endpoints/project_languages_endpoint.rb
|
301
303
|
- lib/ruby_lokalise_api/endpoints/projects_endpoint.rb
|
@@ -330,6 +332,7 @@ files:
|
|
330
332
|
- lib/ruby_lokalise_api/resources/oauth2_token.rb
|
331
333
|
- lib/ruby_lokalise_api/resources/order.rb
|
332
334
|
- lib/ruby_lokalise_api/resources/payment_card.rb
|
335
|
+
- lib/ruby_lokalise_api/resources/permission_template.rb
|
333
336
|
- lib/ruby_lokalise_api/resources/project.rb
|
334
337
|
- lib/ruby_lokalise_api/resources/project_language.rb
|
335
338
|
- lib/ruby_lokalise_api/resources/queued_process.rb
|
@@ -357,6 +360,7 @@ files:
|
|
357
360
|
- lib/ruby_lokalise_api/rest/languages.rb
|
358
361
|
- lib/ruby_lokalise_api/rest/orders.rb
|
359
362
|
- lib/ruby_lokalise_api/rest/payment_cards.rb
|
363
|
+
- lib/ruby_lokalise_api/rest/permission_templates.rb
|
360
364
|
- lib/ruby_lokalise_api/rest/projects.rb
|
361
365
|
- lib/ruby_lokalise_api/rest/queued_processes.rb
|
362
366
|
- lib/ruby_lokalise_api/rest/screenshots.rb
|
@@ -397,7 +401,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
397
401
|
- !ruby/object:Gem::Version
|
398
402
|
version: '0'
|
399
403
|
requirements: []
|
400
|
-
rubygems_version: 3.5.
|
404
|
+
rubygems_version: 3.5.21
|
401
405
|
signing_key:
|
402
406
|
specification_version: 4
|
403
407
|
summary: Ruby interface to the Lokalise API
|