amorail 0.4.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/rspec.yml +23 -0
  3. data/.gitignore +2 -1
  4. data/.rubocop.yml +3 -0
  5. data/CHANGELOG.md +19 -0
  6. data/README.md +49 -8
  7. data/RELEASING.md +43 -0
  8. data/amorail.gemspec +5 -3
  9. data/lib/amorail.rb +27 -6
  10. data/lib/amorail/access_token.rb +44 -0
  11. data/lib/amorail/client.rb +84 -23
  12. data/lib/amorail/config.rb +14 -8
  13. data/lib/amorail/entities/company.rb +2 -0
  14. data/lib/amorail/entities/contact.rb +3 -0
  15. data/lib/amorail/entities/contact_link.rb +2 -0
  16. data/lib/amorail/entities/elementable.rb +4 -2
  17. data/lib/amorail/entities/lead.rb +3 -0
  18. data/lib/amorail/entities/leadable.rb +3 -0
  19. data/lib/amorail/entities/note.rb +2 -0
  20. data/lib/amorail/entities/task.rb +2 -0
  21. data/lib/amorail/entities/webhook.rb +44 -0
  22. data/lib/amorail/entity.rb +17 -3
  23. data/lib/amorail/entity/finders.rb +5 -2
  24. data/lib/amorail/entity/params.rb +3 -2
  25. data/lib/amorail/entity/persistence.rb +5 -0
  26. data/lib/amorail/exceptions.rb +2 -0
  27. data/lib/amorail/property.rb +7 -3
  28. data/lib/amorail/railtie.rb +3 -1
  29. data/lib/amorail/store_adapters.rb +15 -0
  30. data/lib/amorail/store_adapters/abstract_store_adapter.rb +23 -0
  31. data/lib/amorail/store_adapters/memory_store_adapter.rb +50 -0
  32. data/lib/amorail/store_adapters/redis_store_adapter.rb +83 -0
  33. data/lib/amorail/version.rb +3 -1
  34. data/lib/tasks/amorail.rake +2 -0
  35. data/spec/access_token_spec.rb +59 -0
  36. data/spec/client_spec.rb +36 -24
  37. data/spec/company_spec.rb +2 -0
  38. data/spec/contact_link_spec.rb +2 -0
  39. data/spec/contact_spec.rb +2 -0
  40. data/spec/entity_spec.rb +2 -0
  41. data/spec/fixtures/amorail_test.yml +5 -3
  42. data/spec/fixtures/authorize.json +6 -0
  43. data/spec/fixtures/webhooks/list.json +24 -0
  44. data/spec/fixtures/webhooks/subscribe.json +17 -0
  45. data/spec/fixtures/webhooks/unsubscribe.json +17 -0
  46. data/spec/helpers/webmock_helpers.rb +80 -13
  47. data/spec/lead_spec.rb +2 -0
  48. data/spec/my_contact_spec.rb +2 -0
  49. data/spec/note_spec.rb +2 -0
  50. data/spec/property_spec.rb +2 -0
  51. data/spec/spec_helper.rb +4 -2
  52. data/spec/store_adapters/memory_store_adapter_spec.rb +56 -0
  53. data/spec/store_adapters/redis_store_adapter_spec.rb +67 -0
  54. data/spec/support/elementable_example.rb +2 -0
  55. data/spec/support/entity_class_example.rb +2 -0
  56. data/spec/support/leadable_example.rb +2 -0
  57. data/spec/support/my_contact.rb +2 -0
  58. data/spec/support/my_entity.rb +2 -0
  59. data/spec/task_spec.rb +2 -0
  60. data/spec/webhook_spec.rb +61 -0
  61. metadata +48 -17
  62. data/.travis.yml +0 -9
data/spec/lead_spec.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Amorail::Lead do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe MyContact do
data/spec/note_spec.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Amorail::Note do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
  require "webmock/rspec"
3
5
 
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
5
 
@@ -10,9 +12,9 @@ require 'helpers/webmock_helpers'
10
12
 
11
13
  # Cleanup Amorail env
12
14
  ENV.delete_if { |k, _| k =~ /amorail/i }
