amorail 0.3.4 → 0.3.5

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: 8db673fd4edfbf45eb53a5d0f975a89d7895fdba
4
- data.tar.gz: 356888cd7462e7c1ec8c0b427f422faf6b2da05e
3
+ metadata.gz: 64a5f20666f547ec054a884b6a11907c10ba6d33
4
+ data.tar.gz: e7e11f93b5408ffea0f80f15f4e70f4ac03c5ebd
5
5
  SHA512:
6
- metadata.gz: 97c770c6157118cb4613528c1b0f3ce57f958bb973426b3113bf5c5ee0f7da40d197dc516401568ca0fcc615764484de9936e3ff8f82df2d0c4f59dd406aacd2
7
- data.tar.gz: 965e56642519d357ba3eba530bfdd1a8b1f967bb0d8e11ab0dc54ccab032ef82ae40c97a91c0ed396f4cd3a19e72f8a796ea306afae7513616cc0023422fcb64
6
+ metadata.gz: e3f035e93ac4be0bb929e0ac7f0a0d0bf28bbf43df6d648720554daa5ebffa5a9b7aad22a2ab250629e67c699380743c96dcb207d5bc8300305bec64f8c82c80
7
+ data.tar.gz: 1f2ece52b8c8755d38d9c05eb74cef43e515daac3cd7223c8fdeb6b9c6fe9f79962972057b7af07fb714f4e86a0f55478ee4e0ce30a48874cb72a2bb7e35a9d8
data/.rubocop.yml CHANGED
@@ -6,7 +6,9 @@ AllCops:
6
6
  - 'spec/**/*.rb'
7
7
  Exclude:
8
8
  - 'bin/**/*'
9
- RunRailsCops: true
9
+ - Rakefile
10
+ - Gemfile
11
+ - '*.gemspec'
10
12
  DisplayCopNames: true
11
13
  StyleGuideCopsOnly: false
12
14
 
@@ -39,5 +41,18 @@ Metrics/LineLength:
39
41
  Exclude:
40
42
  - 'spec/**/*.rb'
41
43
 
44
+ Metrics/BlockLength:
45
+ Exclude:
46
+ - 'spec/**/*.rb'
47
+
42
48
  Style/WordArray:
43
49
  Enabled: false
