amorail 0.3.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +19 -1
  3. data/README.md +7 -1
  4. data/Rakefile +7 -3
  5. data/amorail.gemspec +4 -3
  6. data/lib/amorail.rb +3 -0
  7. data/lib/amorail/client.rb +8 -4
  8. data/lib/amorail/config.rb +2 -0
  9. data/lib/amorail/entities/company.rb +2 -0
  10. data/lib/amorail/entities/contact.rb +3 -0
  11. data/lib/amorail/entities/contact_link.rb +2 -0
  12. data/lib/amorail/entities/elementable.rb +39 -0
  13. data/lib/amorail/entities/lead.rb +4 -1
  14. data/lib/amorail/entities/leadable.rb +3 -0
  15. data/lib/amorail/entities/note.rb +19 -0
  16. data/lib/amorail/entities/task.rb +11 -23
  17. data/lib/amorail/entities/webhook.rb +44 -0
  18. data/lib/amorail/entity.rb +17 -9
  19. data/lib/amorail/entity/finders.rb +19 -14
  20. data/lib/amorail/entity/params.rb +2 -0
  21. data/lib/amorail/entity/{persistance.rb → persistence.rb} +24 -0
  22. data/lib/amorail/exceptions.rb +2 -0
  23. data/lib/amorail/property.rb +8 -0
  24. data/lib/amorail/railtie.rb +4 -1
  25. data/lib/amorail/version.rb +3 -1
  26. data/lib/tasks/amorail.rake +2 -0
  27. data/spec/client_spec.rb +2 -0
  28. data/spec/company_spec.rb +2 -0
  29. data/spec/contact_link_spec.rb +2 -0
  30. data/spec/contact_spec.rb +17 -0
  31. data/spec/entity_spec.rb +2 -0
  32. data/spec/fixtures/{account_response.json → accounts/response_1.json} +5 -5
  33. data/spec/fixtures/{account2_response.json → accounts/response_2.json} +1 -1
  34. data/spec/fixtures/{contact_create.json → contacts/create.json} +1 -1
  35. data/spec/fixtures/{contact_find_query.json → contacts/find_many.json} +3 -5
  36. data/spec/fixtures/{contact_find.json → contacts/find_one.json} +5 -6
  37. data/spec/fixtures/contacts/links.json +16 -0
  38. data/spec/fixtures/{my_contact_find.json → contacts/my_contact_find.json} +2 -3
  39. data/spec/fixtures/contacts/update.json +13 -0
  40. data/spec/fixtures/leads/create.json +13 -0
  41. data/spec/fixtures/leads/find_many.json +73 -0
  42. data/spec/fixtures/leads/links.json +16 -0
  43. data/spec/fixtures/leads/update.json +13 -0
  44. data/spec/fixtures/leads/update_errors.json +12 -0
  45. data/spec/fixtures/webhooks/list.json +24 -0
  46. data/spec/fixtures/webhooks/subscribe.json +17 -0
  47. data/spec/fixtures/webhooks/unsubscribe.json +17 -0
  48. data/spec/helpers/webmock_helpers.rb +92 -13
  49. data/spec/lead_spec.rb +30 -0
  50. data/spec/my_contact_spec.rb +2 -0
  51. data/spec/note_spec.rb +28 -0
  52. data/spec/property_spec.rb +2 -0
  53. data/spec/spec_helper.rb +4 -2
  54. data/spec/support/elementable_example.rb +54 -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 +8 -28
  60. data/spec/webhook_spec.rb +61 -0
  61. metadata +60 -33
  62. data/.hound.yml +0 -12
  63. data/spec/fixtures/contact_update.json +0 -5
  64. data/spec/fixtures/contacts_links.json +0 -15
  65. data/spec/fixtures/leads.json +0 -69
  66. data/spec/fixtures/leads_links.json +0 -15
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/hash/indifferent_access"
2
4
 
3
5
  module Amorail # :nodoc: all
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Amorail # :nodoc: all
2
4
  class Entity
3
5
  class InvalidRecord < ::Amorail::Error; end
@@ -13,6 +15,7 @@ module Amorail # :nodoc: all
13
15
 
14
16
  def save
15
17
  return false unless valid?
18
+
16
19
  new_record? ? push('add') : push('update')
17
20
  end
18
21
 
@@ -22,6 +25,7 @@ module Amorail # :nodoc: all
22
25
 
23
26
  def update(attrs = {})
24
27
  return false if new_record?
28
+
25
29
  merge_params(attrs)
26
30
  push('update')
27
31
  end
@@ -32,6 +36,7 @@ module Amorail # :nodoc: all
32
36
 
33
37
  def reload
34
38
  fail NotPersisted if id.nil?
39
+
35
40
  load_record(id)
36
41
  end
37
42
 
@@ -40,5 +45,24 @@ module Amorail # :nodoc: all
40
45
  def extract_data_add(response)
41
46
  response.fetch('add').first
42
47
  end
