amorail 0.7.0 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rspec.yml +13 -11
- data/.rubocop.yml +0 -4
- data/CHANGELOG.md +15 -0
- data/amorail.gemspec +2 -2
- data/dip.yml +16 -0
- data/docker-compose.yaml +14 -0
- data/lib/amorail/client.rb +1 -1
- data/lib/amorail/entity/params.rb +11 -2
- data/lib/amorail/entity.rb +8 -2
- data/lib/amorail/store_adapters/memory_store_adapter.rb +1 -1
- data/lib/amorail/store_adapters/redis_store_adapter.rb +2 -2
- data/lib/amorail/version.rb +1 -1
- data/lib/amorail.rb +9 -3
- data/spec/client_spec.rb +35 -0
- data/spec/contact_spec.rb +2 -0
- data/spec/fixtures/authorize_refresh_token.json +6 -0
- data/spec/fixtures/contacts/my_contact_find.json +4 -0
- data/spec/helpers/webmock_helpers.rb +3 -3
- data/spec/my_contact_spec.rb +3 -2
- data/spec/spec_helper.rb +1 -0
- data/spec/store_adapters/memory_store_adapter_spec.rb +1 -1
- data/spec/support/my_contact.rb +1 -0
- metadata +13 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae6f74bb08bbf43aa99b6275ba276971bf8e9933bfdc89c363cd6e03d964c0f5
|
4
|
+
data.tar.gz: 33aa412d6f02256a6e98f0e7e138eca8ac88603633a3940858c0a2f865c6d689
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0dc543509ca614983d4bb92eae450d7202e18a146a89ac5a52f0be77436d5c878b41b3d922760490e74a690ae1b1603a015b76821b7cecd7e0c3f3111e690d2
|
7
|
+
data.tar.gz: 7a7efb60d5746c933d0b043fe0465427854b4a1ece3947c1f55062e11fd50ac8c2ef7d4faa34759807ba6e0adecb852d2af9b52cd00cf0d7faa3a5561c924cf8
|
data/.github/workflows/rspec.yml
CHANGED
@@ -5,19 +5,21 @@ on:
|
|
5
5
|
branches:
|
6
6
|
- master
|
7
7
|
pull_request:
|
8
|
-
|
9
8
|
jobs:
|
10
9
|
build:
|
11
|
-
|
12
10
|
runs-on: ubuntu-latest
|
11
|
+
strategy:
|
12
|
+
matrix:
|
13
|
+
# TODO: Adds 3.0 and 3.1 after updating
|
14
|
+
# ruby: [2.5, 2.7, 3.0, 3.1]
|
15
|
+
ruby: [2.5, 2.7]
|
13
16
|
|
14
17
|
steps:
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
run: bundle exec rspec
|
18
|
+
- uses: actions/checkout@v2
|
19
|
+
- name: Set up Ruby
|
20
|
+
uses: ruby/setup-ruby@v1
|
21
|
+
with:
|
22
|
+
ruby-version: ${{ matrix.ruby }}
|
23
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
24
|
+
- name: Run tests
|
25
|
+
run: bundle exec rspec
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,20 @@
|
|
2
2
|
|
3
3
|
## master (unreleased)
|
4
4
|
|
5
|
+
## 0.7.2 (2024-01-08)
|
6
|
+
|
7
|
+
### Fixes
|
8
|
+
|
9
|
+
- Fixed refreshing tokin with redis storage ([@VladimirMikhailov][]) [#56](https://github.com/teachbase/amorail/pull/56)
|
10
|
+
|
11
|
+
## 0.7.1 (2021-09-25)
|
12
|
+
|
13
|
+
### Features
|
14
|
+
|
15
|
+
- Adds multiple option to property ([@lHydra][])
|
16
|
+
|
17
|
+
See spec/support/my_contact.rb and spec/my_contact_spec.rb
|
18
|
+
|
5
19
|
## 0.7.0 (2021-07-16)
|
6
20
|
|
7
21
|
### Features
|
@@ -17,3 +31,4 @@
|
|
17
31
|
[@palkan]: https://github.com/palkan
|
18
32
|
[@AlexanderShvaykin]: https://github.com/AlexanderShvaykin
|
19
33
|
[@lHydra]: https://github.com/lHydra
|
34
|
+
[@VladimirMikhailov]: https://github.com/VladimirMikhailov
|
data/amorail.gemspec
CHANGED
@@ -19,12 +19,12 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_development_dependency "bundler", "~>
|
22
|
+
spec.add_development_dependency "bundler", "~> 2.3"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.4"
|
25
25
|
spec.add_development_dependency "webmock"
|
26
26
|
spec.add_development_dependency "pry"
|
27
|
-
spec.add_development_dependency "shoulda-matchers"
|
27
|
+
spec.add_development_dependency "shoulda-matchers"
|
28
28
|
spec.add_development_dependency "rubocop", "~> 0.49"
|
29
29
|
spec.add_dependency "anyway_config", ">= 1.0"
|
30
30
|
spec.add_dependency "faraday"
|
data/dip.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
version: "7.6"
|
2
|
+
|
3
|
+
compose:
|
4
|
+
files:
|
5
|
+
- docker-compose.yml
|
6
|
+
|
7
|
+
interaction:
|
8
|
+
sh:
|
9
|
+
description: Open a Bash shell within a Rails container (with dependencies up)
|
10
|
+
service: ruby
|
11
|
+
command: /bin/bash
|
12
|
+
|
13
|
+
rspec:
|
14
|
+
description: Run specs
|
15
|
+
service: ruby
|
16
|
+
command: rspec
|
data/docker-compose.yaml
ADDED
data/lib/amorail/client.rb
CHANGED
@@ -23,8 +23,17 @@ module Amorail # :nodoc: all
|
|
23
23
|
custom_fields = []
|
24
24
|
self.class.properties.each do |k, v|
|
25
25
|
prop_id = props.send(k).id
|
26
|
-
|
27
|
-
|
26
|
+
prop_values = send(v.fetch(:method_name, k))
|
27
|
+
|
28
|
+
if prop_values.is_a?(Array)
|
29
|
+
prop_val = prop_values.map do |value|
|
30
|
+
{ value: value }.merge(v)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
prop_val = [{ value: prop_values }.merge(v)]
|
34
|
+
end
|
35
|
+
|
36
|
+
custom_fields << { id: prop_id, values: prop_val }
|
28
37
|
end
|
29
38
|
|
30
39
|
custom_fields
|
data/lib/amorail/entity.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'active_model'
|
4
|
+
require 'active_support/core_ext/array'
|
4
5
|
|
5
6
|
module Amorail
|
6
7
|
# Core class for all Amo entities (company, contact, etc)
|
@@ -34,7 +35,12 @@ module Amorail
|
|
34
35
|
|
35
36
|
def amo_property(name, options = {})
|
36
37
|
properties[name] = options
|
37
|
-
|
38
|
+
var_name = options.fetch(:method_name, name)
|
39
|
+
define_method "#{var_name}=" do |value|
|
40
|
+
var_value = options[:multiple] ? Array.wrap(value) : Array.wrap(value).first
|
41
|
+
instance_variable_set("@#{var_name}", var_value)
|
42
|
+
end
|
43
|
+
attr_reader(var_name)
|
38
44
|
end
|
39
45
|
|
40
46
|
def attributes
|
@@ -93,7 +99,7 @@ module Amorail
|
|
93
99
|
next if fname.nil?
|
94
100
|
|
95
101
|
fname = "#{fname.downcase}="
|
96
|
-
fval = f.fetch('values').
|
102
|
+
fval = f.fetch('values').map { |v| v['value'] }
|
97
103
|
send(fname, fval) if respond_to?(fname)
|
98
104
|
end
|
99
105
|
end
|
@@ -19,7 +19,7 @@ module Amorail
|
|
19
19
|
def fetch_access(secret)
|
20
20
|
token = storage.get(access_key(secret))
|
21
21
|
refresh_token = storage.get(refresh_key(secret))
|
22
|
-
|
22
|
+
{ token: token, refresh_token: refresh_token }.compact
|
23
23
|
end
|
24
24
|
|
25
25
|
def persist_access(secret, token, refresh_token, expiration)
|
@@ -61,7 +61,7 @@ module Amorail
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def build_redis_url(redis_host: nil, redis_port: nil, redis_db_name: nil)
|
64
|
-
redis_db_name ||= Amorail.config.redis_db_name
|
64
|
+
redis_db_name ||= Amorail.config.redis_db_name.to_s
|
65
65
|
return URI.join(Amorail.config.redis_url, redis_db_name).to_s if Amorail.config.redis_url
|
66
66
|
|
67
67
|
redis_host ||= Amorail.config.redis_host
|
data/lib/amorail/version.rb
CHANGED
data/lib/amorail.rb
CHANGED
@@ -38,7 +38,7 @@ module Amorail
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def with_client(client)
|
41
|
-
client = Client.new(client) unless client.is_a?(Client)
|
41
|
+
client = Client.new(**client) unless client.is_a?(Client)
|
42
42
|
ClientRegistry.client = client
|
43
43
|
yield
|
44
44
|
ensure
|
@@ -61,9 +61,15 @@ module Amorail
|
|
61
61
|
end
|
62
62
|
|
63
63
|
class ClientRegistry # :nodoc:
|
64
|
-
|
64
|
+
class << self
|
65
|
+
def client=(value)
|
66
|
+
Thread.current["attr_#{name}_client"] = value
|
67
|
+
end
|
65
68
|
|
66
|
-
|
69
|
+
def client
|
70
|
+
Thread.current["attr_#{name}_client"]
|
71
|
+
end
|
72
|
+
end
|
67
73
|
end
|
68
74
|
|
69
75
|
require 'amorail/railtie' if defined?(Rails)
|
data/spec/client_spec.rb
CHANGED
@@ -132,4 +132,39 @@ describe Amorail::Client do
|
|
132
132
|
expect(results[2]).to eq 'some_id_3'
|
133
133
|
end
|
134
134
|
end
|
135
|
+
|
136
|
+
describe '#safe_request' do
|
137
|
+
subject(:safe_request) { client.safe_request(:get, '/private/api/v2/json/accounts/current') }
|
138
|
+
|
139
|
+
let(:response) { instance_double('Faraday::Response', body: {}, status: 200) }
|
140
|
+
let(:client) { described_class.new }
|
141
|
+
let(:access_token) { 'eyJ0eXAiOiJKf2QihCJhbGciOiJSUzI1NiIsImp0aSI6IjMxMT' }
|
142
|
+
|
143
|
+
# We need to refresh the token store before the initial safe_request
|
144
|
+
# to test that it performs authorization
|
145
|
+
before { Amorail.token_store = :memory }
|
146
|
+
|
147
|
+
it 'authorizes the client if there is no access token' do
|
148
|
+
expect { safe_request }.to change { client.access_token }.from(nil).to(start_with(access_token))
|
149
|
+
end
|
150
|
+
|
151
|
+
context 'when access token is expired' do
|
152
|
+
let(:renewed_access_token) { '50d084c7efbd911f0a9d03bb387f3ad4dc092be253' }
|
153
|
+
|
154
|
+
before do
|
155
|
+
Amorail.token_store.persist_access(
|
156
|
+
Amorail.config.client_secret,
|
157
|
+
'old_access_token',
|
158
|
+
'refresh_token',
|
159
|
+
Time.now.to_i - 10
|
160
|
+
)
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'refreshes authorization token' do
|
164
|
+
expect { safe_request }.to change { client.access_token }.from(nil).to(
|
165
|
+
start_with(renewed_access_token)
|
166
|
+
)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
135
170
|
end
|
data/spec/contact_spec.rb
CHANGED
@@ -168,10 +168,12 @@ describe Amorail::Contact do
|
|
168
168
|
it "update params" do
|
169
169
|
contact.save!
|
170
170
|
contact.name = "foo"
|
171
|
+
contact.phone = "123456789"
|
171
172
|
|
172
173
|
contact_update_stub(Amorail.config.api_endpoint)
|
173
174
|
expect(contact.save!).to be_truthy
|
174
175
|
expect(contact.name).to eq "foo"
|
176
|
+
expect(contact.phone).to eq "123456789"
|
175
177
|
end
|
176
178
|
|
177
179
|
it "raise error if id is blank?" do
|
@@ -0,0 +1,6 @@
|
|
1
|
+
{
|
2
|
+
"token_type": "Bearer",
|
3
|
+
"expires_in": 86400,
|
4
|
+
"access_token": "50d084c7efbd911f0a9d03bb387f3ad4dc092be253fc5af0564c38454f473c075ecb8b3dc9e65a257196cc0c44a07acea7720140739a86015a701403",
|
5
|
+
"refresh_token": "c17d6c45e63efda9e20edb9ab08eb04cd89cf83acd203a8a4796160886e334561c54e8193f25a27004c732fe784b2bf9f1b4357d3c4a284e30164764"
|
6
|
+
}
|
@@ -54,7 +54,7 @@ module AmoWebMock
|
|
54
54
|
.to_return(
|
55
55
|
status: 200,
|
56
56
|
body: File.read('./spec/fixtures/authorize.json'),
|
57
|
-
headers: {}
|
57
|
+
headers: { 'Content-Type' => 'application/json' }
|
58
58
|
)
|
59
59
|
end
|
60
60
|
|
@@ -65,8 +65,8 @@ module AmoWebMock
|
|
65
65
|
)
|
66
66
|
.to_return(
|
67
67
|
status: 200,
|
68
|
-
body: File.read('./spec/fixtures/
|
69
|
-
headers: {}
|
68
|
+
body: File.read('./spec/fixtures/authorize_refresh_token.json'),
|
69
|
+
headers: { 'Content-Type' => 'application/json' }
|
70
70
|
)
|
71
71
|
end
|
72
72
|
|
data/spec/my_contact_spec.rb
CHANGED
@@ -14,7 +14,7 @@ describe MyContact do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
describe "#params" do
|
17
|
-
let(:
|
17
|
+
let(:contact) do
|
18
18
|
described_class.new(
|
19
19
|
name: 'Test inc',
|
20
20
|
phone: '12345678',
|
@@ -23,7 +23,7 @@ describe MyContact do
|
|
23
23
|
)
|
24
24
|
end
|
25
25
|
|
26
|
-
subject {
|
26
|
+
subject { contact.params }
|
27
27
|
|
28
28
|
specify { is_expected.to include(:last_modified) }
|
29
29
|
specify { is_expected.to include(name: 'Test inc') }
|
@@ -43,6 +43,7 @@ describe MyContact do
|
|
43
43
|
expect(obj.id).to eq 11
|
44
44
|
expect(obj.company_name).to eq "Foo Inc."
|
45
45
|
expect(obj.email).to eq "foo@tb.com"
|
46
|
+
expect(obj.phone).to eq ["1111 111 111", "2222 222 222"]
|
46
47
|
expect(obj.teachbase_id).to eq 1123
|
47
48
|
expect(obj.params[:id]).to eq 11
|
48
49
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -35,7 +35,7 @@ describe Amorail::StoreAdapters::MemoryStoreAdapter do
|
|
35
35
|
context 'when token is expired' do
|
36
36
|
it 'returns blank hash' do
|
37
37
|
store.persist_access('secret', 'token', 'refresh_token', Time.now.to_i - 10_000)
|
38
|
-
expect(store.fetch_access('secret')).to eq(
|
38
|
+
expect(store.fetch_access('secret')).to eq(refresh_token: 'refresh_token')
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
data/spec/support/my_contact.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amorail
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alekseenkoss
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-01-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '2.3'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
27
|
+
version: '2.3'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -85,16 +85,16 @@ dependencies:
|
|
85
85
|
name: shoulda-matchers
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
|
-
- - "
|
88
|
+
- - ">="
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version: '
|
90
|
+
version: '0'
|
91
91
|
type: :development
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
|
-
- - "
|
95
|
+
- - ">="
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version: '
|
97
|
+
version: '0'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: rubocop
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
@@ -211,6 +211,8 @@ files:
|
|
211
211
|
- RELEASING.md
|
212
212
|
- Rakefile
|
213
213
|
- amorail.gemspec
|
214
|
+
- dip.yml
|
215
|
+
- docker-compose.yaml
|
214
216
|
- lib/amorail.rb
|
215
217
|
- lib/amorail/access_token.rb
|
216
218
|
- lib/amorail/client.rb
|
@@ -247,6 +249,7 @@ files:
|
|
247
249
|
- spec/fixtures/accounts/response_2.json
|
248
250
|
- spec/fixtures/amorail_test.yml
|
249
251
|
- spec/fixtures/authorize.json
|
252
|
+
- spec/fixtures/authorize_refresh_token.json
|
250
253
|
- spec/fixtures/contacts/create.json
|
251
254
|
- spec/fixtures/contacts/find_many.json
|
252
255
|
- spec/fixtures/contacts/find_one.json
|
@@ -295,7 +298,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
295
298
|
- !ruby/object:Gem::Version
|
296
299
|
version: '0'
|
297
300
|
requirements: []
|
298
|
-
rubygems_version: 3.
|
301
|
+
rubygems_version: 3.3.7
|
299
302
|
signing_key:
|
300
303
|
specification_version: 4
|
301
304
|
summary: Ruby API client for AmoCRM
|
@@ -310,6 +313,7 @@ test_files:
|
|
310
313
|
- spec/fixtures/accounts/response_2.json
|
311
314
|
- spec/fixtures/amorail_test.yml
|
312
315
|
- spec/fixtures/authorize.json
|
316
|
+
- spec/fixtures/authorize_refresh_token.json
|
313
317
|
- spec/fixtures/contacts/create.json
|
314
318
|
- spec/fixtures/contacts/find_many.json
|
315
319
|
- spec/fixtures/contacts/find_one.json
|