13
- ENV["AMORAIL_CONF"] = File.expand_path("../fixtures/amorail_test.yml", __FILE__)
15
+ ENV["AMORAIL_CONF"] = File.expand_path("fixtures/amorail_test.yml", __dir__)
14
16
 
15
- Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require f }
17
+ Dir[File.expand_path("support/**/*.rb", __dir__)].each { |f| require f }
16
18
 
17
19
  RSpec.configure do |config|
18
20
  config.mock_with :rspec
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Amorail::StoreAdapters::MemoryStoreAdapter do
6
+ let!(:expiration) { Time.now.to_i + 86_000 }
7
+ let(:store) { Amorail::StoreAdapters.build_by_name(:memory) }
8
+
9
+ describe '#initialize' do
10
+ it 'raises error on unknow option' do
11
+ expect { Amorail::StoreAdapters::MemoryStoreAdapter.new(something: 'something') }.to raise_error(ArgumentError)
12
+ end
13
+ end
14
+
15
+ describe '#persist_access' do
16
+ subject { store.persist_access('secret', 'token', 'refresh_token', expiration) }
17
+
18
+ it 'save record to memory' do
19
+ expect { subject }.to change { store.fetch_access('secret') }.from(
20
+ {}
21
+ ).to(
22
+ { token: 'token', refresh_token: 'refresh_token', expiration: expiration }
23
+ )
24
+ end
25
+ end
26
+
27
+ describe '#fetch_access' do
28
+ context 'when token not expired' do
29
+ it 'returns valid data' do
30
+ store.persist_access('secret', 'token', 'refresh_token', expiration)
31
+ expect(store.fetch_access('secret')).to eq({ token: 'token', refresh_token: 'refresh_token', expiration: expiration })
32
+ end
33
+ end
34
+
35
+ context 'when token is expired' do
36
+ it 'returns blank hash' do
37
+ store.persist_access('secret', 'token', 'refresh_token', Time.now.to_i - 10_000)
38
+ expect(store.fetch_access('secret')).to eq({})
39
+ end
40
+ end
41
+ end
42
+
43
+ describe '#update_access' do
44
+ let!(:upd_expiration) { Time.now.to_i + 92_000 }
45
+ subject { store.update_access('secret', 'upd_token', 'upd_refresh', upd_expiration) }
46
+
47
+ it 'refresh token data' do
48
+ store.persist_access('secret', 'token', 'refresh_token', expiration)
49
+ expect { subject }.to change { store.fetch_access('secret') }.from(
50
+ { token: 'token', refresh_token: 'refresh_token', expiration: expiration }
51
+ ).to(
52
+ { token: 'upd_token', refresh_token: 'upd_refresh', expiration: upd_expiration }
53
+ )
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Amorail::StoreAdapters::RedisStoreAdapter do
6
+ before :each do
7
+ Amorail.config.reload
8
+ end
9
+
10
+ let(:store) { Amorail::StoreAdapters.build_by_name(:redis) }
11
+
12
+ describe '#initialize' do
13
+ it 'raises error on mixed redis options' do
14
+ expect { Amorail::StoreAdapters::RedisStoreAdapter.new(redis_url: 'redis://127.0.0.1:6379/0',
15
+ redis_port: '8082') }.to raise_error(ArgumentError)
16
+ end
17
+
18
+ it 'raises error on unknown options' do
19
+ expect { Amorail::StoreAdapters::RedisStoreAdapter.new(redis_url: 'redis://127.0.0.1:6379/0',
20
+ something: 'smth') }.to raise_error(ArgumentError)
21
+ end
22
+ end
23
+
24
+ describe 'configuration' do
25
+ let(:adapter) { Amorail::StoreAdapters::RedisStoreAdapter.new }
26
+
27
+ it 'set default url' do
28
+ expect(adapter.storage.id).to eq('redis://127.0.0.1:6379/0')
29
+ end
30
+
31
+ it 'set url from env variable' do
32
+ ENV['REDIS_URL'] = 'redis://localhost:2020/'
33
+ adapter = Amorail::StoreAdapters::RedisStoreAdapter.new
34
+ expect(adapter.storage.id).to eq('redis://localhost:2020/0')
35
+
36
+ ENV.delete('REDIS_URL')
37
+ adapter = Amorail::StoreAdapters::RedisStoreAdapter.new
38
+ expect(adapter.storage.id).to eq('redis://127.0.0.1:6379/0')
39
+ end
40
+
41
+ it 'configuration via host post and db' do
42
+ adapter = Amorail::StoreAdapters::RedisStoreAdapter.new(
43
+ redis_host: '127.0.0.2',
44
+ redis_port: '6372',
45
+ redis_db_name: '2'
46
+ )
47
+ expect(adapter.storage.id).to eq('redis://127.0.0.2:6372/2')
48
+ end
49
+
50
+ it 'configuration via host port and db in module' do
51
+ Amorail.config.redis_host = '127.0.0.2'
52
+ Amorail.config.redis_port = '6372'
53
+ Amorail.config.redis_db_name = '2'
54
+ expect(adapter.storage.id).to eq('redis://127.0.0.2:6372/2')
55
+ end
56
+
57
+ it 'configuration via redis url' do
58
+ adapter = Amorail::StoreAdapters::RedisStoreAdapter.new(redis_url: 'redis://127.0.0.2:6322')
59
+ expect(adapter.storage.id).to eq('redis://127.0.0.2:6322/0')
60
+ end
61
+
62
+ it 'configuration via redis url in module' do
63
+ Amorail.config.redis_url = 'redis://127.0.0.2:6322'
64
+ expect(adapter.storage.id).to eq('redis://127.0.0.2:6322/0')
65
+ end
66
+ end
67
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  shared_examples 'elementable' do
2
4
  describe 'validations' do
3
5
  it { is_expected.to validate_presence_of(:element_id) }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  shared_examples 'entity_class' do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  shared_examples 'leadable' do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class MyContact < Amorail::Contact # :nodoc:
2
4
  amo_property :teachbase_id
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # We only need this class to set Amo names for core Entity
2
4
  class MyEntity < Amorail::Entity # :nodoc:
3
5
  amo_names 'entity'
data/spec/task_spec.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Amorail::Task do
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Amorail::Webhook do
6
+ before { mock_api }
7
+
8
+ describe '.list' do
9
+ context 'there are some webhooks' do
10
+ before { webhooks_list_stub(Amorail.config.api_endpoint) }
11
+
12
+ it 'loads webhooks' do
13
+ res = described_class.list
14
+ expect(res.size).to eq 2
15
+ expect(res.first.id).to eq '1'
16
+ expect(res.first.url).to eq 'http://example.org'
17
+ expect(res.first.events).to eq ['add_contact']
18
+ expect(res.first.disabled).to eq false
19
+ expect(res.last.id).to eq '2'
20
+ expect(res.last.url).to eq 'http://example.com'
21
+ expect(res.last.events).to eq ['add_contact', 'add_company']
22
+ expect(res.last.disabled).to eq true
23
+ end
24
+ end
25
+
26
+ context 'there are not any webhooks' do
27
+ before { webhooks_list_stub(Amorail.config.api_endpoint, empty: true) }
28
+
29
+ it 'returns an empty array' do
30
+ res = described_class.list
31
+ expect(res).to eq []
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '.subscribe' do
37
+ it 'creates webhooks' do
38
+ webhooks = [
39
+ { url: 'http://example.org', events: ['add_contact'] },
40
+ { url: 'http://example.com', events: ['add_contact', 'add_company'] }
41
+ ]
42
+ stub = webhooks_subscribe_stub(Amorail.config.api_endpoint, webhooks)
43
+ res = described_class.subscribe(webhooks)
44
+ expect(stub).to have_been_requested
45
+ expect(res.first.url).to eq 'http://example.org'
46
+ expect(res.last.url).to eq 'http://example.com'
47
+ end
48
+ end
49
+
50
+ describe '.unsubscribe' do
51
+ it 'removes webhooks' do
52
+ webhooks = [
53
+ { url: 'http://example.org', events: ['add_contact'] },
54
+ { url: 'http://example.com', events: ['add_contact', 'add_company'] }
55
+ ]
56
+ stub = webhooks_unsubscribe_stub(Amorail.config.api_endpoint, webhooks)
57
+ described_class.unsubscribe(webhooks)
58
+ expect(stub).to have_been_requested
59
+ end
60
+ end
61
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amorail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alekseenkoss
8
8
  - palkan
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-08-14 00:00:00.000000000 Z
12
+ date: 2021-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -113,22 +113,16 @@ dependencies:
113
113
  name: anyway_config