50
+
51
+ Style/SymbolArray:
52
+ Enabled: false
53
+
54
+ Style/SignalException:
55
+ Enabled: false
56
+
57
+ Layout/MultilineMethodCallBraceLayout:
58
+ Enabled: false
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build Status](https://travis-ci.org/teachbase/amorail.svg?branch=master)](https://travis-ci.org/teachbase/amorail)
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
2
 
3
3
  # Amorail
4
4
 
@@ -128,6 +128,12 @@ Or using query:
128
128
  Amorail::Company.find_by_query("vip")
129
129
  ```
130
130
 
131
+ Or using arbitrary params:
132
+
133
+ ```ruby
134
+ Amorail::Company.where(query: "test", limit_rows: 10)
135
+ ```
136
+
131
137
  Also you can update objects, e.g:
132
138
 
133
139
  ```ruby
data/Rakefile CHANGED
@@ -1,11 +1,15 @@
1
1
  require "bundler/gem_tasks"
2
2
  require 'rspec/core/rake_task'
3
- Dir.glob('lib/tasks/*.rake').each {|r| import r}
3
+ require "rubocop/rake_task"
4
4
 
5
- RSpec::Core::RakeTask.new(:spec)
5
+ Dir.glob('lib/tasks/*.rake').each { |r| import r }
6
6
 
7
- task :default => :spec
7
+ RSpec::Core::RakeTask.new(:spec)
8
8
 
9
9
  task :console do
10
10
  sh 'pry -r ./lib/amorail.rb'
11
11
  end
12
+
13
+ RuboCop::RakeTask.new
14
+
15
+ task default: [:rubocop, :spec]
data/amorail.gemspec CHANGED
@@ -22,8 +22,9 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.4"
24
24
  spec.add_development_dependency "webmock"
25
- spec.add_development_dependency 'pry'
26
- spec.add_development_dependency 'shoulda-matchers', "~> 2.0"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "shoulda-matchers", "~> 2.0"
27
+ spec.add_development_dependency "rubocop", "~> 0.49"
27
28
  spec.add_dependency "anyway_config", "~> 0", ">= 0.3"
28
29
  spec.add_dependency "faraday"
29
30
  spec.add_dependency "faraday_middleware"
@@ -0,0 +1,37 @@
1
+ module Amorail
2
+ # Provides common functionallity for entities
3
+ # that can be attached to another objects.
4
+ module Elementable
5
+ extend ActiveSupport::Concern
6
+
7
+ ELEMENT_TYPES = {
8
+ contact: 1,
9
+ lead: 2,
10
+ company: 3,
11
+ task: 4
12
+ }.freeze
13
+
14
+ included do
15
+ amo_field :element_id, :element_type
16
+
17
+ validates :element_id, :element_type,
18
+ presence: true
19
+ end
20
+
21
+ ELEMENT_TYPES.each do |type, value|
22
+ class_eval <<-CODE, __FILE__, __LINE__ + 1
23
+ def #{type}=(val) # def contact=(val)
24
+ #{type}! if val # contact! if val
25
+ end # end
26
+
27
+ def #{type}? # def contact?
28
+ self.element_type == #{value} # self.element_type == 1
29
+ end # end
30
+
31
+ def #{type}! # def contact!
32
+ self.element_type = #{value} # self.element_type = 1
33
+ end # end
34
+ CODE
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ require 'amorail/entities/elementable'
2
+
3
+ module Amorail
4
+ # AmoCRM note entity
5
+ class Note < Amorail::Entity
6
+ include Elementable
7
+
8
+ amo_names 'notes'
9
+
10
+ amo_field :note_type, :text
11
+
12
+ validates :note_type, :text,
13
+ presence: true
14
+
15
+ validates :element_type, inclusion: ELEMENT_TYPES.values
16
+ end
17
+ end
@@ -1,32 +1,18 @@
1
+ require 'amorail/entities/elementable'
2
+
1
3
  module Amorail
2
4
  # AmoCRM task entity
3
5
  class Task < Amorail::Entity
4
- amo_names "tasks"
5
-
6
- amo_field :element_id, :element_type, :text,
7
- :task_type, complete_till: :timestamp
6
+ include Elementable
8
7
 
9
- validates :text, :element_id,
10
- :element_type, :complete_till,
11
- :task_type,
12
- presence: true
8
+ amo_names 'tasks'
13
9
 
14
- validates :element_type, inclusion: 1..2
10
+ amo_field :task_type, :text, complete_till: :timestamp
15
11
 
16
- [{ name: "contact", val: 1 }, { name: "lead", val: 2 }].each do |prop|
17
- class_eval <<-CODE, __FILE__, __LINE__ + 1
18
- def #{prop[:name]}=(val)
19
- #{prop[:name]}! if val
20
- end
21
-
22
- def #{prop[:name]}?
23
- self.element_type == #{prop[:val]}
24
- end
12
+ validates :task_type, :text, :complete_till,
13
+ presence: true
25
14
 
26
- def #{prop[:name]}!
27
- self.element_type = #{prop[:val]}
28
- end
29
- CODE
30
- end
15
+ validates :element_type, inclusion:
16
+ ELEMENT_TYPES.reject { |type, _| type == :task }.values
31
17
  end
32
18
  end
@@ -111,11 +111,13 @@ module Amorail
111
111
  def handle_response(response, method)
112
112
  return false unless response.status == 200
113
113
  extract_method = "extract_data_#{method}"
114
+ return self unless respond_to?(extract_method, true)
115
+
114
116
  reload_model(
115
117
  send(extract_method,
116
118
  response.body['response'][self.class.amo_response_name]
117
119
  )
118
- ) if respond_to?(extract_method, true)
120
+ )
119
121
  self
