Skip to content

Commit

Permalink
prepare for a new version\
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis committed Nov 30, 2022
1 parent 17374df commit 9538253
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 42 deletions.
5 changes: 5 additions & 0 deletions docs/_data/api_items.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
path: upload-translation-file
- title: Delete a file
path: delete-a-file
- title: JWT
path: jwt
sub_paths:
- title: Get OTA JWT
path: get-ota-jwt
- title: Translation keys
path: keys
sub_paths:
Expand Down
9 changes: 9 additions & 0 deletions docs/additional_info/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 7.1.0 (30-Nov-2022)

* Added support for the [`Jwt` endpoint](https://developers.lokalise.com/reference/get-ota-jwt). You can now request JWT easily (please note that these tokens are used only to work with OTA):

```ruby
resp = @client.jwt
resp.jwt # => 'eyJ0eXAiOi...`
```

## 7.0.0 (18-Nov-2022)

* **Breaking change**: `#token` and `#refresh` methods (used to request OAuth 2 access and refresh tokens) now return proper Ruby objects:
Expand Down
17 changes: 17 additions & 0 deletions docs/api/jwt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# JWT

## Get OTA JWT

[Doc](https://developers.lokalise.com/reference/get-ota-jwt)

```ruby
@client.jwt # Output:
## A JWT resource
```

For example:

```ruby
resp = @client.jwt
resp.jwt # => 'eyJ0eXAiOi...`
```
49 changes: 49 additions & 0 deletions lib/ruby_lokalise_api/base_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module RubyLokaliseApi
class BaseClient
attr_reader :token, :token_header
attr_accessor :timeout, :open_timeout

def initialize(token, params = {})
@token = token
@timeout = params.fetch(:timeout, nil)
@open_timeout = params.fetch(:open_timeout, nil)
@token_header = ''
end

# rubocop:disable Metrics/ParameterLists
# Constructs request to perform the specified action
# @param klass The actual class to call the method upon
# @param method [Symbol] The method to call (:new, :update, :create etc)
# @param endpoint_ids [Array, Hash] IDs that are used to generate the proper path to the endpoint
# @param params [Array, Hash] Request parameters
# @param object_key [String, Symbol] Key that should be used to wrap parameters into
# @param initial_ids [Array] IDs that should be used to generate base endpoint path.
# The base path is used for method chaining
def construct_request(klass, method, endpoint_ids, params = {}, object_key = nil, initial_ids = nil)
path = klass.endpoint(*endpoint_ids)
formatted_params = format_params(params, object_key)
formatted_params[:_initial_path] = klass.endpoint(*initial_ids) if initial_ids
klass.send method, self, path, formatted_params
end
# rubocop:enable Metrics/ParameterLists

# Converts `params` to hash with arrays under the `object_key` key.
# Used in bulk operations
#
# @return [Hash]
def format_params(params, object_key)
return params unless object_key

params = [params] unless params.is_a?(Array)
{object_key => params}
end

def base_url; end

def compression?; end

alias c_r construct_request
end
end
41 changes: 4 additions & 37 deletions lib/ruby_lokalise_api/client.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

module RubyLokaliseApi
class Client
class Client < BaseClient
include RubyLokaliseApi::Rest::Branches
include RubyLokaliseApi::Rest::Comments
include RubyLokaliseApi::Rest::Contributors
include RubyLokaliseApi::Rest::CustomTranslationStatuses
include RubyLokaliseApi::Rest::Files
include RubyLokaliseApi::Rest::Jwt
include RubyLokaliseApi::Rest::Keys
include RubyLokaliseApi::Rest::Languages
include RubyLokaliseApi::Rest::Orders
Expand All @@ -25,42 +26,10 @@ class Client
include RubyLokaliseApi::Rest::Translations
include RubyLokaliseApi::Rest::Webhooks

attr_reader :token, :token_header
attr_accessor :timeout, :open_timeout

def initialize(token, params = {})
@token = token
@timeout = params.fetch(:timeout, nil)
@open_timeout = params.fetch(:open_timeout, nil)
@token_header = 'x-api-token'
end

# rubocop:disable Metrics/ParameterLists
# Constructs request to perform the specified action
# @param klass The actual class to call the method upon
# @param method [Symbol] The method to call (:new, :update, :create etc)
# @param endpoint_ids [Array, Hash] IDs that are used to generate the proper path to the endpoint
# @param params [Array, Hash] Request parameters
# @param object_key [String, Symbol] Key that should be used to wrap parameters into
# @param initial_ids [Array] IDs that should be used to generate base endpoint path.
# The base path is used for method chaining
def construct_request(klass, method, endpoint_ids, params = {}, object_key = nil, initial_ids = nil)
path = klass.endpoint(*endpoint_ids)
formatted_params = format_params(params, object_key)
formatted_params[:_initial_path] = klass.endpoint(*initial_ids) if initial_ids
klass.send method, self, path, formatted_params
end
# rubocop:enable Metrics/ParameterLists

# Converts `params` to hash with arrays under the `object_key` key.
# Used in bulk operations
#
# @return [Hash]
def format_params(params, object_key)
return params unless object_key
super(token, params)

params = [params] unless params.is_a?(Array)
{object_key => params}
@token_header = 'x-api-token'
end

def base_url
Expand All @@ -70,7 +39,5 @@ def base_url
def compression?
true
end

alias c_r construct_request
end
end
2 changes: 2 additions & 0 deletions lib/ruby_lokalise_api/data/attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ custom_translation_status:
file:
- filename
- key_count
jwt:
- jwt
key:
- key_id
- created_at
Expand Down
8 changes: 4 additions & 4 deletions lib/ruby_lokalise_api/oauth2/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ def initialize(client_id, client_secret, params = {})
@open_timeout = params[:open_timeout]
end

def base_url
URI('https://app.lokalise.com/oauth2/')
end

def auth(scope:, redirect_uri: nil, state: nil)
scope = scope.join(' ') if scope.is_a?(Array)

Expand Down Expand Up @@ -49,6 +45,10 @@ def refresh(token)
RubyLokaliseApi::OAuth2::Refresh.new post('token', self, params)
end

def base_url
URI('https://app.lokalise.com/oauth2/')
end

def compression?
false
end
Expand Down
13 changes: 13 additions & 0 deletions lib/ruby_lokalise_api/resources/jwt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module RubyLokaliseApi
module Resources
class Jwt < Base
class << self
def endpoint(*_args)
path_from 'jwt-tokens': 'ota'
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/ruby_lokalise_api/rest/jwt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module RubyLokaliseApi
module Rest
module Jwt
# Returns a JWT that can be used to work with OTA
#
# @see https://developers.lokalise.com/reference/get-ota-jwt
# @return [RubyLokaliseApi::Resources::Jwt]
def jwt
c_r RubyLokaliseApi::Resources::Jwt, :find, nil, {}
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby_lokalise_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RubyLokaliseApi
VERSION = '7.0.0'
VERSION = '7.1.0'
end
67 changes: 67 additions & 0 deletions spec/fixtures/vcr_cassettes/jwt.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions spec/lib/ruby_lokalise_api/client/jwt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

RSpec.describe RubyLokaliseApi::Client do
specify '#jwt' do
resp = VCR.use_cassette('jwt') do
test_client.jwt
end

expect(resp).to be_an_instance_of(RubyLokaliseApi::Resources::Jwt)
expect(resp.jwt).to include('eyJ0eXAiOi')
end
end

0 comments on commit 9538253

Please sign in to comment.