amorail 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 42a2851b1c9adb5f49a331d622c3fd276c63761b
4
- data.tar.gz: 8012dce427d24125bfeb3a10059033c577bcbd5b
2
+ SHA256:
3
+ metadata.gz: f65fb80eb5f426c50e32fa474252e764e42e3887cddbf7f735ad217ad98560cf
4
+ data.tar.gz: 5f44b5e558be01b346ad68d4d84c903deec88e05514430531b6aa0b6e02c3e73
5
5
  SHA512:
6
- metadata.gz: a102ba6296a1e3ed6ab4f2c4160415429f351398e83240d0d2453a72c6302eefaf83c42624b43f037e00fb7dbc40f669f37ea27bd32d6311fe5d250a5b0dc248
7
- data.tar.gz: b7214099abe616f5902717c4f823cabab5e818e84752cf649330cf70191c8b0b6488495f1b4e26fd7afcc27c5712d2431ef728356daefd5fd53a6d03b6b3af2b
6
+ metadata.gz: 14958c70d64dda63dd82c334d08b2c63cbe8183fbffc8b5c5a63893e42280b0439e8eef4411b9b222c52e54d7dfa0949e82061de6feb4b3814ad169d5ab37cc6
7
+ data.tar.gz: a370262be82cdde926d639976fea7e23833a6f999cff3100c2a5df57e9962c8ea05b5e6b682e4d2738b4f322a3cba73d81195e721eefe017e8717c103a665f2e
@@ -56,3 +56,6 @@ Style/SignalException:
56
56
 
57
57
  Layout/MultilineMethodCallBraceLayout:
58
58
  Enabled: false
59
+
60
+ Lint/MissingCopEnableDirective:
61
+ Enabled: false
@@ -22,6 +22,7 @@ module Amorail
22
22
 
23
23
  def company
24
24
  return if linked_company_id.nil?
25
+
25
26
  @company ||= Amorail::Company.find(linked_company_id)
26
27
  end
27
28
  end
@@ -15,6 +15,7 @@ module Amorail
15
15
  # Return list of associated contacts
16
16
  def contacts
17
17
  fail NotPersisted if id.nil?
18
+
18
19
  @contacts ||=
19
20
  begin
20
21
  links = Amorail::ContactLink.find_by_leads(id)
@@ -22,6 +22,7 @@ module Amorail
22
22
  # Return all linked leads
23
23
  def leads
24
24
  return [] if linked_leads_id.empty?
25
+
25
26
  @leads ||= Amorail::Lead.find_all(linked_leads_id)
26
27
  end
27
28
  end
@@ -0,0 +1,42 @@
1
+ module Amorail
2
+ # AmoCRM webhook entity
3
+ class Webhook < Entity
4
+ amo_names 'webhooks'
5
+
6
+ amo_field :id, :url, :events, :disabled
7
+
8
+ def self.list
9
+ response = client.safe_request(:get, remote_url('list'))
10
+
11
+ return [] if response.body.blank?
12
+
13
+ response.body['response'].fetch(amo_response_name, []).map do |attributes|
14
+ new.reload_model(attributes)
15
+ end
16
+ end
17
+
18
+ def self.subscribe(webhooks)
19
+ perform_webhooks_request('subscribe', webhooks) do |data|
20
+ data.map { |attrs| new.reload_model(attrs) }
21
+ end
22
+ end
23
+
24
+ def self.unsubscribe(webhooks)
25
+ perform_webhooks_request('unsubscribe', webhooks)
26
+ end
27
+
28
+ def self.perform_webhooks_request(action, webhooks, &block)
29
+ response = client.safe_request(
30
+ :post,
31
+ remote_url(action),
32
+ request: { webhooks: { action => webhooks } }
33
+ )
34
+
35
+ return response unless block
36
+
37
+ block.call(response.body['response'].dig(amo_response_name, 'subscribe'))
38
+ end
39
+
40
+ private_class_method :perform_webhooks_request
41
+ end
42
+ end
@@ -77,6 +77,7 @@ module Amorail
77
77
  attrs.each do |k, v|
