amorail 0.5.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 +7 -0
  2. data/.gitignore +15 -0
  3. data/.rubocop.yml +61 -0
  4. data/.travis.yml +9 -0
  5. data/Gemfile +5 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +245 -0
  8. data/Rakefile +15 -0
  9. data/amorail.gemspec +33 -0
  10. data/lib/amorail.rb +49 -0
  11. data/lib/amorail/client.rb +101 -0
  12. data/lib/amorail/config.rb +17 -0
  13. data/lib/amorail/entities/company.rb +23 -0
  14. data/lib/amorail/entities/contact.rb +29 -0
  15. data/lib/amorail/entities/contact_link.rb +32 -0
  16. data/lib/amorail/entities/elementable.rb +37 -0
  17. data/lib/amorail/entities/lead.rb +26 -0
  18. data/lib/amorail/entities/leadable.rb +29 -0
  19. data/lib/amorail/entities/note.rb +17 -0
  20. data/lib/amorail/entities/task.rb +18 -0
  21. data/lib/amorail/entities/webhook.rb +42 -0
  22. data/lib/amorail/entity.rb +128 -0
  23. data/lib/amorail/entity/finders.rb +67 -0
  24. data/lib/amorail/entity/params.rb +95 -0
  25. data/lib/amorail/entity/persistence.rb +66 -0
  26. data/lib/amorail/exceptions.rb +25 -0
  27. data/lib/amorail/property.rb +130 -0
  28. data/lib/amorail/railtie.rb +8 -0
  29. data/lib/amorail/version.rb +4 -0
  30. data/lib/tasks/amorail.rake +6 -0
  31. data/spec/client_spec.rb +123 -0
  32. data/spec/company_spec.rb +82 -0
  33. data/spec/contact_link_spec.rb +40 -0
  34. data/spec/contact_spec.rb +187 -0
  35. data/spec/entity_spec.rb +55 -0
  36. data/spec/fixtures/accounts/response_1.json +344 -0
  37. data/spec/fixtures/accounts/response_2.json +195 -0
  38. data/spec/fixtures/amorail_test.yml +3 -0
  39. data/spec/fixtures/contacts/create.json +13 -0
  40. data/spec/fixtures/contacts/find_many.json +57 -0
  41. data/spec/fixtures/contacts/find_one.json +41 -0
  42. data/spec/fixtures/contacts/links.json +16 -0
  43. data/spec/fixtures/contacts/my_contact_find.json +47 -0
  44. data/spec/fixtures/contacts/update.json +13 -0
  45. data/spec/fixtures/leads/create.json +13 -0
  46. data/spec/fixtures/leads/find_many.json +73 -0
  47. data/spec/fixtures/leads/links.json +16 -0
  48. data/spec/fixtures/leads/update.json +13 -0
  49. data/spec/fixtures/leads/update_errors.json +12 -0
  50. data/spec/fixtures/webhooks/list.json +24 -0
  51. data/spec/fixtures/webhooks/subscribe.json +17 -0
  52. data/spec/fixtures/webhooks/unsubscribe.json +17 -0
  53. data/spec/helpers/webmock_helpers.rb +279 -0
  54. data/spec/lead_spec.rb +101 -0
  55. data/spec/my_contact_spec.rb +48 -0
  56. data/spec/note_spec.rb +26 -0
  57. data/spec/property_spec.rb +45 -0
  58. data/spec/spec_helper.rb +20 -0
  59. data/spec/support/elementable_example.rb +52 -0
  60. data/spec/support/entity_class_example.rb +15 -0
  61. data/spec/support/leadable_example.rb +33 -0
  62. data/spec/support/my_contact.rb +3 -0
  63. data/spec/support/my_entity.rb +4 -0
  64. data/spec/task_spec.rb +49 -0
  65. data/spec/webhook_spec.rb +59 -0
  66. metadata +319 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f65fb80eb5f426c50e32fa474252e764e42e3887cddbf7f735ad217ad98560cf