114
114
  requirement: !ruby/object:Gem::Requirement
115
115
  requirements:
116
- - - "~>"
117
- - !ruby/object:Gem::Version
118
- version: '0'
119
116
  - - ">="
120
117
  - !ruby/object:Gem::Version
121
- version: '0.3'
118
+ version: '1.0'
122
119
  type: :runtime
123
120
  prerelease: false
124
121
  version_requirements: !ruby/object:Gem::Requirement
125
122
  requirements:
126
- - - "~>"
127
- - !ruby/object:Gem::Version
128
- version: '0'
129
123
  - - ">="
130
124
  - !ruby/object:Gem::Version
131
- version: '0.3'
125
+ version: '1.0'
132
126
  - !ruby/object:Gem::Dependency
133
127
  name: faraday
134
128
  requirement: !ruby/object:Gem::Requirement
@@ -185,6 +179,20 @@ dependencies:
185
179
  - - ">="
186
180
  - !ruby/object:Gem::Version
187
181
  version: '0'
182
+ - !ruby/object:Gem::Dependency
183
+ name: redis
184
+ requirement: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ type: :runtime
190
+ prerelease: false
191
+ version_requirements: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
188
196
  description: Ruby API client for AmoCRM. You can integrate your system with it.
189
197
  email:
190
198
  - alekseenkoss@gmail.com
@@ -193,15 +201,18 @@ executables: []
193
201
  extensions: []
194
202
  extra_rdoc_files: []
195
203
  files:
204
+ - ".github/workflows/rspec.yml"
196
205
  - ".gitignore"
197
206
  - ".rubocop.yml"
198
- - ".travis.yml"
207
+ - CHANGELOG.md
199
208
  - Gemfile
200
209
  - LICENSE.txt
201
210
  - README.md
211
+ - RELEASING.md
202
212
  - Rakefile
203
213
  - amorail.gemspec
204
214
  - lib/amorail.rb
215
+ - lib/amorail/access_token.rb
205
216
  - lib/amorail/client.rb
206
217
  - lib/amorail/config.rb
207
218
  - lib/amorail/entities/company.rb
@@ -212,6 +223,7 @@ files:
212
223
  - lib/amorail/entities/leadable.rb
213
224
  - lib/amorail/entities/note.rb
214
225
  - lib/amorail/entities/task.rb
226
+ - lib/amorail/entities/webhook.rb
215
227
  - lib/amorail/entity.rb
216
228
  - lib/amorail/entity/finders.rb
217
229
  - lib/amorail/entity/params.rb
@@ -219,8 +231,13 @@ files:
219
231
  - lib/amorail/exceptions.rb
220
232
  - lib/amorail/property.rb
221
233
  - lib/amorail/railtie.rb
234
+ - lib/amorail/store_adapters.rb
235
+ - lib/amorail/store_adapters/abstract_store_adapter.rb
236
+ - lib/amorail/store_adapters/memory_store_adapter.rb
237
+ - lib/amorail/store_adapters/redis_store_adapter.rb
222
238
  - lib/amorail/version.rb
223
239
  - lib/tasks/amorail.rake
240
+ - spec/access_token_spec.rb
224
241
  - spec/client_spec.rb
225
242
  - spec/company_spec.rb
226
243
  - spec/contact_link_spec.rb
@@ -229,6 +246,7 @@ files:
229
246
  - spec/fixtures/accounts/response_1.json
230
247
  - spec/fixtures/accounts/response_2.json
231
248
  - spec/fixtures/amorail_test.yml
249
+ - spec/fixtures/authorize.json
232
250
  - spec/fixtures/contacts/create.json
