amorail 0.2.0 → 0.3.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
2
  SHA1:
3
- metadata.gz: 6a49cf3e73052b401ec558f46cf546671cefd4ba
4
- data.tar.gz: 05204059536051197800da4bd8f784c9dd13b0d2
3
+ metadata.gz: f73b954b7479f45998e9934162fbb77088770d26
4
+ data.tar.gz: 738477b6b61034bc74376805c2801936957616d4
5
5
  SHA512:
6
- metadata.gz: ecdea4ff97ea0018b2b25cce5e049fef9fb8c7ecea9fffe88777f4b25fa0b98378233d8ef3abbd4adf27c29d8ceee205621339e2db4d3697392ea375e63ee484
7
- data.tar.gz: 24379dc6fc1498860dadda55c13ebacf7bba1ccab4b9a058aad304eb22dbd2d64e7d10386e23f3f1d14912b11830c5dd23cd0c62f274f8e7f361a46ccb511cc7
6
+ metadata.gz: 56e949043eb9234d5ee67698c6f6fd46bbca5012bf913487e777122c7e748ea33523e86b80de3ac0b5e0facd9670b01cbf570735a2089518f28e3248b12e0a4e
7
+ data.tar.gz: 50bdcbb2c1280922828b3291901311fcb0eae9a023ccf89f271c6586cf983668237295a9998ae85de3cba43ca55ca2fb41cf357e8830bdae0d3b237bb0b7a50e
data/README.md CHANGED
@@ -211,10 +211,29 @@ rake amorail:check
211
211
  ```
212
212
  Rake task will returns information about properties.
213
213
 
214
+ ### Multiple configurations
215
+
216
+ It is possible to use Amorail with multiple AmoCRM accounts. To do so use `Amorail.with_client` method,
217
+ which receive client params or client instance and a block to execute within custom context:
218
+
219
+ ```ruby
220
+ Amorail.with_client(usermail: "custom@mail.com", api_endpoint: "https://my.acmocrm.ru", api_key: "my_secret_key") do
221
+ # Client specific code here
222
+ end
223
+
224
+ # or using client instance
225
+ my_client = Amorail::Client.new(usermail: "custom@mail.com", api_endpoint: "https://my.acmocrm.ru", api_key: "my_secret_key")
226
+
227
+ Amorail.with_client(client) do
228
+ ...
229
+ end
230
+ ```
231
+
214
232
  ## Contributing
215
233
 
216
234
  1. Fork it ( https://github.com/[my-github-username]/amorail/fork )
217
235
  2. Create your feature branch (`git checkout -b my-new-feature`)
218
236
  3. Commit your changes (`git commit -am 'Add some feature'`)
219
- 4. Push to the branch (`git push origin my-new-feature`)
220
- 5. Create a new Pull Request
237
+ 4. Follow style guides (use Rubocop)
238
+ 5. Push to the branch (`git push origin my-new-feature`)
239
+ 6. Create a new Pull Request
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "rspec", "~> 3.4"
24
24
  spec.add_development_dependency "webmock"
25
25
  spec.add_development_dependency 'pry'
26
26
  spec.add_development_dependency 'shoulda-matchers', "~> 2.0"
@@ -15,7 +15,7 @@ module Amorail
15
15
  end
16
16
 
17
17
  def self.properties
18
- @properties ||= Property.new(client)
18
+ client.properties
19
19
  end
20
20
 
21
21
  def self.configure
@@ -23,7 +23,7 @@ module Amorail
23
23
  end
24
24
 
25
25
  def self.client
26
- @client ||= Client.new
26
+ ClientRegistry.client || (@client ||= Client.new)
27
27
  end
28
28
 
29
29
  def self.reset
@@ -31,5 +31,18 @@ module Amorail
31
31
  @client = nil
32
32
  end
33
33
 
34
+ def self.with_client(client)
35
+ client = Client.new(client) unless client.is_a?(Client)
36
+ ClientRegistry.client = client
37
+ yield
38
+ ClientRegistry.client = nil
39
+ end
40
+
41
+ class ClientRegistry # :nodoc:
42
+ extend ActiveSupport::PerThreadRegistry
43
+
44
+ attr_accessor :client
45
+ end
46
+
34
47
  require 'amorail/engine' if defined?(Rails)
35
48
  end
@@ -6,17 +6,25 @@ require 'active_support'
6
6
  module Amorail
7
7
  # Amorail http client
8
8
  class Client
9
- attr_accessor :cookies
9
+ attr_reader :usermail, :api_key, :api_endpoint
10
10
 
11
- def initialize
12
- @host = Amorail.config.api_endpoint
13
- @connect = Faraday.new(url: @host) do |faraday|
11
+ def initialize(api_endpoint: Amorail.config.api_endpoint,
12
+ api_key: Amorail.config.api_key,
13
+ usermail: Amorail.config.usermail)
14
+ @api_endpoint = api_endpoint
15
+ @api_key = api_key
16
+ @usermail = usermail
17
+ @connect = Faraday.new(url: api_endpoint) do |faraday|
14
18
  faraday.adapter Faraday.default_adapter
15
19
  faraday.response :json, content_type: /\bjson$/
16
20
  faraday.use :instrumentation
17
21
  end
18
22
  end
19
23
 
24
+ def properties
25
+ @properties ||= Property.new(self)
26
+ end
27
+
20
28
  def connect
21
29
  @connect || self.class.new
22
30
  end
@@ -25,8 +33,8 @@ module Amorail
25
33
  self.cookies = nil
26
34
  response = post(
27
35
  Amorail.config.auth_url,
28
- 'USER_LOGIN' => Amorail.config.usermail,
29
- 'USER_HASH' => Amorail.config.api_key
36
+ 'USER_LOGIN' => usermail,
37
+ 'USER_HASH' => api_key
30
38
  )
31
39
  cookie_handler(response)
32
40
  response
@@ -55,6 +63,10 @@ module Amorail
55
63
  handle_response(response)
56
64
  end
57
65
 
66
+ private
67
+
68
+ attr_accessor :cookies
69
+
58
70
  def cookie_handler(response)
59
71
  self.cookies = response.headers['set-cookie'].split('; ')[0]
60
72
  end
@@ -9,7 +9,7 @@ module Amorail
9
9
  # Find links by contacts ids
10
10
  def find_by_contacts(*ids)
11
11
  ids = ids.first if ids.size == 1 && ids.first.is_a?(Array)
12
- response = Amorail.client.safe_request(
12
+ response = client.safe_request(
13
13
  :get,
14
14
  remote_url('links'),
15
15
  contacts_link: ids
@@ -20,7 +20,7 @@ module Amorail
20
20
  # Find links by leads ids
21
21
  def find_by_leads(*ids)
22
22
  ids = ids.first if ids.size == 1 && ids.first.is_a?(Array)
23
- response = Amorail.client.safe_request(
23
+ response = client.safe_request(
24
24
  :get,
25
25
  remote_url('links'),
26
26
  deals_link: ids
@@ -12,6 +12,8 @@ module Amorail
12
12
  class << self
13
13
  attr_reader :amo_name, :amo_response_name
14
14
 
15
+ delegate :client, to: Amorail
16
+
15
17
  # copy Amo names
16
18
  def inherited(subclass)
17
19
  subclass.amo_names amo_name, amo_response_name
@@ -51,8 +53,8 @@ module Amorail
51
53
  amo_field :id, :request_id, :responsible_user_id,
52
54
  date_create: :timestamp, last_modified: :timestamp
53
55
 
54
- delegate :client, :properties, to: Amorail
55
- delegate :amo_name, :remote_url, to: :class
56
+ delegate :amo_name, :remote_url, :client, to: :class
57
+ delegate :properties, to: Amorail
56
58
 
57
59
  def initialize(attributes = {})
58
60
  super(attributes)
@@ -17,7 +17,7 @@ module Amorail # :nodoc: all
17
17
  def find_all(*ids)
18
18
  ids = ids.first if ids.size == 1 && ids.first.is_a?(Array)
19
19
 
20
- response = Amorail.client.safe_request(
20
+ response = client.safe_request(
21
21
  :get,
22
22
  remote_url('list'),
23
23
  id: ids
@@ -28,7 +28,7 @@ module Amorail # :nodoc: all
28
28
  # Find AMO entities by query
29
29
  # Returns array of matching entities.
30
30
  def find_by_query(q)
31
- response = Amorail.client.safe_request(
31
+ response = client.safe_request(
32
32
  :get,
33
33
  remote_url('list'),
34
34
  query: q
@@ -46,7 +46,6 @@ module Amorail # :nodoc: all
46
46
  end
47
47
 
48
48
  # this method removes nil values and empty arrays from params hash (deep)
49
- # rubocop:disable Metrics/AbcSize
50
49
  # rubocop:disable Metrics/CyclomaticComplexity
51
50
  # rubocop:disable Metrics/MethodLength
52
51
  def normalize_params(data)
@@ -71,7 +70,6 @@ module Amorail # :nodoc: all
71
70
  end
72
71
  compacted.with_indifferent_access
73
72
  end
74
- # rubocop:enable Metrics/AbcSize
75
73
  # rubocop:enable Metrics/CyclomaticComplexity
76
74
  # rubocop:enable Metrics/MethodLength
77
75
 
@@ -1,4 +1,4 @@
1
1
  # Amorail version
2
2
  module Amorail
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0".freeze
4
4
  end
@@ -5,19 +5,119 @@ describe Amorail::Client do
5
5
 
6
6
  before(:each) { mock_api }
7
7
 
8
- it "it should create client" do
9
- expect(Amorail.config.usermail).to eq "amorail@test.com"
10
- expect(Amorail.config.api_key).to eq "75742b166417fe32ae132282ce178cf6"
11
- expect(Amorail.config.api_endpoint).to eq "https://test.amocrm.ru"
12
- end
8
+ context "default client" do
9
+ it "should create client", :aggregate_failures do
10
+ expect(subject.usermail).to eq "amorail@test.com"
11
+ expect(subject.api_key).to eq "75742b166417fe32ae132282ce178cf6"
12
+ expect(subject.api_endpoint).to eq "https://test.amocrm.ru"
13
+ end
14
+
15
+ it "should #authorize method call" do
16
+ res = client.authorize
17
+ expect(res.status).to eq 200
18
+ end
13
19
 
14
- it "should #authorize method call" do
15
- res = client.authorize
16
- expect(res.status).to eq 200
20
+ it "should #authorize and set cookie" do
21
+ res = client.get("/private/api/v2/json/accounts/current")
22
+ expect(res.status).to eq 200
23
+ end
17
24
  end
18
25
 
19
- it "should #authorize and set cookie" do
20
- res = client.get("/private/api/v2/json/accounts/current")
21
- expect(res.status).to eq 200
26
+ describe "#with_client" do
27
+ before { mock_custom_api("https://custom.amo.com", "custom@amo.com", "123") }
28
+
29
+ let(:new_client) do
30
+ described_class.new(
31
+ api_endpoint: "https://custom.amo.com",
32
+ usermail: "custom@amo.com",
33
+ api_key: "123"
34
+ )
35
+ end
36
+
37
+ it "use custom client as instance", :aggregate_failures do
38
+ expect(Amorail.client.usermail).to eq "amorail@test.com"
39
+ Amorail.with_client(new_client) do
40
+ expect(Amorail.client.usermail).to eq "custom@amo.com"
41
+ expect(Amorail.client.api_endpoint).to eq "https://custom.amo.com"
42
+ expect(Amorail.client.api_key).to eq "123"
43
+ end
44
+
45
+ expect(Amorail.client.usermail).to eq "amorail@test.com"
46
+ end
47
+
48
+ it "use custom client as options", :aggregate_failures do
49
+ expect(Amorail.client.usermail).to eq "amorail@test.com"
50
+ Amorail.with_client(
51
+ api_endpoint: "https://custom.amo.com",
52
+ usermail: "custom@amo.com",
53
+ api_key: "123"
54
+ ) do
55
+ expect(Amorail.client.usermail).to eq "custom@amo.com"
56
+ expect(Amorail.client.api_endpoint).to eq "https://custom.amo.com"
57
+ expect(Amorail.client.api_key).to eq "123"
58
+ end
59
+
60
+ expect(Amorail.client.usermail).to eq "amorail@test.com"
61
+ end
62
+
63
+ it "loads custom properties", :aggregate_failures do
64
+ expect(Amorail.properties.company.phone.id).to eq "1460589"
65
+
66
+ Amorail.with_client(new_client) do
67
+ expect(Amorail.properties.company.phone.id).to eq "301"
68
+ end
69
+
70
+ expect(Amorail.properties.company.phone.id).to eq "1460589"
71
+ end
72
+
73
+ it "threadsafe", :aggregate_failures do
74
+ results = []
75
+ q1 = Queue.new
76
+ q2 = Queue.new
77
+ q3 = Queue.new
78
+ threads = []
79
+
80
+ # This thread enters block first but commits result
81
+ # only after the second thread enters block
82
+ threads << Thread.new do
83
+ q1.pop
84
+ Amorail.with_client(usermail: 'test1@amo.com') do
85
+ q2 << 1
86
+ q1.pop
87
+ results << Amorail.client.usermail
88
+ q2 << 1
89
+ end
90
+ q3 << 1
91
+ end
92
+
93
+ # This thread enters block second and commits result
94
+ # after the first block
95
+ threads << Thread.new do
96
+ q2.pop
97
+ Amorail.with_client(usermail: 'test2@amo.com') do
98
+ q1 << 1
99
+ q2.pop
100
+ results << Amorail.client.usermail
101
+ end
102
+ q3 << 1
103
+ end
104
+
105
+ # This thread enters block third and commits
106
+ # after all other threads left blocks
107
+ threads << Thread.new do
108
+ Amorail.with_client(usermail: 'test3@amo.com') do
109
+ q3.pop
110
+ q3.pop
111
+ results << Amorail.client.usermail
112
+ end
113
+ end
114
+
115
+ q1 << 1
116
+ threads.each(&:join)
117
+
118
+ expect(results[0]).to eq 'test1@amo.com'
119
+ expect(results[1]).to eq 'test2@amo.com'
120
+ expect(results[2]).to eq 'test3@amo.com'
121
+ end
22
122
  end
23
123
  end
@@ -0,0 +1,195 @@
1
+ {
2
+ "response": {
3
+ "account": {
4
+ "id": "101",
5
+ "name": "База клиентов",
6
+ "subdomain": "custom_test",
7
+ "currency": "RUB",
8
+ "paid_from": false,
9
+ "paid_till": false,
10
+ "timezone": "Europe/Moscow",
11
+ "language": "ru",
12
+ "date_pattern": "d.m.Y H:i",
13
+ "limits": {
14
+ "users_count": false,
15
+ "contacts_count": false,
16
+ "active_deals_count": false
17
+ },
18
+ "users": [
19
+ {
20
+ "id": "337914",
21
+ "name": "sergey",
22
+ "last_name": null,
23
+ "login": "alekseenkoss@gmail.com",
24
+ "group_id": 0,
25
+ "rights_lead_add": "A",
26
+ "rights_lead_view": "A",
27
+ "rights_lead_edit": "A",
28
+ "rights_lead_delete": "A",
29
+ "rights_lead_export": "A",
30
+ "rights_contact_add": "A",
31
+ "rights_contact_view": "A",
32
+ "rights_contact_edit": "A",
33
+ "rights_contact_delete": "A",
34
+ "rights_contact_export": "A",
35
+ "rights_company_add": "A",
36
+ "rights_company_view": "A",
37
+ "rights_company_edit": "A",
38
+ "rights_company_delete": "A",
39
+ "rights_company_export": "A",
40
+ "is_admin": "Y"
41
+ }
42
+ ],
43
+ "groups": [],
44
+ "leads_statuses": [
45
+ {
46
+ "name": "Demo",
47
+ "id": "101",
48
+ "color": "#99CCFF",
49
+ "editable": "Y",
50
+ "sort": "10"
51
+ },
52
+ {
53
+ "name": "Closed",
54
+ "id": "102",
55
+ "color": "#FFFF99",
56
+ "editable": "Y",
57
+ "sort": "20"
58
+ }
59
+ ],
60
+ "custom_fields": {
61
+ "contacts": [
62
+ {
63
+ "id": "201",
64
+ "name": "Должность",
65
+ "code": "POSITION",
66
+ "multiple": "N",
67
+ "type_id": "1",
68
+ "disabled": "0"
69
+ },
70
+ {
71
+ "id": "202",
72
+ "name": "Телефон",
73
+ "code": "PHONE",
74
+ "multiple": "Y",
75
+ "type_id": "8",
76
+ "disabled": "0",
77
+ "enums": {
78
+ "3392086": "WORK",
79
+ "3392088": "WORKDD",
80
+ "3392090": "MOB",
81
+ "3392092": "FAX",
82
+ "3392094": "HOME",
83
+ "3392096": "OTHER"
84
+ }
85
+ },
86
+ {
87
+ "id": "203",
88
+ "name": "Email",
89
+ "code": "EMAIL",
90
+ "multiple": "Y",
91
+ "type_id": "8",
92
+ "disabled": "0",
93
+ "enums": {
94
+ "3392098": "WORK",
95
+ "3392100": "PRIV",
96
+ "3392102": "OTHER"
97
+ }
98
+ },
99
+ {
100
+ "id": "204",
101
+ "name": "Мгн. сообщения",
102
+ "code": "IM",
103
+ "multiple": "Y",
104
+ "type_id": "8",
105
+ "disabled": "0",
106
+ "enums": {
107
+ "3392104": "SKYPE",
108
+ "3392106": "ICQ",
109
+ "3392108": "JABBER",
110
+ "3392110": "GTALK",
111
+ "3392112": "MSN",
112
+ "3392114": "OTHER"
113
+ }
114
+ }
115
+ ],
116
+ "leads": [],
117
+ "companies": [
118
+ {
119
+ "id": "301",
120
+ "name": "Телефон",
121
+ "code": "PHONE",
122
+ "multiple": "Y",
123
+ "type_id": "8",
124
+ "disabled": "0",
125
+ "enums": {
126
+ "3392086": "WORK",
127
+ "3392088": "WORKDD",
128
+ "3392090": "MOB",
129
+ "3392092": "FAX",
130
+ "3392094": "HOME",
131
+ "3392096": "OTHER"
132
+ }
133
+ },
134
+ {
135
+ "id": "302",
136
+ "name": "Email",
137
+ "code": "EMAIL",
138
+ "multiple": "Y",
139
+ "type_id": "8",
140
+ "disabled": "0",
141
+ "enums": {
142
+ "3392098": "WORK",
143
+ "3392100": "PRIV",
144
+ "3392102": "OTHER"
145
+ }
146
+ },
147
+ {
148
+ "id": "303",
149
+ "name": "Web",
150
+ "code": "WEB",
151
+ "multiple": "N",
152
+ "type_id": "7",
153
+ "disabled": "0"
154
+ },
155
+ {
156
+ "id": "304",
157
+ "name": "Адрес",
158
+ "code": "ADDRESS",
159
+ "multiple": "N",
160
+ "type_id": "9",
161
+ "disabled": "0"
162
+ }
163
+ ]
164
+ },
165
+ "note_types": [
166
+ {
167
+ "id": 1000,
168
+ "name": "",
169
+ "code": "DEMO_REQUESTED",
170
+ "editable": "N"
171
+ },
172
+ {
173
+ "id": 1001,
174
+ "name": "",
175
+ "code": "BILL_CREATED",
176
+ "editable": "N"
177
+ }
178
+ ],
179
+ "task_types": [
180
+ {
181
+ "id": 1,
182
+ "name": "Звонок",
183
+ "code": "CALL"
184
+ },
185
+ {
186
+ "id": 2,
187
+ "name": "Встреча",
188
+ "code": "MEETING"
189
+ }
190
+ ],
191
+ "timezoneoffset": "+03:00"
192
+ },
193
+ "server_time": 1422442143
194
+ }
195
+ }
@@ -9,6 +9,16 @@ module AmoWebMock
9
9
  account_info_stub(Amorail.config.api_endpoint)
10
10
  end
11
11
 
12
+ def mock_custom_api(endpoint, usermail, api_key, properties = 'account2_response.json')
13
+ authorize_stub(
14
+ endpoint,
15
+ usermail,
16
+ api_key
17
+ )
18
+
19
+ account_info_stub(endpoint, properties)
20
+ end
21
+
12
22
  def authorize_stub(endpoint, usermail, api_key)
13
23
  cookie = 'PHPSESSID=58vorte6dd4t7h6mtuig9l0p50; path=/; domain=amocrm.ru'
14
24
  stub_request(:post, "#{endpoint}/private/api/auth.php?type=json")
@@ -23,10 +33,10 @@ module AmoWebMock
23
33
  })
24
34
  end
25
35
 
26
- def account_info_stub(endpoint)
36
+ def account_info_stub(endpoint, properties = 'account_response.json')
27
37
  stub_request(:get, endpoint + '/private/api/v2/json/accounts/current')
28
38
  .to_return(
29
- body: File.read('./spec/fixtures/account_response.json'),
39
+ body: File.read("./spec/fixtures/#{properties}"),
30
40
  headers: { 'Content-Type' => 'application/json' },
31
41
  status: 200
32
42
  )
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.2.0
4
+ version: 0.3.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: 2015-11-26 00:00:00.000000000 Z
12
+ date: 2016-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,16 +43,16 @@ dependencies:
43
43
  name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: '3.4'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
55
+ version: '3.4'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: webmock
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -211,6 +211,7 @@ files:
211
211
  - spec/contact_link_spec.rb
212
212
  - spec/contact_spec.rb
213
213
  - spec/entity_spec.rb
214
+ - spec/fixtures/account2_response.json
214
215
  - spec/fixtures/account_response.json
215
216
  - spec/fixtures/amorail_test.yml
216
217
  - spec/fixtures/contact_create.json
@@ -251,7 +252,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
251
252
  version: '0'
252
253
  requirements: []
253
254
  rubyforge_project:
254
- rubygems_version: 2.4.5
255
+ rubygems_version: 2.5.1
255
256
  signing_key:
256
257
  specification_version: 4
257
258
  summary: Ruby API client for AmoCRM
@@ -261,6 +262,7 @@ test_files:
261
262
  - spec/contact_link_spec.rb
262
263
  - spec/contact_spec.rb
263
264
  - spec/entity_spec.rb
265
+ - spec/fixtures/account2_response.json
264
266
  - spec/fixtures/account_response.json
265
267
  - spec/fixtures/amorail_test.yml
266
268
  - spec/fixtures/contact_create.json
@@ -281,4 +283,3 @@ test_files:
281
283
  - spec/support/my_contact.rb
282
284
  - spec/support/my_entity.rb
283
285
  - spec/task_spec.rb
284
- has_rdoc: