Skip to content

Commit 4681ad5

Browse files
committed
refactors
1 parent 877aab1 commit 4681ad5

File tree

17 files changed

+224
-155
lines changed

17 files changed

+224
-155
lines changed

docs/Gemfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ source 'https://rubygems.org'
1010
#
1111
# This will help ensure the proper Jekyll version is running.
1212
# Happy Jekylling!
13-
gem 'jekyll', '~> 3.9.0'
13+
gem 'jekyll', '~> 3.10.0'
1414

1515
# This is the default theme for new Jekyll sites. You may change this to anything you like.
1616
gem 'minima', '~> 2.0'
1717

1818
# If you want to use GitHub Pages, remove the "gem "jekyll"" above and
1919
# uncomment the line below. To upgrade, run `bundle update github-pages`.
20-
gem 'github-pages', '~> 231', group: :jekyll_plugins
20+
gem 'github-pages', '~> 232', group: :jekyll_plugins
2121

2222
# If you have any plugins, put them here!
2323
group :jekyll_plugins do
@@ -32,7 +32,7 @@ install_if -> { RUBY_PLATFORM =~ /mingw|mswin|java/ } do
3232
end
3333

3434
# Performance-booster for watching directories on Windows
35-
gem 'wdm', '~> 0.1.0', install_if: Gem.win_platform?
35+
gem 'wdm', '~> 0.2.0', install_if: Gem.win_platform?
3636

3737
# kramdown v2 ships without the gfm parser by default. If you're using
3838
# kramdown v1, comment out this line.

lib/ruby_lokalise_api/collections/base.rb

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,23 @@ def initialize(response)
3232
def next_page
3333
return nil if last_page?
3434

35-
override_params = { page: current_page + 1 }
36-
37-
self.class.new(
38-
reinit_endpoint(
39-
override_req_params: override_params
40-
).do_get
41-
)
35+
fetch_page(current_page + 1)
4236
end
4337

4438
# Tries to fetch the next cursor for paginated collection
4539
# Returns a new collection or nil if the next cursor is not available
4640
def load_next_cursor
4741
return nil unless next_cursor?
4842

49-
override_params = { cursor: next_cursor }
50-
51-
self.class.new(
52-
reinit_endpoint(
53-
override_req_params: override_params
54-
).do_get
55-
)
43+
fetch_cursor(next_cursor)
5644
end
5745

5846
# Tries to fetch the previous page for paginated collection
5947
# Returns a new collection or nil if the previous page is not available
6048
def prev_page
6149
return nil if first_page?
6250

63-
self.class.new(
64-
reinit_endpoint(
65-
override_req_params: { page: current_page - 1 }
66-
).do_get
67-
)
51+
fetch_page(current_page - 1)
6852
end
6953

7054
# Checks whether the next page is available
@@ -131,7 +115,7 @@ def produce_collection_from(response)
131115
data_key_plural = collection_key_for klass: self.class.base_name
132116

133117
resources_data = content[data_key_plural]
134-
other_data = content.reject { |key, _| key == data_key_plural }
118+
other_data = content.except(data_key_plural)
135119

136120
@collection = build_collection resources_data, other_data
137121
end
@@ -165,6 +149,16 @@ def resource_endpoint
165149

166150
klass.const_defined?(:RESOURCES_ENDPOINT) ? klass.const_get(:RESOURCES_ENDPOINT) : klass.const_get(:ENDPOINT)
167151
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
168162
end
169163
end
170164
end

lib/ruby_lokalise_api/connection.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ module RubyLokaliseApi
44
# Module to setup connection using Faraday
55
module Connection
66
# 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
711
def connection(endpoint, params = {})
812
Faraday.new(options(endpoint, params), request_params_for(endpoint.client)) do |faraday|
913
faraday.adapter Faraday.default_adapter
10-
faraday.request(:gzip)
14+
faraday.request :gzip
1115
end
1216
end
1317

@@ -17,19 +21,33 @@ def options(endpoint, params)
1721
req_params = __base_options(endpoint)
1822
client = endpoint.client
1923

20-
if client.respond_to?(:token) && client.respond_to?(:token_header)
21-
req_params[:headers][client.token_header] = client.token
22-
end
24+
add_token_header(req_params, client) if client_responds_to_token?(client)
2325

24-
# Sending content-type is needed only when the body is actually present
25-
# Trying to send this header in other cases seems to result in error 500
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)
2728

2829
req_params[:headers][:accept_encoding] = 'gzip,deflate,br'
2930