120
122
  end
121
123
  end
@@ -14,26 +14,26 @@ module Amorail # :nodoc: all
14
14
  rec
15
15
  end
16
16
 
17
- def find_all(*ids)
18
- ids = ids.first if ids.size == 1 && ids.first.is_a?(Array)
19
-
17
+ # General method to load many records by proving some filters
18
+ def where(options)
20
19
  response = client.safe_request(
21
20
  :get,
22
21
  remote_url('list'),
23
- id: ids
22
+ options
24
23
  )
25
24
  load_many(response)
26
25
  end
27
26
 
27
+ def find_all(*ids)
28
+ ids = ids.first if ids.size == 1 && ids.first.is_a?(Array)
29
+
30
+ where(id: ids)
31
+ end
32
+
28
33
  # Find AMO entities by query
29
34
  # Returns array of matching entities.
30
35
  def find_by_query(q)
31
- response = client.safe_request(
32
- :get,
33
- remote_url('list'),
34
- query: q
35
- )
36
- load_many(response)
36
+ where(query: q)
37
37
  end
38
38
 
39
39
  private
@@ -8,6 +8,10 @@ module Amorail
8
8
  super
9
9
  end
10
10
  end
11
+
12
+ def respond_to_missing?(method_sym, *args)
13
+ args.size.zero? && data.key?(method_sym.to_s)
14
+ end
11
15
  end
12
16
 
13
17
  class Property # :nodoc: all
@@ -1,4 +1,5 @@
1
1
  module Amorail
2
+ # Add amorail rake tasks
2
3
  class Railtie < Rails::Railtie
3
4
  rake_tasks do
4
5
  load File.expand_path('../../tasks/amorail.rake', __FILE__)
@@ -1,4 +1,4 @@
1
1
  # Amorail version
2
2
  module Amorail
3
- VERSION = "0.3.4".freeze
3
+ VERSION = "0.3.5".freeze
4
4
  end
data/spec/contact_spec.rb CHANGED
@@ -136,6 +136,21 @@ describe Amorail::Contact do
136
136
  end
137
137
  end
138
138
 
139
+ describe ".where" do
140
+ before { contacts_where_stub(Amorail.config.api_endpoint, query: 'xxx', limit_rows: 10, limit_offset: 100) }
141
+
142
+ it "loads entities" do
143
+ res = described_class.where(query: 'xxx', limit_rows: 10, limit_offset: 100)
144
+ expect(res.size).to eq 2
145
+ expect(res.first.id).to eq 101
146
+ expect(res.last.id).to eq 102
147
+ expect(res.first.company_name).to eq "Foo Inc."
148
+ expect(res.last.email).to eq "foo2@tb.com"
149
+ expect(res.first.phone).to eq "1111 111 111"
150
+ expect(res.first.params[:id]).to eq 101
151
+ end
152
+ end
153
+
139
154
  describe "#save" do
140
155
  before { contact_create_stub(Amorail.config.api_endpoint) }
141
156
 
@@ -150,6 +150,26 @@ module AmoWebMock
150
150
  end
151
151
  end
152
152
 
153
+ def contacts_where_stub(endpoint, success = true, **params)
154
+ if success
155
+ stub_request(
156
+ :get,
157
+ "#{endpoint}/private/api/v2/json/contacts/list"
158
+ ).with(
159
+ query: params
160
+ ).to_return(
161
+ body: File.read('./spec/fixtures/contact_find_query.json'),
162
+ headers: { 'Content-Type' => 'application/json' },
163
+ status: 200
164
+ )
165
+ else
166
+ stub_request(
167
+ :get,
168
+ "#{endpoint}/private/api/v2/json/contacts/list?query=#{query}")
169
+ .to_return(status: 204)
170
+ end
171
+ end
172
+
153
173
  def company_create_stub(endpoint)