233
251
  - spec/fixtures/contacts/find_many.json
234
252
  - spec/fixtures/contacts/find_one.json
@@ -240,23 +258,29 @@ files:
240
258
  - spec/fixtures/leads/links.json
241
259
  - spec/fixtures/leads/update.json
242
260
  - spec/fixtures/leads/update_errors.json
261
+ - spec/fixtures/webhooks/list.json
262
+ - spec/fixtures/webhooks/subscribe.json
263
+ - spec/fixtures/webhooks/unsubscribe.json
243
264
  - spec/helpers/webmock_helpers.rb
244
265
  - spec/lead_spec.rb
245
266
  - spec/my_contact_spec.rb
246
267
  - spec/note_spec.rb
247
268
  - spec/property_spec.rb
248
269
  - spec/spec_helper.rb
270
+ - spec/store_adapters/memory_store_adapter_spec.rb
271
+ - spec/store_adapters/redis_store_adapter_spec.rb
249
272
  - spec/support/elementable_example.rb
250
273
  - spec/support/entity_class_example.rb
251
274
  - spec/support/leadable_example.rb
252
275
  - spec/support/my_contact.rb
253
276
  - spec/support/my_entity.rb
254
277
  - spec/task_spec.rb
278
+ - spec/webhook_spec.rb
255
279
  homepage: ''
256
280
  licenses:
257
281
  - MIT
258
282
  metadata: {}
259
- post_install_message:
283
+ post_install_message:
260
284
  rdoc_options: []
261
285
  require_paths:
262
286
  - lib
@@ -264,19 +288,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
264
288
  requirements:
265
289
  - - ">="
266
290
  - !ruby/object:Gem::Version
267
- version: '0'
291
+ version: 2.5.0
268
292
  required_rubygems_version: !ruby/object:Gem::Requirement
269
293
  requirements:
270
294
  - - ">="
271
295
  - !ruby/object:Gem::Version
272
296
  version: '0'
273
297
  requirements: []
274
- rubyforge_project:
275
- rubygems_version: 2.6.12
276
- signing_key:
298
+ rubygems_version: 3.0.8
299
+ signing_key:
277
300
  specification_version: 4
278
301
  summary: Ruby API client for AmoCRM
279
302
  test_files:
303
+ - spec/access_token_spec.rb
280
304
  - spec/client_spec.rb
281
305
  - spec/company_spec.rb
282
306
  - spec/contact_link_spec.rb
@@ -285,6 +309,7 @@ test_files:
285
309
  - spec/fixtures/accounts/response_1.json
286
310
  - spec/fixtures/accounts/response_2.json
287
311
  - spec/fixtures/amorail_test.yml
312
+ - spec/fixtures/authorize.json
288
313
  - spec/fixtures/contacts/create.json
289
314
  - spec/fixtures/contacts/find_many.json
290
315
  - spec/fixtures/contacts/find_one.json
@@ -296,15 +321,21 @@ test_files:
296
321
  - spec/fixtures/leads/links.json
297
322
  - spec/fixtures/leads/update.json
298
323
  - spec/fixtures/leads/update_errors.json
324
+ - spec/fixtures/webhooks/list.json
325
+ - spec/fixtures/webhooks/subscribe.json
326
+ - spec/fixtures/webhooks/unsubscribe.json
299
327
  - spec/helpers/webmock_helpers.rb
300
328
  - spec/lead_spec.rb
301
329
  - spec/my_contact_spec.rb
302
330
  - spec/note_spec.rb
303
331
  - spec/property_spec.rb
304
332
  - spec/spec_helper.rb
333
+ - spec/store_adapters/memory_store_adapter_spec.rb
334
+ - spec/store_adapters/redis_store_adapter_spec.rb
305
335
  - spec/support/elementable_example.rb
306
336
  - spec/support/entity_class_example.rb
307
337
  - spec/support/leadable_example.rb
308
338
  - spec/support/my_contact.rb
309
339
  - spec/support/my_entity.rb
310
340
  - spec/task_spec.rb
341
+ - spec/webhook_spec.rb