78
78
  action = "#{k}="
79
79
  next unless respond_to?(action)
80
+
80
81
  send(action, v)
81
82
  end
82
83
  self
@@ -84,9 +85,11 @@ module Amorail
84
85
 
85
86
  def merge_custom_fields(fields)
86
87
  return if fields.nil?
88
+
87
89
  fields.each do |f|
88
90
  fname = f['code'] || f['name']
89
91
  next if fname.nil?
92
+
90
93
  fname = "#{fname.downcase}="
91
94
  fval = f.fetch('values').first.fetch('value')
92
95
  send(fname, fval) if respond_to?(fname)
@@ -119,7 +122,7 @@ module Amorail
119
122
  )
120
123
  reload_model(data)
121
124
  rescue InvalidRecord
122
- return false
125
+ false
123
126
  end
124
127
  end
125
128
  end
@@ -11,6 +11,7 @@ module Amorail # :nodoc: all
11
11
  def find!(id)
12
12
  rec = find(id)
13
13
  fail RecordNotFound unless rec
14
+
14
15
  rec
15
16
  end
16
17
 
@@ -32,8 +33,8 @@ module Amorail # :nodoc: all
32
33
 
33
34
  # Find AMO entities by query
34
35
  # Returns array of matching entities.
35
- def find_by_query(q)
36
- where(query: q)
36
+ def find_by_query(query)
37
+ where(query: query)
37
38
  end
38
39
 
39
40
  private
@@ -13,6 +13,7 @@ module Amorail # :nodoc: all
13
13
 
14
14
  def save
15
15
  return false unless valid?
16
+
16
17
  new_record? ? push('add') : push('update')
17
18
  end
18
19
 
@@ -22,6 +23,7 @@ module Amorail # :nodoc: all
22
23
 
23
24
  def update(attrs = {})
24
25
  return false if new_record?
26
+
25
27
  merge_params(attrs)
26
28
  push('update')
27
29
  end
@@ -32,6 +34,7 @@ module Amorail # :nodoc: all
32
34
 
33
35
  def reload
34
36
  fail NotPersisted if id.nil?
37
+
35
38
  load_record(id)
36
39
  end
37
40
 
@@ -26,6 +26,7 @@ module Amorail
26
26
  data['custom_fields'].fetch(source_name, []).each do |contact|
27
27
  identifier = contact['code'].presence || contact['name'].presence
28
28
  next if identifier.nil?
29
+
29
30
  hash[identifier.downcase] = PropertyItem.new(contact)
30
31
  end
31
32
  new hash
@@ -118,6 +119,7 @@ module Amorail
118
119
  prop_item = PropertyItem.new(tt)
119
120
  identifier = tt['code'].presence || tt['name'].presence
120
121
  next if identifier.nil?
122
+
121
123
  hash[identifier.downcase] = prop_item
122
124
  hash[identifier] = prop_item
123
125
  end
@@ -2,7 +2,7 @@ module Amorail
2
2
  # Add amorail rake tasks
3
3
  class Railtie < Rails::Railtie
4
4
  rake_tasks do
5
- load File.expand_path('../../tasks/amorail.rake', __FILE__)
5
+ load File.expand_path('../tasks/amorail.rake', __dir__)
6
6
  end
7
7
  end
8
8
  end
@@ -1,4 +1,4 @@
1
1
  # Amorail version
2
2
  module Amorail
3
- VERSION = "0.4.0".freeze
3
+ VERSION = "0.5.0".freeze
4
4
  end