154
174
  stub_request(:post, endpoint + '/private/api/v2/json/company/set')
155
175
  .to_return(
data/spec/note_spec.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Amorail::Note do
4
+ before { mock_api }
5
+
6
+ it_behaves_like 'elementable'
7
+
8
+ describe 'validations' do
9
+ it { is_expected.to validate_presence_of(:text) }
10
+ it { is_expected.to validate_presence_of(:note_type) }
11
+ it { is_expected.to validate_inclusion_of(:element_type).in_range(1..4) }
12
+ end
13
+
14
+ describe '.attributes' do
15
+ subject { described_class.attributes }
16
+
17
+ it_behaves_like 'entity_class'
18
+
19
+ specify do
20
+ is_expected.to include(
21
+ :text,
22
+ :note_type
23
+ )
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,52 @@
1
+ shared_examples 'elementable' do
2
+ describe 'validations' do
3
+ it { is_expected.to validate_presence_of(:element_id) }
4
+ it { is_expected.to validate_presence_of(:element_type) }
5
+ end
6
+
7
+ describe '.attributes' do
8
+ subject { described_class.attributes }
9
+
10
+ specify do
11
+ is_expected.to include(
12
+ :element_type,
13
+ :element_id
14
+ )
15
+ end
16
+ end
17
+
18
+ describe '#params' do
19
+ subject { elementable.params }
20
+
21
+ let(:elementable) do
22
+ described_class.new(
23
+ element_id: 1,
24
+ element_type: 2
25
+ )
26
+ end
27
+
28
+ it { is_expected.to include(element_id: 1) }
29
+ it { is_expected.to include(element_type: 2) }
30
+ end
31
+
32
+ describe 'element type behaviour' do
33
+ let(:elementable) { described_class.new }
34
+
35
+ it 'set element_type on initialize' do
36
+ expect(described_class.new(lead: true).element_type).to eq 2
37
+ expect(described_class.new(lead: false).element_type).to be_nil
38
+ expect(described_class.new(contact: true).contact?).to be_truthy
39
+ end
40
+
41
+ it 'set element_type with bang method' do
42
+ elementable.contact!
43
+ expect(elementable.element_type).to eq 1
44
+
45
+ elementable.lead!
46
+ expect(elementable.element_type).to eq 2
47
+
48
+ elementable.company!
49
+ expect(elementable.element_type).to eq 3
50
+ end
51
+ end
52
+ end
data/spec/task_spec.rb CHANGED
@@ -3,13 +3,13 @@ require "spec_helper"
3
3
  describe Amorail::Task do
4
4
  before { mock_api }
5
5
 
6
+ it_behaves_like 'elementable'
7
+
6
8
  describe "validations" do
7
- it { should validate_presence_of(:element_id) }
8
- it { should validate_presence_of(:element_type) }
9
- it { should validate_inclusion_of(:element_type).in_range(1..2) }
10
- it { should validate_presence_of(:text) }
11
- it { should validate_presence_of(:task_type) }
12
- it { should validate_presence_of(:complete_till) }
9
+ it { is_expected.to validate_presence_of(:text) }
10
+ it { is_expected.to validate_presence_of(:task_type) }
11
+ it { is_expected.to validate_presence_of(:complete_till) }
12
+ it { is_expected.to validate_inclusion_of(:element_type).in_range(1..3) }
13
13
  end
14
14
 
15
15
  describe ".attributes" do
@@ -19,8 +19,6 @@ describe Amorail::Task do
19
19
 
20
20
  specify do
21
21
  is_expected.to include(
22
- :element_type,
23
- :element_id,
24
22
  :text,
25
23
  :task_type,
26
24
  :complete_till
@@ -28,27 +26,9 @@ describe Amorail::Task do
28
26
  end
29
27
  end
30
28
 
31
- describe "contact and lead" do
32
- let(:task) { described_class.new }
33
- it "set element_type on initialize" do
34
- expect(described_class.new(lead: true).element_type).to eq 2
35
- expect(described_class.new(contact: true).contact?).to be_truthy
36
- expect(described_class.new(lead: false).element_type).to be_nil
37
- end
38
-
39
- it "set element_type with bang method" do
40
- task.contact!
41
- expect(task.element_type).to eq 1
42
- task.lead!
43
- expect(task.element_type).to eq 2
44
- end
45
- end
46
-
47
29
  describe "#params" do
48
30
  let(:task) do
49
31
  described_class.new(
50
- element_id: 1,
51
- element_type: 1,
52
32
  text: 'Win the war',
53
33
  task_type: 'test',
54
34
  complete_till: '2015-05-09 12:00:00'
@@ -58,8 +38,6 @@ describe Amorail::Task do
58
38
  subject { task.params }
59
39
 
60
40
  specify { is_expected.to include(:last_modified) }
61
- specify { is_expected.to include(element_id: 1) }
62
- specify { is_expected.to include(element_type: 1) }
63
41
  specify { is_expected.to include(text: 'Win the war') }
64
42
  specify { is_expected.to include(task_type: 'test') }
65
43
  specify {
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.3.4
4
+ version: 0.3.5
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-07-12 00:00:00.000000000 Z
12
+ date: 2017-07-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -95,6 +95,20 @@ dependencies:
95
95
  - - "~>"
96
96
  - !ruby/object:Gem::Version
97
97
  version: '2.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '0.49'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '0.49'
98
112
  - !ruby/object:Gem::Dependency
99
113
  name: anyway_config
100
114
  requirement: !ruby/object:Gem::Requirement
@@ -180,7 +194,6 @@ extensions: []
180
194
  extra_rdoc_files: []
181
195
  files:
182
196
  - ".gitignore"
183
- - ".hound.yml"
184
197
  - ".rubocop.yml"
185
198
  - ".travis.yml"
186
199
  - Gemfile
@@ -194,8 +207,10 @@ files:
194
207
  - lib/amorail/entities/company.rb
195
208
  - lib/amorail/entities/contact.rb
196
209
  - lib/amorail/entities/contact_link.rb
210
+ - lib/amorail/entities/elementable.rb
197
211
  - lib/amorail/entities/lead.rb
198
212
  - lib/amorail/entities/leadable.rb
213
+ - lib/amorail/entities/note.rb
199
214
  - lib/amorail/entities/task.rb
200
215
  - lib/amorail/entity.rb
201
216
  - lib/amorail/entity/finders.rb
@@ -225,8 +240,10 @@ files:
225
240
  - spec/helpers/webmock_helpers.rb
226
241
  - spec/lead_spec.rb
227
242
  - spec/my_contact_spec.rb
243
+ - spec/note_spec.rb
228
244
  - spec/property_spec.rb
229
245
  - spec/spec_helper.rb
246
+ - spec/support/elementable_example.rb
230
247
  - spec/support/entity_class_example.rb
231
248
  - spec/support/leadable_example.rb
232
249
  - spec/support/my_contact.rb
@@ -276,8 +293,10 @@ test_files:
276
293
  - spec/helpers/webmock_helpers.rb
277
294
  - spec/lead_spec.rb
278
295
  - spec/my_contact_spec.rb
296
+ - spec/note_spec.rb
279
297
  - spec/property_spec.rb
280
298
  - spec/spec_helper.rb
299
+ - spec/support/elementable_example.rb
281
300
  - spec/support/entity_class_example.rb
282
301
  - spec/support/leadable_example.rb
283
302
  - spec/support/my_contact.rb
data/.hound.yml DELETED
@@ -1,12 +0,0 @@
1
- ruby:
2
- enabled: true
3
- config_file: .rubocop.yml
4
-
5
- javascript:
6
- enabled: false
7
-
8
- coffeescript:
9
- enabled: false
10
-
11
- sass:
12
- enabled: false