ruby-lokalise-api 4.3.1 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/lib/ruby-lokalise-api/base_request.rb +24 -0
- data/lib/ruby-lokalise-api/client.rb +5 -3
- data/lib/ruby-lokalise-api/collections/base.rb +19 -7
- data/lib/ruby-lokalise-api/collections/segment.rb +15 -0
- data/lib/ruby-lokalise-api/connection.rb +3 -2
- data/lib/ruby-lokalise-api/data/attributes.json +26 -0
- data/lib/ruby-lokalise-api/error.rb +2 -2
- data/lib/ruby-lokalise-api/oauth2/auth.rb +62 -0
- data/lib/ruby-lokalise-api/oauth2/connection.rb +25 -0
- data/lib/ruby-lokalise-api/oauth2/request.rb +23 -0
- data/lib/ruby-lokalise-api/oauth2_client.rb +11 -0
- data/lib/ruby-lokalise-api/request.rb +4 -14
- data/lib/ruby-lokalise-api/resources/base.rb +18 -7
- data/lib/ruby-lokalise-api/resources/branch.rb +3 -1
- data/lib/ruby-lokalise-api/resources/contributor.rb +1 -1
- data/lib/ruby-lokalise-api/resources/custom_translation_status.rb +1 -1
- data/lib/ruby-lokalise-api/resources/key.rb +1 -0
- data/lib/ruby-lokalise-api/resources/key_comment.rb +1 -1
- data/lib/ruby-lokalise-api/resources/order.rb +1 -0
- data/lib/ruby-lokalise-api/resources/payment_card.rb +1 -0
- data/lib/ruby-lokalise-api/resources/project.rb +1 -0
- data/lib/ruby-lokalise-api/resources/project_comment.rb +1 -0
- data/lib/ruby-lokalise-api/resources/project_language.rb +1 -1
- data/lib/ruby-lokalise-api/resources/queued_process.rb +1 -1
- data/lib/ruby-lokalise-api/resources/screenshot.rb +1 -0
- data/lib/ruby-lokalise-api/resources/segment.rb +19 -0
- data/lib/ruby-lokalise-api/resources/snapshot.rb +1 -0
- data/lib/ruby-lokalise-api/resources/task.rb +1 -0
- data/lib/ruby-lokalise-api/resources/team_user.rb +1 -1
- data/lib/ruby-lokalise-api/resources/team_user_billing_details.rb +14 -0
- data/lib/ruby-lokalise-api/resources/team_user_group.rb +1 -1
- data/lib/ruby-lokalise-api/resources/translation.rb +1 -0
- data/lib/ruby-lokalise-api/resources/translation_provider.rb +1 -1
- data/lib/ruby-lokalise-api/resources/webhook.rb +1 -0
- data/lib/ruby-lokalise-api/rest/segments.rb +43 -0
- data/lib/ruby-lokalise-api/rest/team_user_billing_details.rb +34 -0
- data/lib/ruby-lokalise-api/utils/attribute_helpers.rb +3 -1
- data/lib/ruby-lokalise-api/utils/string_utils.rb +24 -18
- data/lib/ruby-lokalise-api/version.rb +1 -1
- data/lib/ruby-lokalise-api.rb +30 -2
- data/ruby-lokalise-api.gemspec +18 -15
- data/spec/lib/ruby-lokalise-api/connection_spec.rb +28 -43
- data/spec/lib/ruby-lokalise-api/oauth2/auth_spec.rb +73 -0
- data/spec/lib/ruby-lokalise-api/rest/branches_spec.rb +8 -8
- data/spec/lib/ruby-lokalise-api/rest/contributors_spec.rb +2 -2
- data/spec/lib/ruby-lokalise-api/rest/keys_spec.rb +1 -1
- data/spec/lib/ruby-lokalise-api/rest/languages_spec.rb +2 -2
- data/spec/lib/ruby-lokalise-api/rest/projects_spec.rb +14 -1
- data/spec/lib/ruby-lokalise-api/rest/screenshots_spec.rb +2 -2
- data/spec/lib/ruby-lokalise-api/rest/segments_spec.rb +106 -0
- data/spec/lib/ruby-lokalise-api/rest/snapshots_spec.rb +1 -1
- data/spec/lib/ruby-lokalise-api/rest/tasks_spec.rb +2 -2
- data/spec/lib/ruby-lokalise-api/rest/team_user_billing_details_spec.rb +48 -0
- data/spec/lib/ruby-lokalise-api/rest/team_users_spec.rb +1 -1
- data/spec/lib/ruby-lokalise-api/rest/translations_spec.rb +1 -1
- data/spec/lib/ruby-lokalise-api_spec.rb +15 -7
- data/spec/support/test_client.rb +8 -0
- data/spec/support/vcr.rb +5 -0
- metadata +27 -12
- data/spec/lib/ruby-lokalise-api/utils/snakecase_spec.rb +0 -19
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lokalise
|
4
|
+
class Client
|
5
|
+
# Returns all segments for the given key and language ISO code
|
6
|
+
#
|
7
|
+
# @see https://app.lokalise.com/api2docs/curl/#transition-list-all-segments-for-key-language-get
|
8
|
+
# @return [Lokalise::Collection::Segments<Lokalise::Resources::Segment>]
|
9
|
+
# @param project_id [String]
|
10
|
+
# @param key_id [String, Integer]
|
11
|
+
# @param lang_iso [String]
|
12
|
+
# @param params [Hash]
|
13
|
+
def segments(project_id, key_id, lang_iso, params = {})
|
14
|
+
c_r Lokalise::Collections::Segment, :all, [project_id, key_id, lang_iso], params
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns a segment under a given number for the given key and language ISO code
|
18
|
+
#
|
19
|
+
# @see https://app.lokalise.com/api2docs/curl/#transition-retrieve-a-segment-for-key-language-get
|
20
|
+
# @return [Lokalise::Resources::Segment]
|
21
|
+
# @param project_id [String]
|
22
|
+
# @param key_id [String, Integer]
|
23
|
+
# @param lang_iso [String]
|
24
|
+
# @params segment_number [Integer, String]
|
25
|
+
# @param params [Hash]
|
26
|
+
def segment(project_id, key_id, lang_iso, segment_number, params = {})
|
27
|
+
c_r Lokalise::Resources::Segment, :find, [project_id, key_id, lang_iso, segment_number], params
|
28
|
+
end
|
29
|
+
|
30
|
+
# Updates a segment under a given number for the given key and language ISO code
|
31
|
+
#
|
32
|
+
# @see https://app.lokalise.com/api2docs/curl/#transition-update-a-segment-post
|
33
|
+
# @return [Lokalise::Resources::Segment]
|
34
|
+
# @param project_id [String]
|
35
|
+
# @param key_id [String, Integer]
|
36
|
+
# @param lang_iso [String]
|
37
|
+
# @params segment_number [Integer, String]
|
38
|
+
# @param params [Hash]
|
39
|
+
def update_segment(project_id, key_id, lang_iso, segment_number, params = {})
|
40
|
+
c_r Lokalise::Resources::Segment, :update, [project_id, key_id, lang_iso, segment_number], params
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lokalise
|
4
|
+
class Client
|
5
|
+
# Returns team user billing details
|
6
|
+
#
|
7
|
+
# @see https://app.lokalise.com/api2docs/curl/#transition-retrieve-team-user-billing-details-get
|
8
|
+
# @return [Lokalise::Resources::TeamUserBillingDetails]
|
9
|
+
# @param team_id [String]
|
10
|
+
def team_user_billing_details(team_id)
|
11
|
+
c_r Lokalise::Resources::TeamUserBillingDetails, :find, team_id
|
12
|
+
end
|
13
|
+
|
14
|
+
# Creates team user billing details
|
15
|
+
#
|
16
|
+
# @see https://app.lokalise.com/api2docs/curl/#transition-create-team-user-billing-details-post
|
17
|
+
# @return [Lokalise::Resources::TeamUserBillingDetails]
|
18
|
+
# @param team_id [String]
|
19
|
+
# @param params [Hash]
|
20
|
+
def create_team_user_billing_details(team_id, params)
|
21
|
+
c_r Lokalise::Resources::TeamUserBillingDetails, :create, team_id, params
|
22
|
+
end
|
23
|
+
|
24
|
+
# Updates team user billing details
|
25
|
+
#
|
26
|
+
# @see https://app.lokalise.com/api2docs/curl/#transition-create-team-user-billing-details-post
|
27
|
+
# @return [Lokalise::Resources::TeamUserBillingDetails]
|
28
|
+
# @param team_id [String]
|
29
|
+
# @param params [Hash]
|
30
|
+
def update_team_user_billing_details(team_id, params)
|
31
|
+
c_r Lokalise::Resources::TeamUserBillingDetails, :update, team_id, params
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -3,6 +3,8 @@
|
|
3
3
|
module Lokalise
|
4
4
|
module Utils
|
5
5
|
module AttributeHelpers
|
6
|
+
using Lokalise::Utils::StringUtils
|
7
|
+
|
6
8
|
UNIFIED_RESOURCES = %w[comment language].freeze
|
7
9
|
|
8
10
|
# Returns the name of the API resource for the given class.
|
@@ -40,7 +42,7 @@ module Lokalise
|
|
40
42
|
# @return [String]
|
41
43
|
# @param model_class [String]
|
42
44
|
def id_key_for(model_class)
|
43
|
-
|
45
|
+
get_key name: 'ID_KEY', model_class: model_class
|
44
46
|
end
|
45
47
|
|
46
48
|
# Loads attributes for the given resource based on its name
|
@@ -3,25 +3,31 @@
|
|
3
3
|
# Initial code taken from Facets gem by Rubyworks
|
4
4
|
# https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
6
|
+
module Lokalise
|
7
|
+
module Utils
|
8
|
+
module StringUtils
|
9
|
+
refine String do
|
10
|
+
# Underscore a string such that camelcase, dashes and spaces are
|
11
|
+
# replaced by underscores.
|
12
|
+
def snakecase
|
13
|
+
base_class_name.
|
14
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
15
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
16
|
+
tr('-', '_').
|
17
|
+
gsub(/\s/, '_').
|
18
|
+
gsub(/__+/, '_').
|
19
|
+
downcase
|
20
|
+
end
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
# Turn `Module::Nested::ClassName` to just `ClassName`
|
23
|
+
def base_class_name
|
24
|
+
split('::').last
|
25
|
+
end
|
23
26
|
|
24
|
-
|
25
|
-
|
27
|
+
def remove_trailing_slash
|
28
|
+
delete_suffix '/'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
26
32
|
end
|
27
33
|
end
|
data/lib/ruby-lokalise-api.rb
CHANGED
@@ -1,19 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'faraday'
|
4
|
-
require '
|
4
|
+
require 'faraday/gzip'
|
5
5
|
require 'yaml'
|
6
6
|
require 'addressable'
|
7
7
|
|
8
8
|
require 'ruby-lokalise-api/version'
|
9
9
|
require 'ruby-lokalise-api/json_handler'
|
10
10
|
require 'ruby-lokalise-api/connection'
|
11
|
+
require 'ruby-lokalise-api/base_request'
|
11
12
|
require 'ruby-lokalise-api/request'
|
12
13
|
require 'ruby-lokalise-api/error'
|
13
14
|
require 'ruby-lokalise-api/utils/string_utils'
|
14
15
|
require 'ruby-lokalise-api/utils/attribute_helpers'
|
15
16
|
require 'ruby-lokalise-api/utils/endpoint_helpers'
|
16
17
|
|
18
|
+
require 'ruby-lokalise-api/oauth2/connection'
|
19
|
+
require 'ruby-lokalise-api/oauth2/request'
|
20
|
+
|
17
21
|
require 'ruby-lokalise-api/resources/base'
|
18
22
|
require 'ruby-lokalise-api/resources/branch'
|
19
23
|
require 'ruby-lokalise-api/resources/project'
|
@@ -37,6 +41,8 @@ require 'ruby-lokalise-api/resources/translation_provider'
|
|
37
41
|
require 'ruby-lokalise-api/resources/team_user_group'
|
38
42
|
require 'ruby-lokalise-api/resources/custom_translation_status'
|
39
43
|
require 'ruby-lokalise-api/resources/webhook'
|
44
|
+
require 'ruby-lokalise-api/resources/segment'
|
45
|
+
require 'ruby-lokalise-api/resources/team_user_billing_details'
|
40
46
|
|
41
47
|
require 'ruby-lokalise-api/collections/base'
|
42
48
|
require 'ruby-lokalise-api/collections/branch'
|
@@ -61,8 +67,12 @@ require 'ruby-lokalise-api/collections/translation_provider'
|
|
61
67
|
require 'ruby-lokalise-api/collections/team_user_group'
|
62
68
|
require 'ruby-lokalise-api/collections/custom_translation_status'
|
63
69
|
require 'ruby-lokalise-api/collections/webhook'
|
70
|
+
require 'ruby-lokalise-api/collections/segment'
|
64
71
|
|
65
72
|
require 'ruby-lokalise-api/client'
|
73
|
+
require 'ruby-lokalise-api/oauth2_client'
|
74
|
+
|
75
|
+
require 'ruby-lokalise-api/oauth2/auth'
|
66
76
|
|
67
77
|
module Lokalise
|
68
78
|
class << self
|
@@ -72,12 +82,30 @@ module Lokalise
|
|
72
82
|
# @param token [String]
|
73
83
|
# @param params [Hash]
|
74
84
|
def client(token, params = {})
|
75
|
-
@client
|
85
|
+
@client = Lokalise::Client.new token, params
|
76
86
|
end
|
77
87
|
|
78
88
|
# Reset the currently set client
|
79
89
|
def reset_client!
|
80
90
|
@client = nil
|
81
91
|
end
|
92
|
+
|
93
|
+
# Initializes a new OAuth2Client object
|
94
|
+
#
|
95
|
+
# @return [Lokalise::OAuth2Client]
|
96
|
+
# @param token [String]
|
97
|
+
# @param params [Hash]
|
98
|
+
def oauth2_client(token, params = {})
|
99
|
+
@oauth2_client = Lokalise::OAuth2Client.new token, params
|
100
|
+
end
|
101
|
+
|
102
|
+
# Reset the currently set OAuth2 client
|
103
|
+
def reset_oauth2_client!
|
104
|
+
@oauth2_client = nil
|
105
|
+
end
|
106
|
+
|
107
|
+
def auth_client(client_id, client_secret)
|
108
|
+
Lokalise::OAuth2::Auth.new client_id, client_secret
|
109
|
+
end
|
82
110
|
end
|
83
111
|
end
|
data/ruby-lokalise-api.gemspec
CHANGED
@@ -5,7 +5,7 @@ require File.expand_path('lib/ruby-lokalise-api/version', __dir__)
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'ruby-lokalise-api'
|
7
7
|
spec.version = Lokalise::VERSION
|
8
|
-
spec.authors = ['Ilya Bodrov']
|
8
|
+
spec.authors = ['Ilya Bodrov-Krukowski']
|
9
9
|
spec.email = ['golosizpru@gmail.com']
|
10
10
|
spec.summary = 'Ruby interface to the Lokalise API'
|
11
11
|
spec.description = 'Opinionated Ruby client for the Lokalise platform API allowing to work with translations, projects, users and other resources as with Ruby objects.'
|
@@ -22,19 +22,22 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.extra_rdoc_files = ['README.md']
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
-
spec.add_dependency 'addressable',
|
26
|
-
spec.add_dependency 'faraday',
|
27
|
-
spec.add_dependency '
|
28
|
-
spec.add_dependency 'json',
|
25
|
+
spec.add_dependency 'addressable', '~> 2.5'
|
26
|
+
spec.add_dependency 'faraday', '~> 2.0'
|
27
|
+
spec.add_dependency 'faraday-gzip', '~> 0.1'
|
28
|
+
spec.add_dependency 'json', '>= 1.8.0'
|
29
29
|
|
30
|
-
spec.add_development_dependency 'codecov',
|
31
|
-
spec.add_development_dependency 'dotenv',
|
32
|
-
spec.add_development_dependency 'oj',
|
33
|
-
spec.add_development_dependency 'rake',
|
34
|
-
spec.add_development_dependency 'rspec',
|
35
|
-
spec.add_development_dependency 'rubocop',
|
36
|
-
spec.add_development_dependency 'rubocop-performance',
|
37
|
-
spec.add_development_dependency 'rubocop-rspec',
|
38
|
-
spec.add_development_dependency 'simplecov',
|
39
|
-
spec.add_development_dependency 'vcr',
|
30
|
+
spec.add_development_dependency 'codecov', '~> 0.1'
|
31
|
+
spec.add_development_dependency 'dotenv', '~> 2.5'
|
32
|
+
spec.add_development_dependency 'oj', '~> 3.10'
|
33
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
34
|
+
spec.add_development_dependency 'rspec', '~> 3.6'
|
35
|
+
spec.add_development_dependency 'rubocop', '~> 1.6'
|
36
|
+
spec.add_development_dependency 'rubocop-performance', '~> 1.5'
|
37
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 2.0'
|
38
|
+
spec.add_development_dependency 'simplecov', '~> 0.16'
|
39
|
+
spec.add_development_dependency 'vcr', '~> 6.0'
|
40
|
+
spec.metadata = {
|
41
|
+
'rubygems_mfa_required' => 'true'
|
42
|
+
}
|
40
43
|
end
|
@@ -1,83 +1,68 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
RSpec.describe Lokalise::Connection do
|
4
|
-
include
|
4
|
+
let(:dummy) { Class.new { include Lokalise::Connection }.new }
|
5
5
|
|
6
6
|
let(:project_id) { '803826145ba90b42d5d860.46800099' }
|
7
7
|
let(:key_id) { 44_596_059 }
|
8
8
|
|
9
9
|
before { Lokalise.reset_client! }
|
10
10
|
|
11
|
-
after
|
12
|
-
|
13
|
-
|
11
|
+
after { Lokalise.reset_client! }
|
12
|
+
|
13
|
+
it 'Authorization header must be present for OAuth2 client' do
|
14
|
+
conn = dummy.connection test_oauth2_client
|
15
|
+
expect(conn.headers['Authorization']).to eq(test_oauth2_client.token)
|
16
|
+
expect(conn.headers['X-api-token']).to be_nil
|
14
17
|
end
|
15
18
|
|
16
|
-
it 'timeouts
|
17
|
-
conn = connection test_client
|
19
|
+
it 'timeouts are not be set by default but the token must be present' do
|
20
|
+
conn = dummy.connection test_client
|
18
21
|
expect(conn.options.timeout).to be_nil
|
19
22
|
expect(conn.options.open_timeout).to be_nil
|
20
23
|
expect(conn.headers['X-api-token']).to eq(test_client.token)
|
21
|
-
expect(conn.builder.handlers).not_to include(FaradayMiddleware::Gzip)
|
22
24
|
end
|
23
25
|
|
24
26
|
it 'allows to customize timeouts' do
|
25
27
|
custom_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'],
|
26
28
|
open_timeout: 100, timeout: 500)
|
27
|
-
conn = connection custom_client
|
29
|
+
conn = dummy.connection custom_client
|
28
30
|
expect(conn.options.timeout).to eq(500)
|
29
31
|
expect(conn.options.open_timeout).to eq(100)
|
30
32
|
expect(conn.headers['X-api-token']).to eq(custom_client.token)
|
31
33
|
|
32
34
|
custom_client.timeout = 300
|
33
35
|
custom_client.open_timeout = 200
|
34
|
-
another_conn = connection custom_client
|
36
|
+
another_conn = dummy.connection custom_client
|
35
37
|
expect(another_conn.options.timeout).to eq(300)
|
36
38
|
expect(another_conn.options.open_timeout).to eq(200)
|
37
39
|
end
|
38
40
|
|
39
|
-
it '
|
40
|
-
|
41
|
-
expect(conn.builder.adapter).to eq(Faraday::Adapter::NetHttp)
|
42
|
-
|
43
|
-
Faraday.default_adapter = :excon
|
44
|
-
|
45
|
-
another_conn = connection test_client
|
46
|
-
expect(another_conn.builder.adapter).to eq(Faraday::Adapter::Excon)
|
47
|
-
expect(conn.builder.adapter).to eq(Faraday::Adapter::NetHttp)
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'allows to customize compression' do
|
51
|
-
custom_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'], enable_compression: true)
|
52
|
-
conn = connection custom_client
|
53
|
-
expect(conn.headers['X-api-token']).to eq(custom_client.token)
|
54
|
-
expect(conn.builder.handlers).to include(FaradayMiddleware::Gzip)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'is possible to enable gzip compression' do
|
58
|
-
gzip_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'], enable_compression: true)
|
41
|
+
it 'works with gzip compression' do
|
42
|
+
gzip_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'])
|
59
43
|
keys = VCR.use_cassette('all_keys_gzip') do
|
60
|
-
gzip_client.keys project_id
|
44
|
+
gzip_client.keys project_id, limit: 30
|
61
45
|
end.collection
|
62
46
|
|
63
47
|
expect(keys.first.key_id).to eq(key_id)
|
64
48
|
end
|
65
49
|
|
66
|
-
it '
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
expect(keys.first.key_id).to eq(key_id)
|
50
|
+
it 'gzip compression is on by default' do
|
51
|
+
custom_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'])
|
52
|
+
conn = dummy.connection custom_client
|
53
|
+
expect(conn.headers['X-api-token']).to eq(custom_client.token)
|
54
|
+
expect(conn.builder.handlers).to include(Faraday::Gzip::Middleware)
|
73
55
|
end
|
74
56
|
|
75
|
-
it '
|
76
|
-
|
77
|
-
|
78
|
-
default_gzip_client.keys project_id
|
79
|
-
end.collection
|
57
|
+
it 'is possible to customize adapter' do
|
58
|
+
conn = dummy.connection test_client
|
59
|
+
expect(conn.builder.adapter).to eq(Faraday::Adapter::NetHttp)
|
80
60
|
|
81
|
-
|
61
|
+
Faraday.default_adapter = :test
|
62
|
+
|
63
|
+
another_conn = dummy.connection test_client
|
64
|
+
|
65
|
+
expect(another_conn.builder.adapter).to eq(Faraday::Adapter::Test)
|
66
|
+
expect(conn.builder.adapter).to eq(Faraday::Adapter::NetHttp)
|
82
67
|
end
|
83
68
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Lokalise::OAuth2::Auth do
|
4
|
+
describe '#auth' do
|
5
|
+
it 'returns auth code' do
|
6
|
+
uri = auth_client.auth scope: 'read_projects'
|
7
|
+
expect(uri).to include(described_class::BASE_URL.to_s)
|
8
|
+
expect(uri).to include('auth?client_id=')
|
9
|
+
expect(uri).to include('&scope=read_projects')
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'allows to pass an array of scopes' do
|
13
|
+
scopes = %w[write_projects write_team_groups write_tasks]
|
14
|
+
uri = auth_client.auth scope: scopes
|
15
|
+
expect(uri).to include(described_class::BASE_URL.to_s)
|
16
|
+
expect(uri).to include('auth?client_id=')
|
17
|
+
expect(uri).to include("&scope=#{scopes.join('+')}")
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'allows to set redirect_uri' do
|
21
|
+
uri = auth_client.auth scope: 'read_projects', redirect_uri: 'http://example.com/callback'
|
22
|
+
expect(uri).to include(described_class::BASE_URL.to_s)
|
23
|
+
expect(uri).to include('example.com%2Fcallback')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'allows to set state' do
|
27
|
+
state = '123abc'
|
28
|
+
uri = auth_client.auth scope: 'read_projects', state: state
|
29
|
+
expect(uri).to include(described_class::BASE_URL.to_s)
|
30
|
+
expect(uri).to include(state)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#token' do
|
35
|
+
it 'returns an OAuth2 token' do
|
36
|
+
resp = VCR.use_cassette('oauth2/token') do
|
37
|
+
auth_client.token ENV['OAUTH2_CODE']
|
38
|
+
end
|
39
|
+
expect(resp['access_token']).not_to be_nil
|
40
|
+
expect(resp['refresh_token']).not_to be_nil
|
41
|
+
expect(resp['expires_in']).to eq(3600)
|
42
|
+
expect(resp['token_type']).to eq('Bearer')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'raises an error when the code is invalid' do
|
46
|
+
expect do
|
47
|
+
VCR.use_cassette('oauth2/token_error') do
|
48
|
+
auth_client.token 'incorrect_code'
|
49
|
+
end
|
50
|
+
end.to raise_error(Lokalise::Error::BadRequest)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#refresh' do
|
55
|
+
it 'returns a refreshed OAuth2 token' do
|
56
|
+
resp = VCR.use_cassette('oauth2/refresh') do
|
57
|
+
auth_client.refresh ENV['OAUTH2_REFRESH_TOKEN']
|
58
|
+
end
|
59
|
+
expect(resp['access_token']).not_to be_nil
|
60
|
+
expect(resp['refresh_token']).to be_nil
|
61
|
+
expect(resp['expires_in']).to eq(3600)
|
62
|
+
expect(resp['token_type']).to eq('Bearer')
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'raises an error when the token is invalid' do
|
66
|
+
expect do
|
67
|
+
VCR.use_cassette('oauth2/refresh_error') do
|
68
|
+
auth_client.refresh 'incorrect_token'
|
69
|
+
end
|
70
|
+
end.to raise_error(Lokalise::Error::BadRequest)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -10,20 +10,20 @@ RSpec.describe Lokalise::Client do
|
|
10
10
|
test_client.branches project_id
|
11
11
|
end.collection
|
12
12
|
|
13
|
-
expect(branches.count).to eq(
|
13
|
+
expect(branches.count).to eq(4)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'supports pagination' do
|
17
17
|
branches = VCR.use_cassette('all_branches_pagination') do
|
18
|
-
test_client.branches project_id, limit:
|
18
|
+
test_client.branches project_id, limit: 2, page: 2
|
19
19
|
end
|
20
20
|
|
21
|
-
expect(branches.collection.count).to eq(
|
22
|
-
expect(branches.total_results).to eq(
|
23
|
-
expect(branches.total_pages).to eq(
|
24
|
-
expect(branches.results_per_page).to eq(
|
25
|
-
expect(branches.current_page).to eq(
|
26
|
-
expect(branches.collection.first.name).to eq('
|
21
|
+
expect(branches.collection.count).to eq(2)
|
22
|
+
expect(branches.total_results).to eq(4)
|
23
|
+
expect(branches.total_pages).to eq(2)
|
24
|
+
expect(branches.results_per_page).to eq(2)
|
25
|
+
expect(branches.current_page).to eq(2)
|
26
|
+
expect(branches.collection.first.name).to eq('merge-me')
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -106,7 +106,7 @@ RSpec.describe Lokalise::Client do
|
|
106
106
|
languages: [{lang_iso: 'en'}]
|
107
107
|
end.collection.first
|
108
108
|
|
109
|
-
expect(contributor.client).to eq(test_client)
|
109
|
+
expect(contributor.client.token).to eq(test_client.token)
|
110
110
|
expect(contributor.fullname).to eq('chained')
|
111
111
|
|
112
112
|
id = contributor.user_id
|
@@ -116,7 +116,7 @@ RSpec.describe Lokalise::Client do
|
|
116
116
|
contributor.update fullname: 'updated!'
|
117
117
|
end
|
118
118
|
|
119
|
-
expect(updated_contributor.client).to eq(test_client)
|
119
|
+
expect(updated_contributor.client.token).to eq(test_client.token)
|
120
120
|
expect(updated_contributor.fullname).to eq('updated!')
|
121
121
|
expect(updated_contributor.user_id).to eq(id)
|
122
122
|
expect(updated_contributor.path).to eq(path)
|
@@ -160,7 +160,7 @@ RSpec.describe Lokalise::Client do
|
|
160
160
|
key.update key_name: 'updated!'
|
161
161
|
end
|
162
162
|
|
163
|
-
expect(updated_key.client).to eq(test_client)
|
163
|
+
expect(updated_key.client.token).to eq(test_client.token)
|
164
164
|
expect(updated_key.key_name['ios']).to eq('updated!')
|
165
165
|
expect(updated_key.path).to eq(path)
|
166
166
|
|
@@ -109,7 +109,7 @@ RSpec.describe Lokalise::Client do
|
|
109
109
|
test_client.create_languages project_id, lang_iso: 'ab', custom_name: 'chained lang'
|
110
110
|
end.collection.first
|
111
111
|
|
112
|
-
expect(language.client).to eq(test_client)
|
112
|
+
expect(language.client.token).to eq(test_client.token)
|
113
113
|
expect(language.lang_name).to eq('chained lang')
|
114
114
|
|
115
115
|
path = language.path
|
@@ -118,7 +118,7 @@ RSpec.describe Lokalise::Client do
|
|
118
118
|
language.update lang_name: 'updated!'
|
119
119
|
end
|
120
120
|
|
121
|
-
expect(updated_language.client).to eq(test_client)
|
121
|
+
expect(updated_language.client.token).to eq(test_client.token)
|
122
122
|
expect(updated_language.lang_name).to eq('updated!')
|
123
123
|
expect(updated_language.lang_id).to eq(language.lang_id)
|
124
124
|
expect(updated_language.path).to eq(path)
|
@@ -73,6 +73,19 @@ RSpec.describe Lokalise::Client do
|
|
73
73
|
expect(project.team_id).to eq(176_692)
|
74
74
|
end
|
75
75
|
|
76
|
+
context 'with OAuth 2 token' do
|
77
|
+
it 'creates a project' do
|
78
|
+
name = 'OAuth 2 project'
|
79
|
+
description = 'Via OAuth 2'
|
80
|
+
project = VCR.use_cassette('oauth2/new_project') do
|
81
|
+
test_oauth2_client.create_project name: name, description: description
|
82
|
+
end
|
83
|
+
|
84
|
+
expect(project.name).to eq(name)
|
85
|
+
expect(project.description).to eq(description)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
76
89
|
specify '#update_project' do
|
77
90
|
project = VCR.use_cassette('update_project') do
|
78
91
|
test_client.update_project new_project_id,
|
@@ -115,7 +128,7 @@ RSpec.describe Lokalise::Client do
|
|
115
128
|
project.update name: 'updated!'
|
116
129
|
end
|
117
130
|
|
118
|
-
expect(updated_project.client).to eq(test_client)
|
131
|
+
expect(updated_project.client.token).to eq(test_client.token)
|
119
132
|
expect(updated_project.name).to eq('updated!')
|
120
133
|
expect(updated_project.path).to eq(path)
|
121
134
|
|
@@ -91,7 +91,7 @@ RSpec.describe Lokalise::Client do
|
|
91
91
|
end.collection.first
|
92
92
|
end
|
93
93
|
|
94
|
-
expect(screenshot.client).to eq(test_client)
|
94
|
+
expect(screenshot.client.token).to eq(test_client.token)
|
95
95
|
expect(screenshot.title).to eq('chained screen')
|
96
96
|
|
97
97
|
path = screenshot.path
|
@@ -100,7 +100,7 @@ RSpec.describe Lokalise::Client do
|
|
100
100
|
screenshot.update title: 'updated!'
|
101
101
|
end
|
102
102
|
|
103
|
-
expect(updated_screenshot.client).to eq(test_client)
|
103
|
+
expect(updated_screenshot.client.token).to eq(test_client.token)
|
104
104
|
expect(updated_screenshot.title).to eq('updated!')
|
105
105
|
expect(updated_screenshot.screenshot_id).to eq(screenshot.screenshot_id)
|
106
106
|
expect(updated_screenshot.path).to eq(path)
|