@@ -0,0 +1,24 @@
1
+ {
2
+ "response": {
3
+ "webhooks": [
4
+ {
5
+ "id": "1",
6
+ "url": "http://example.org",
7
+ "events": [
8
+ "add_contact"
9
+ ],
10
+ "disabled": false
11
+ },
12
+ {
13
+ "id": "2",
14
+ "url": "http://example.com",
15
+ "events": [
16
+ "add_contact",
17
+ "add_company"
18
+ ],
19
+ "disabled": true
20
+ }
21
+ ],
22
+ "server_time": 1539938502
23
+ }
24
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "response": {
3
+ "webhooks": {
4
+ "subscribe": [
5
+ {
6
+ "url": "http://example.org",
7
+ "result": true
8
+ },
9
+ {
10
+ "url": "http://example.com",
11
+ "result": true
12
+ }
13
+ ]
14
+ },
15
+ "server_time": 1539941636
16
+ }
17
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "response": {
3
+ "webhooks": {
4
+ "unsubscribe": [
5
+ {
6
+ "url": "http://example.org",
7
+ "result": true
8
+ },
9
+ {
10
+ "url": "http://example.com",
11
+ "result": true
12
+ }
13
+ ]
14
+ },
15
+ "server_time": 1539941911
16
+ }
17
+ }
@@ -246,4 +246,34 @@ module AmoWebMock
246
246
  .to_return(status: 204)
247
247
  end
248
248
  end
249
+
250
+ def webhooks_list_stub(endpoint, empty: false)
251
+ body = empty ? '' : File.read('./spec/fixtures/webhooks/list.json')
252
+ stub_request(:get, "#{endpoint}/private/api/v2/json/webhooks/list")
253
+ .to_return(
254
+ body: body,
255
+ headers: { 'Content-Type' => 'application/json' },
256
+ status: 200
257
+ )
258
+ end
259
+
260
+ def webhooks_subscribe_stub(endpoint, webhooks)
261
+ stub_request(:post, "#{endpoint}/private/api/v2/json/webhooks/subscribe")
262
+ .with(body: { request: { webhooks: { subscribe: webhooks } } }.to_json)
263
+ .to_return(
264
+ body: File.read('./spec/fixtures/webhooks/subscribe.json'),
265
+ headers: { 'Content-Type' => 'application/json' },
266
+ status: 200
267
+ )
268
+ end
269
+
270
+ def webhooks_unsubscribe_stub(endpoint, webhooks)
271
+ stub_request(:post, "#{endpoint}/private/api/v2/json/webhooks/unsubscribe")
272
+ .with(body: { request: { webhooks: { unsubscribe: webhooks } } }.to_json)
273
+ .to_return(
274
+ body: File.read('./spec/fixtures/webhooks/unsubscribe.json'),
275
+ headers: { 'Content-Type' => 'application/json' },
276
+ status: 200
277
+ )
278
+ end
249
279
  end
@@ -10,9 +10,9 @@ require 'helpers/webmock_helpers'
10
10
 
11
11
  # Cleanup Amorail env
12
12
  ENV.delete_if { |k, _| k =~ /amorail/i }
13
- ENV["AMORAIL_CONF"] = File.expand_path("../fixtures/amorail_test.yml", __FILE__)
13
+ ENV["AMORAIL_CONF"] = File.expand_path("fixtures/amorail_test.yml", __dir__)
14
14
 
15
- Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require f }
15
+ Dir[File.expand_path("support/**/*.rb", __dir__)].each { |f| require f }
16
16
 
17
17
  RSpec.configure do |config|