4
+ data.tar.gz: 5f44b5e558be01b346ad68d4d84c903deec88e05514430531b6aa0b6e02c3e73
5
+ SHA512:
6
+ metadata.gz: 14958c70d64dda63dd82c334d08b2c63cbe8183fbffc8b5c5a63893e42280b0439e8eef4411b9b222c52e54d7dfa0949e82061de6feb4b3814ad169d5ab37cc6
7
+ data.tar.gz: a370262be82cdde926d639976fea7e23833a6f999cff3100c2a5df57e9962c8ea05b5e6b682e4d2738b4f322a3cba73d81195e721eefe017e8717c103a665f2e
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
@@ -0,0 +1,61 @@
1
+ AllCops:
2
+ # Include gemspec and Rakefile
3
+ Include:
4
+ - 'lib/**/*.rb'
5
+ - 'lib/**/*.rake'
6
+ - 'spec/**/*.rb'
7
+ Exclude:
8
+ - 'bin/**/*'
9
+ - Rakefile
10
+ - Gemfile
11
+ - '*.gemspec'
12
+ DisplayCopNames: true
13
+ StyleGuideCopsOnly: false
14
+
15
+ Style/Documentation:
16
+ Exclude:
17
+ - 'spec/**/*.rb'
18
+
19
+ Style/StringLiterals:
20
+ Enabled: false
21
+
22
+ Style/BlockDelimiters:
23
+ Exclude:
24
+ - 'spec/**/*.rb'
25
+
26
+ Metrics/MethodLength:
27
+ Exclude:
28
+ - 'spec/**/*.rb'
29
+
30
+ Rails/Date:
31
+ Enabled: false
32
+
33
+ Rails/TimeZone:
34
+ Enabled: false
35
+
36
+ Metrics/MethodLength:
37
+ Exclude:
38
+ - 'spec/**/*.rb'
39
+
40
+ Metrics/LineLength:
41
+ Exclude:
42
+ - 'spec/**/*.rb'
43
+
44
+ Metrics/BlockLength:
45
+ Exclude:
46
+ - 'spec/**/*.rb'
47
+
48
+ Style/WordArray:
49
+ Enabled: false
50
+
51
+ Style/SymbolArray:
52
+ Enabled: false
53
+
54
+ Style/SignalException:
55
+ Enabled: false
56
+
57
+ Layout/MultilineMethodCallBraceLayout:
58
+ Enabled: false
59
+
60
+ Lint/MissingCopEnableDirective:
61
+ Enabled: false
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+
6
+ before_install: gem install bundler -v 1.13.6
7
+
8
+ notifications:
9
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in amorail.gemspec
4
+ gem 'pry-byebug'
5
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 alekseenkoss
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,245 @@
1
+ [![Gem Version](https://badge.fury.io/rb/amorail.svg)](https://rubygems.org/gems/amorail) [![Build Status](https://travis-ci.org/teachbase/amorail.svg?branch=master)](https://travis-ci.org/teachbase/amorail)
2
+
3
+ # Amorail
4
+
5
+ AmoCRM client
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'amorail'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install amorail
22
+
23
+ ## Usage
24
+
25
+ With Amorail you can manipulate the following AmoCRM entities: Companies, Contacts, Leads and Tasks.
26
+ We're triying to build simple AR-like interface.
27
+
28
+ ### Auth configuration
29
+
30
+ Amorail uses [anyway_config](https://github.com/palkan/anyway_config) for configuration, so you
31
+ can provide configuration parameters through env vars, seperate config file (`config/amorail.yml`) or `secrets.yml`.
32
+
33
+ Required params: **usermail**, **api_key** and **api_endpoint**.
34
+
35
+ Example:
36
+
37
+ ```
38
+ # config/secrets.yml
39
+ development:
40
+ ...
41
+ amorail:
42
+ usermail: 'amorail@test.com'
43
+ api_key: '75742b166417fe32ae132282ce178cf6'
44
+ api_endpoint: 'https://test.amocrm.ru'
45
+ ```
46
+
47
+ ### Running from console
48
+
49
+ You can try amorail in action from console ([PRY](https://github.com/pry/pry) is required to be installed):
50
+
51
+ ```shell
52
+ # amorail gem directory
53
+ AMORAIL_USERMAIL=my_mail@test.com AMORAIL_API_KEY=my_key AMORAIL_API_ENDPOINT=my@amo.com bundle exec rake console
54
+ pry> Amorail.properties
55
+ # ... prints properties (custom_fields) data
56
+ pry> Amorail::Contact.find_by_query("test_contact")
57
+ # ... returns array of contacts which satisfy the query
58
+ ```
59
+
60
+ ### Create new objects
61
+
62
+ Create Leads
63
+
64
+ ```ruby
65
+ lead = Amorail::Lead.new(
66
+ name: "Example Lead",
67
+ tags: "IT, Sales",
68
+ price: 100,
69
+ status_id: Amorail.properties.leads.statuses[
70
+ Rails.application.secrets.amoparams['lead_status']
71
+ ].id
72
+ )
73
+
74
+ lead.save!
75
+ ```
76
+
77
+ Create Company
78
+
79
+ ```ruby
80
+ company = Amorail::Company.new(
81
+ name: "My company",
82
+ phone: "222-111",
83
+ email: "human@example.com"
84
+ )
85
+ company.linked_leads_id << lead.id
86
+ company.save!
87
+ ```
88
+
89
+ Create Contact
90
+
91
+ ```ruby
92
+ contact = Amorail::Contact.new(
93
+ name: "Ivan Ivanov",
94
+ linked_company_id: company.id,
95
+ phone: "111-222",
96
+ email: "ivan@example.com"
97
+ )
98
+
99
+ contact.linked_leads_id << lead.id
100
+ contact.save!
101
+ ```
102
+
103
+ Create Task
104
+
105
+ ```ruby
106
+ task = Amorail::Task.new(
107
+ text: "Example task",
108
+ lead: true,
109
+ complete_till: Time.zone.today.end_of_day,
110
+ task_type: Amorail.properties.tasks[Rails.application.secrets.amoparams['task_code']].id
111
+ )
112
+
113
+ # set up lead id
114
+ task.element_id = lead.id
115
+ # and save it
116
+ task.save!
117
+ ```
118
+
119
+ You can find any object by id:
120
+
121
+ ```ruby
122
+ Amorail::Company.find(company_id)
123
+ ```
124
+
125
+ Or using query:
126
+
127
+ ```ruby
128
+ Amorail::Company.find_by_query("vip")
129
+ ```
130
+
131
+ Or using arbitrary params:
132
+
133
+ ```ruby
134
+ Amorail::Company.where(query: "test", limit_rows: 10)
135
+ ```
136
+
137
+ Also you can update objects, e.g:
138
+
139
+ ```ruby
140
+ company = Amorail::Company.find(company_id)
141
+ contact = Amorail::Contact.find(contact_id)
142
+
143
+ # like this
144
+ contact.linked_company_id = company.id
145
+ contact.save!
146
+
147
+ # or
148
+
149
+ contact.update(linked_company_id: company.id)
150
+ ```
151
+
152
+ ### Querying
153
+
154
+ Load by id
155
+
156
+ ```ruby
157
+ Amorail::Contact.find(223) #=> #<Amorail::Contact ...>
158
+ ```
159
+
160
+ Load many entites by array of ids
161
+
162
+ ```ruby
163
+ Amorail::Contact.find_all(123, 124) #=> [#<Amorail::Contact ...>, ...]
164
+ ```
165
+
166
+ Load by query
167
+
168
+ ```ruby
169
+ Amorail::Contact.find_by_query("my_company") #=> [#<Amorail::Contact ...>, ...]
170
+ ```
171
+
172
+
173
+ Load contacts associated with lead
174
+
175
+ ```ruby
176
+ lead = Amorail::Lead.find(1)
177
+ lead.contacts #=> [#<Amorail::Contact ...>, ...]
178
+ ```
179
+
180
+ Load company associated with contact
181
+
182
+ ```ruby
183
+ contact = Amorail::Contact.find(1)
184
+ contact.company #=> #<Amorail::Company ...>
185
+ ```
186
+
187
+ Load leads associated with contact
188
+
189
+ ```ruby
190
+ contact = Amorail::Contact.find(1)
191
+ contact.leads #=> [#<Amorail::Lead ...>, ...]
192
+ ```
193
+
194
+ Load contacts-leads pairs
195
+
196
+ ```ruby
197
+ # Load all contact-leads pairs for contacts
198
+ Amorail::ContactLink.find_by_contacts(1, 2)
199
+
200
+ # Load all contact-leads pairs for leads
201
+ Amorail::ContactLink.find_by_leads(1, 2)
202
+ ```
203
+
204
+ ### Properties Configuration
205
+
206
+ AmoCRM is using "custom_fields" architecture,
207
+ to get all information for your account, you can
208
+ find properties and set up configuration manually in config/secrets.yml.
209
+
210
+ Note: response example in official documentation:
211
+ https://developers.amocrm.ru/rest_api/accounts_current.php
212
+
213
+ 1) Get list of properties for your account
214
+
215
+ ```
216
+ rake amorail:check
217
+ ```
218
+ Rake task will returns information about properties.
219
+
220
+ ### Multiple configurations
221
+
222
+ It is possible to use Amorail with multiple AmoCRM accounts. To do so use `Amorail.with_client` method,
223
+ which receive client params or client instance and a block to execute within custom context:
224
+
225
+ ```ruby
226
+ Amorail.with_client(usermail: "custom@mail.com", api_endpoint: "https://my.acmocrm.ru", api_key: "my_secret_key") do
227
+ # Client specific code here
228
+ end
229
+
230
+ # or using client instance
231
+ my_client = Amorail::Client.new(usermail: "custom@mail.com", api_endpoint: "https://my.acmocrm.ru", api_key: "my_secret_key")
232
+
233
+ Amorail.with_client(client) do
234
+ ...
235
+ end
236
+ ```
237
+
238
+ ## Contributing
239
+
240
+ 1. Fork it ( https://github.com/[my-github-username]/amorail/fork )
241
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
242
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
243
+ 4. Follow style guides (use Rubocop)
244
+ 5. Push to the branch (`git push origin my-new-feature`)
245
+ 6. Create a new Pull Request
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ require "rubocop/rake_task"
4
+
5
+ Dir.glob('lib/tasks/*.rake').each { |r| import r }
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :console do
10
+ sh 'pry -r ./lib/amorail.rb'
11
+ end
12
+
13
+ RuboCop::RakeTask.new
14
+
15
+ task default: [:rubocop, :spec]
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'amorail/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "amorail"
8
+ spec.version = Amorail::VERSION
9
+ spec.authors = ["alekseenkoss", "palkan"]
10
+ spec.email = ["alekseenkoss@gmail.com", "dementiev.vm@gmail.com"]
11
+ spec.summary = %q{Ruby API client for AmoCRM}
12
+ spec.description = %q{Ruby API client for AmoCRM. You can integrate your system with it.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.4"
24
+ spec.add_development_dependency "webmock"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "shoulda-matchers", "~> 2.0"
27
+ spec.add_development_dependency "rubocop", "~> 0.49"
28
+ spec.add_dependency "anyway_config", "~> 0", ">= 0.3"
29
+ spec.add_dependency "faraday"
30
+ spec.add_dependency "faraday_middleware"
31
+ spec.add_dependency 'activemodel'
32
+ spec.add_dependency 'json'
33
+ end
@@ -0,0 +1,49 @@
1
+ require 'amorail/version'
2
+ require 'amorail/config'
3
+ require 'amorail/client'
4
+ require 'amorail/exceptions'
5
+ require 'amorail/entity'
6
+ require 'amorail/property'
7
+
8
+ Gem.find_files('amorail/entities/*.rb').each { |path| require path }
9
+
10
+ # AmoCRM API integration.
11
+ # https://www.amocrm.com/
12
+ module Amorail
13
+ def self.config
14
+ @config ||= Config.new
15
+ end
16
+
17
+ def self.properties
18
+ client.properties
19
+ end
20
+
21
+ def self.configure
22
+ yield(config) if block_given?
23
+ end
24
+
25
+ def self.client
26
+ ClientRegistry.client || (@client ||= Client.new)
27
+ end
28
+
29
+ def self.reset
30
+ @config = nil
31
+ @client = nil
32
+ end
33
+
34
+ def self.with_client(client)
35
+ client = Client.new(client) unless client.is_a?(Client)
36
+ ClientRegistry.client = client
37
+ yield
38
+ ensure
39
+ ClientRegistry.client = nil
40
+ end
41
+
42
+ class ClientRegistry # :nodoc:
43
+ extend ActiveSupport::PerThreadRegistry
44
+
45
+ attr_accessor :client
46
+ end
47
+
48
+ require 'amorail/railtie' if defined?(Rails)
49
+ end