48
+
49
+ # Update response can have status 200 and contain errors.
50
+ # In case of errors "update" key in a response is a Hash with "errors" key.
51
+ # If there are no errors "update" key is an Array with entities attributes.
52
+ def extract_data_update(response)
53
+ case data = response.fetch('update')
54
+ when Array
55
+ data.first
56
+ when Hash
57
+ merge_errors(data)
58
+ raise(InvalidRecord)
59
+ end
60
+ end
61
+
62
+ def merge_errors(data)
63
+ data.fetch("errors").each do |_, message|
64
+ errors.add(:base, message)
65
+ end
66
+ end
43
67
  end
44
68
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Amorail Exceptions.
2
4
  # Every class is name of HTTP response error code(status)
3
5
  module Amorail
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Amorail
2
4
  # Return hash key as method call
3
5
  module MethodMissing
@@ -8,6 +10,10 @@ module Amorail
8
10
  super
9
11
  end
10
12
  end
13
+
14
+ def respond_to_missing?(method_sym, *args)
15
+ args.size.zero? && data.key?(method_sym.to_s)
16
+ end
11
17
  end
12
18
 
13
19
  class Property # :nodoc: all
@@ -22,6 +28,7 @@ module Amorail
22
28
  data['custom_fields'].fetch(source_name, []).each do |contact|
23
29
  identifier = contact['code'].presence || contact['name'].presence
24
30
  next if identifier.nil?
31
+
25
32
  hash[identifier.downcase] = PropertyItem.new(contact)
26
33
  end
27
34
  new hash
@@ -114,6 +121,7 @@ module Amorail
114
121
  prop_item = PropertyItem.new(tt)
115
122
  identifier = tt['code'].presence || tt['name'].presence
116
123
  next if identifier.nil?
124
+
117
125
  hash[identifier.downcase] = prop_item
118
126
  hash[identifier] = prop_item
119
127
  end
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Amorail
4
+ # Add amorail rake tasks
2
5
  class Railtie < Rails::Railtie
3
6
  rake_tasks do
4
- load File.expand_path('../../tasks/amorail.rake', __FILE__)
7
+ load File.expand_path('../tasks/amorail.rake', __dir__)
5
8
  end
6
9
  end
7
10
  end
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Amorail version
2
4
  module Amorail
3
- VERSION = "0.3.4".freeze
5
+ VERSION = '0.6.0'
4
6
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  namespace :amorail do
2
4
  desc 'Check Amorail configuration'
3
5
  task :check do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Amorail::Client do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Amorail::Company do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Amorail::ContactLink do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Amorail::Contact do
@@ -136,6 +138,21 @@ describe Amorail::Contact do
136
138
  end
137
139
  end
138
140
 
141
+ describe ".where" do
142
+ before { contacts_where_stub(Amorail.config.api_endpoint, query: 'xxx', limit_rows: 10, limit_offset: 100) }
143
+
144
+ it "loads entities" do
145
+ res = described_class.where(query: 'xxx', limit_rows: 10, limit_offset: 100)
146
+ expect(res.size).to eq 2
147
+ expect(res.first.id).to eq 101
148
+ expect(res.last.id).to eq 102
149
+ expect(res.first.company_name).to eq "Foo Inc."
150
+ expect(res.last.email).to eq "foo2@tb.com"
151
+ expect(res.first.phone).to eq "1111 111 111"
152
+ expect(res.first.params[:id]).to eq 101
153
+ end
154
+ end
155
+
139
156
  describe "#save" do
140
157
  before { contact_create_stub(Amorail.config.api_endpoint) }
141
158
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe MyEntity do
@@ -141,9 +141,9 @@
141
141
  }
142
142
  },
143
143
  {
144
- "id": "116302",
145
- "name": "teachbase_id",
146
- "multiple": "N",
144
+ "id": "116302",
145
+ "name": "teachbase_id",
146
+ "multiple": "N",
147
147
  "type_id": "8"
148
148
  }
149
149
  ],
@@ -162,7 +162,7 @@
162
162
  "multiple": "N",
163
163
  "type_id": "3"
164
164
  }
165
- ],
165
+ ],
166
166
  "companies": [
167
167
  {
168
168
  "id": "1460589",
@@ -341,4 +341,4 @@
341
341
  },
342
342
  "server_time": 1422442143
343
343
  }
344
- }
344
+ }
@@ -192,4 +192,4 @@
192
192
  },
193
193
  "server_time": 1422442143
194
194
  }
195
- }
195
+ }
@@ -10,4 +10,4 @@
10
10
  },
11
11
  "server_time": 1423139130
12
12
  }
13
- }
13
+ }
@@ -7,8 +7,7 @@
7
7
  "account_id": "8195968",
8
8
  "last_modified": 1423139130,
9
9
  "company_name": "Foo Inc.",
10
- "custom_fields":
11
- [
10
+ "custom_fields": [
12
11
  {
13
12
  "id": "1460591",
14
13
  "name": "Email",
@@ -39,8 +38,7 @@
39
38
  "account_id": "8195968",
40
39
  "last_modified": 1423139150,
41
40
  "company_name": "Foo Inc.",
42
- "custom_fields":
43
- [
41
+ "custom_fields": [
44
42
  {
45
43
  "id": "1460591",
46
44
  "name": "Email",
@@ -56,4 +54,4 @@
56
54
  }
57
55
  ]
58
56
  }
59
- }
57
+ }
@@ -7,12 +7,11 @@
7
7
  "account_id": "8195968",
8
8
  "last_modified": 1423139130,
9
9
  "company_name": "Foo Inc.",
10
- "linked_leads_id": [
11
- "1872746",
12
- "1885024"
10
+ "linked_leads_id": [
11
+ "1872746",
12
+ "1885024"
13
13
  ],
14
- "custom_fields":
15
- [
14
+ "custom_fields": [
16
15
  {
17
16
  "id": "1460591",
18
17
  "name": "Email",
@@ -39,4 +38,4 @@
39
38
  }
40
39
  ]
41
40
  }
42
- }
41
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "response": {
3
+ "links": [
4
+ {
5
+ "contact_id": "101",
6
+ "lead_id": "1",
7
+ "last_modified": 1374741830
8
+ },
9
+ {
10
+ "contact_id": "101",
11
+ "lead_id": "2",
12
+ "last_modified": 1374839942
13
+ }
14
+ ]
15
+ }
16
+ }
@@ -7,8 +7,7 @@
7
7
  "account_id": "8195968",
8
8
  "last_modified": 1423139130,
9
9
  "company_name": "Foo Inc.",
10
- "custom_fields":
11
- [
10
+ "custom_fields": [
12
11
  {
13
12
  "id": "1460591",
14
13
  "name": "Email",
@@ -45,4 +44,4 @@
45
44
  }
46
45
  ]
47
46
  }
48
- }
47
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "response": {
3
+ "contacts": {
4
+ "update": [
5
+ {
6
+ "id": 12509853,
7
+ "last_modified": 1501256276
8
+ }
9
+ ]
10
+ },
11
+ "server_time": 1501452197
12
+ }
13
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "response": {
3
+ "leads": {
4
+ "add": [
5
+ {
6
+ "id": 3980037,
7
+ "request_id": 0
8
+ }
9
+ ]
10
+ },
11
+ "server_time": 1502394640
12
+ }
13
+ }
@@ -0,0 +1,73 @@
1
+ {
2
+ "response": {
3
+ "leads": [
4
+ {
5
+ "id": "1",
6
+ "name": "Research new technologies",
7
+ "last_modified": 1374656336,
8
+ "status_id": "7046196",
9
+ "pipeline_id": 683506,
10
+ "price": "500000",
11
+ "responsible_user_id": "103586",
12
+ "tags": [
13
+ {
14
+ "id": "960472",
15
+ "name": "USA"
16
+ },
17
+ {
18
+ "id": "960854",
19
+ "name": "Lead"
20
+ }
21
+ ],
22
+ "date_create": 1386014400,
23
+ "account_id": "7046192",
24
+ "created_user_id": "4502311",
25
+ "custom_fields": [
26
+ {
27
+ "id": "484604",
28
+ "name": "field",
29
+ "values": [
30
+ {
31
+ "value": "text"
32
+ }
33
+ ]
34
+ }
35
+ ]
36
+ },
37
+ {
38
+ "id": "2",
39
+ "name": "Sell it!",
40
+ "last_modified": 1374656336,
41
+ "status_id": "7046196",
42
+ "pipeline_id": 683506,
43
+ "price": "100000",
44
+ "responsible_user_id": "103586",
45
+ "tags": [
46
+ {
47
+ "id": "960472",
48
+ "name": "USA"
49
+ },
50
+ {
51
+ "id": "960854",
52
+ "name": "Lead"
53
+ }
54
+ ],
55
+ "date_create": 1386014400,
56
+ "account_id": "7046192",
57
+ "created_user_id": "4502311",
58
+ "custom_fields": [
59
+ {
60
+ "id": "484604",
61
+ "name": "field",
62
+ "values": [
63
+ {
64
+ "value": "text"
65
+ }
66
+ ]
67
+ }
68
+ ]
69
+ }
70
+ ],
71
+ "server_time": 1374839787
72
+ }
73
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "response": {
3
+ "links": [
4
+ {
5
+ "contact_id": "101",
6
+ "lead_id": "2",
7
+ "last_modified": 1374839942
8
+ },
9
+ {
10
+ "contact_id": "102",
11
+ "lead_id": "2",
12
+ "last_modified": 1374839942
13
+ }
14
+ ]
15
+ }
16
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "response": {
3
+ "leads": {
4
+ "update": [
5
+ {
6
+ "id": 3103031,
7
+ "last_modified": 1502393989
8
+ }
9
+ ]
10
+ },
11
+ "server_time": 1502393993
12
+ }
13
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "response": {
3
+ "leads": {
4
+ "update": {
5
+ "errors": {
6
+ "3103031": "Last modified date is older than in database"
7
+ }
8
+ }
9
+ },
10
+ "server_time": 1502391257
11
+ }
12
+ }