3031
req_params
3132
end
3233

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+
3351
def __base_options(endpoint)
3452
{
3553
headers: {

lib/ruby_lokalise_api/endpoints/base_endpoint.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def reinitialize(query_params: nil, req_params: {})
2828
end
2929

3030
def base_url
31-
self.class.const_get(:BASE_URL)
31+
self.class::BASE_URL
3232
end
3333

3434
def full_uri
35-
base_url + uri
35+
base_url + uri.to_s
3636
end
3737

3838
# Creates methods like `do_post`, `do_get` that proxy calls to the
@@ -52,7 +52,7 @@ def partial_uri(*_args)
5252
end
5353

5454
def partial_uri_template
55-
self.class.const_get(:PARTIAL_URI_TEMPLATE)
55+
self.class::PARTIAL_URI_TEMPLATE
5656
end
5757
end
5858
end

lib/ruby_lokalise_api/endpoints/oauth2/oauth2_endpoint.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,30 @@ class OAuth2Endpoint < BaseEndpoint
1010
def initialize(client, params = {})
1111
super
1212

13-
@uri = partial_uri(base_query(*@query_params), params.fetch(:get, []))
13+
@uri = build_uri(params.fetch(:get, []))
1414
end
1515

1616
private
1717

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+
1823
def partial_uri(segments, query)
1924
template = super
2025

2126
template.expand(
2227
segments: segments.to_a.flatten,
23-
query: query.filter { |_k, v| !v.nil? }
28+
query: filter_query_params(query)
2429
).to_s
2530
end
2631

32+
# Filters out nil values from query parameters
33+
def filter_query_params(query)
34+
query.compact
35+
end
36+
2737
def base_query(segment = nil)
2838
[segment]
2939
end

lib/ruby_lokalise_api/oauth2/auth.rb

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,50 @@ def oauth2_endpoint
2929
def auth(scope:, redirect_uri: nil, state: nil)
3030
get_params = {
3131
client_id: client_id,
32-
scope: (scope.is_a?(Array) ? scope.join(' ') : scope),
32+
scope: scope_to_string(scope),
3333
state: state,
3434
redirect_uri: redirect_uri
3535
}
3636

3737
oauth2_endpoint.new(self, query: 'auth', get: get_params).full_uri
3838
end
3939

40-
# Requests OAuth2 access token. Requires OAuth2 code obtained
41-
# using the `.auth` method
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
4443
def token(code)
45-
endpoint = oauth2_endpoint.new(
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)
5545
end
5646

57-
# Refreshes expired OAuth2 access token.
58-
# @return [RubyLokaliseApi::Resources::OAuth2RefreshedToken]
59-
# @param refresh_token [String]
47+
# Refreshes an expired OAuth2 token
48+
# @param refresh_token [String] The refresh token
49+
# @return [RubyLokaliseApi::Resources::OAuth2RefreshedToken] The refreshed token resource
6050
def refresh(refresh_token)
61-
endpoint = oauth2_endpoint.new(
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)
7152
end
7253

7354
private
7455

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+
7576
def common_params
7677
{
7778
client_id: @client_id,

lib/ruby_lokalise_api/request.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ def raise_on_error!(response, body)
6363
end
6464

6565
def respond_with(response, endpoint)
66-
begin
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)
7167

7268
raise_on_error! response, body
7369

7470
RubyLokaliseApi::Response.new(body, endpoint, response.headers)
7571
end
7672

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+
7779
def respond_with_error(code, body)
7880
raise(RubyLokaliseApi::Error, body['error'] || body) unless RubyLokaliseApi::Error::ERRORS.key? code
7981

lib/ruby_lokalise_api/resources/base.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,28 @@ def reinit_endpoint(req_params = {})
5757
@self_endpoint.reinitialize(query_params: read_main_params, req_params: req_params)
5858
end
5959

60+
# Populates attributes from the response content
6061
def populate_attrs_from(content)
6162
return unless content
6263

63-
data_key = data_key_for klass: self.class.base_name
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
6467

65-
supported_attrs.each do |attrib|
66-
value = if content.key?(data_key) && content[data_key].is_a?(Hash) && content[data_key].key?(attrib)
67-
content[data_key][attrib]
68-
else
69-
content[attrib]
70-
end
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
7175

72-
instance_variable_set :"@#{attrib}", value
73-
end
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)
7482
end
7583
end
7684
end

lib/ruby_lokalise_api/response.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,11 @@ def patch_endpoint_with(new_endpoint)
3131
@endpoint = new_endpoint
3232
end
3333

34-
# Returns an array of pagination headers
35-
def pagination_headers
36-
self.class.const_get(:PAGINATION_HEADERS)
37-
end
38-
3934
private
4035

4136
# Keep only pagination headers
4237
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)
38+
raw_headers.to_h.slice(*PAGINATION_HEADERS).transform_keys(&:to_sym)
4739
end
4840
end
4941
end

0 commit comments

Comments
 (0)