18
18
  config.mock_with :rspec
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Amorail::Webhook do
4
+ before { mock_api }
5
+
6
+ describe '.list' do
7
+ context 'there are some webhooks' do
8
+ before { webhooks_list_stub(Amorail.config.api_endpoint) }
9
+
10
+ it 'loads webhooks' do
11
+ res = described_class.list
12
+ expect(res.size).to eq 2
13
+ expect(res.first.id).to eq '1'
14
+ expect(res.first.url).to eq 'http://example.org'
15
+ expect(res.first.events).to eq ['add_contact']
16
+ expect(res.first.disabled).to eq false
17
+ expect(res.last.id).to eq '2'
18
+ expect(res.last.url).to eq 'http://example.com'
19
+ expect(res.last.events).to eq ['add_contact', 'add_company']
20
+ expect(res.last.disabled).to eq true
21
+ end
22
+ end
23
+
24
+ context 'there are not any webhooks' do
25
+ before { webhooks_list_stub(Amorail.config.api_endpoint, empty: true) }
26
+
27
+ it 'returns an empty array' do
28
+ res = described_class.list
29
+ expect(res).to eq []
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '.subscribe' do
35
+ it 'creates webhooks' do
36
+ webhooks = [
37
+ { url: 'http://example.org', events: ['add_contact'] },
38
+ { url: 'http://example.com', events: ['add_contact', 'add_company'] }
39
+ ]
40
+ stub = webhooks_subscribe_stub(Amorail.config.api_endpoint, webhooks)
41
+ res = described_class.subscribe(webhooks)
42
+ expect(stub).to have_been_requested
43
+ expect(res.first.url).to eq 'http://example.org'
44
+ expect(res.last.url).to eq 'http://example.com'
45
+ end
46
+ end
47
+
48
+ describe '.unsubscribe' do
49
+ it 'removes webhooks' do
50
+ webhooks = [
51
+ { url: 'http://example.org', events: ['add_contact'] },
52
+ { url: 'http://example.com', events: ['add_contact', 'add_company'] }
53
+ ]
54
+ stub = webhooks_unsubscribe_stub(Amorail.config.api_endpoint, webhooks)
55
+ described_class.unsubscribe(webhooks)
56
+ expect(stub).to have_been_requested
57
+ end
58
+ end
59
+ end
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.4.0
4
+ version: 0.5.0
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: 2017-08-14 00:00:00.000000000 Z
12
+ date: 2018-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -212,6 +212,7 @@ files:
212
212
  - lib/amorail/entities/leadable.rb
213
213
  - lib/amorail/entities/note.rb
214
214
  - lib/amorail/entities/task.rb
215
+ - lib/amorail/entities/webhook.rb
215
216
  - lib/amorail/entity.rb
216
217
  - lib/amorail/entity/finders.rb
217
218
  - lib/amorail/entity/params.rb
@@ -240,6 +241,9 @@ files:
240
241
  - spec/fixtures/leads/links.json
241
242
  - spec/fixtures/leads/update.json
242
243
  - spec/fixtures/leads/update_errors.json
244
+ - spec/fixtures/webhooks/list.json
245
+ - spec/fixtures/webhooks/subscribe.json
246
+ - spec/fixtures/webhooks/unsubscribe.json
243
247
  - spec/helpers/webmock_helpers.rb
244
248
  - spec/lead_spec.rb
245
249
  - spec/my_contact_spec.rb
@@ -252,6 +256,7 @@ files:
252
256
  - spec/support/my_contact.rb
253
257
  - spec/support/my_entity.rb
254
258
  - spec/task_spec.rb
259
+ - spec/webhook_spec.rb
255
260
  homepage: ''
256
261
  licenses:
257
262
  - MIT
@@ -272,7 +277,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
272
277
  version: '0'
273
278
  requirements: []
274
279
  rubyforge_project:
275
- rubygems_version: 2.6.12
280
+ rubygems_version: 2.7.7
276
281
  signing_key:
277
282
  specification_version: 4
278
283
  summary: Ruby API client for AmoCRM
@@ -296,6 +301,9 @@ test_files:
296
301
  - spec/fixtures/leads/links.json
297
302
  - spec/fixtures/leads/update.json
298
303
  - spec/fixtures/leads/update_errors.json
304
+ - spec/fixtures/webhooks/list.json
305
+ - spec/fixtures/webhooks/subscribe.json
306
+ - spec/fixtures/webhooks/unsubscribe.json
299
307
  - spec/helpers/webmock_helpers.rb
300
308
  - spec/lead_spec.rb
301
309
  - spec/my_contact_spec.rb
@@ -308,3 +316,4 @@ test_files:
308
316
  - spec/support/my_contact.rb
309
317
  - spec/support/my_entity.rb
310
318
  - spec/task_spec.rb
319
+ - spec/webhook_spec.rb