capsule_crm 0.1.6 → 0.4.6

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: 808ce675363a8bae072d7242b8973f41cf5e0c08
4
- data.tar.gz: edb34f7e67119375ed840b6da034b2fa653ea116
3
+ metadata.gz: 48c3f7884a088c4d42cee4aa450fc594c3f9fa96
4
+ data.tar.gz: 35822071aafa5ea3b7def6616e3e30003fb150ea
5
5
  SHA512:
6
- metadata.gz: 5d0464a4804f4551450541ff4e237d9b04eb94581d074a7460b51eb4ab4dc6008109a98774778e597ad8c9534240e3899a291edb6cc7ad63253c92c9656f5628
7
- data.tar.gz: 56388862805ba1ad015807817c545d9688930d87da6e4b96971e17d68e8d40a9a0adcfd9e92021d30419d69fe1e07db7b43a552ae92dd1c2cd728121d7a5327b
6
+ metadata.gz: 968f3d57a5f7228200488c74021a8c27d05269213094797fdd8124cb843f149cdfc9d1d1b9ffd2a3996fa44cc54b691f9f238bb8b84bf7fbc0a11d4e38376a3f
7
+ data.tar.gz: 782f7e740f951e5c7ca5114a1ea6f481b5638657f64c39f8210ff6da890a6342d03dedec30df05120d05f8d0c92d36ee0312289f619fa6749e90daec7ef42fee
data/capsule_crm.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |gem|
26
26
  gem.add_development_dependency('coveralls')
27
27
  gem.add_development_dependency('cucumber')
28
28
  gem.add_development_dependency('fabrication')
29
+ gem.add_development_dependency('faker')
29
30
  gem.add_development_dependency('guard')
30
31
  gem.add_development_dependency('guard-rspec')
31
32
  gem.add_development_dependency('rb-fsevent')
@@ -29,9 +29,10 @@ module CapsuleCRM
29
29
  # person.organisation
30
30
  # => organisation
31
31
  def belongs_to(association_name, options = {})
32
+ foreign_key = options[:foreign_key] || :"#{association_name}_id"
33
+
32
34
  class_eval do
33
- attribute options[:foreign_key] ||
34
- :"#{association_name}_id", Integer
35
+ attribute foreign_key, Integer
35
36
  end
36
37
 
37
38
  (class << self; self; end).instance_eval do
@@ -42,9 +43,9 @@ module CapsuleCRM
42
43
 
43
44
  define_method association_name do
44
45
  instance_variable_get(:"@#{association_name}") ||
45
- if self.send("#{association_name}_id")
46
+ if self.send(foreign_key)
46
47
  options[:class_name].constantize.
47
- find(self.send("#{association_name}_id")).tap do |object|
48
+ find(self.send(foreign_key)).tap do |object|
48
49
  self.send("#{association_name}=", object)
49
50
  end
50
51
  else
@@ -55,7 +56,7 @@ module CapsuleCRM
55
56
  define_method "#{association_name}=" do |associated_object|
56
57
  instance_variable_set(:"@#{association_name}", associated_object)
57
58
  id = associated_object ? associated_object.id : nil
58
- self.send "#{association_name}_id=", id
59
+ self.send "#{foreign_key}=", id
59
60
  end
60
61
  end
61
62
  end
@@ -11,15 +11,18 @@ module CapsuleCRM
11
11
  #
12
12
  # association_name - The String name of the associated collection
13
13
  # options: - The Hash of options (default: {}):
14
- # class_name: The String name of the class used in the
15
- # association
14
+ # :class_name - The String name of the class used in the
15
+ # association
16
+ # :source - the String method name used by the
17
+ # object on the belongs_to side of the association
18
+ # to set this object
16
19
  #
17
20
  # Examples
18
21
  #
19
22
  # class CapsuleCRM::Organizatio
20
23
  # include CapsuleCRM::Associations::HasMany
21
24
  #
22
- # has_many :people, class_name: 'CapsuleCRM::Person'
25
+ # has_many :people, class_name: 'CapsuleCRM::Person', source: :person
23
26
  # end
24
27
  #
25
28
  # organization = CapsuleCRM::Organization.find(1)
@@ -46,10 +49,14 @@ module CapsuleCRM
46
49
  end
47
50
 
48
51
  define_method "#{association_name}=" do |associated_objects|
52
+ if associated_objects.is_a?(Hash)
53
+ associated_objects = Array(options[:class_name].constantize.new(associated_objects[options[:class_name].demodulize.downcase]))
54
+ end
49
55
  instance_variable_set :"@#{association_name}",
50
56
  CapsuleCRM::Associations::HasManyProxy.new(
51
- parent, associated_objects
52
- )
57
+ self, options[:class_name].constantize,
58
+ associated_objects, options[:source]
59
+ )
53
60
  end
54
61
  end
55
62
  end
@@ -0,0 +1,9 @@
1
+ module CapsuleCRM
2
+ class Attachment
3
+ include Virtus
4
+
5
+ attribute :id, Integer
6
+ attribute :filename, String
7
+ attribute :content_type, String
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ module CapsuleCRM
2
+ class Country
3
+ include Virtus
4
+
5
+ attribute :name
6
+
7
+ # Public: Retrieve a list of countries from Capsule
8
+ #
9
+ # Examples
10
+ #
11
+ # CapsuleCRM::Country.all
12
+ #
13
+ # Returns an Array of CapsuleCRM::Country objects
14
+ def self.all
15
+ CapsuleCRM::Connection.
16
+ get('/api/countries')['countries']['country'].map do |country_name|
17
+ new name: country_name
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module CapsuleCRM
2
+ class Currency
3
+ include Virtus
4
+
5
+ extend ActiveModel::Naming
6
+ include ActiveModel::Conversion
7
+ include ActiveModel::Validations
8
+
9
+ attribute :code, String
10
+
11
+ # Public: Retrieve a list of all currencies in Capsule
12
+ #
13
+ # Examples
14
+ #
15
+ # CapsuleCRM::Currency.all
16
+ #
17
+ # Returns an Array of CapsuleCRM::Currency objects
18
+ def self.all
19
+ CapsuleCRM::Connection.
20
+ get('/api/currencies')['currencies']['currency'].map do |currency_code|
21
+ new code: currency_code
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ module CapsuleCRM
2
+ class CustomField
3
+ include Virtus
4
+
5
+ extend ActiveModel::Naming
6
+ include ActiveModel::Conversion
7
+ include ActiveModel::Validations
8
+
9
+ attribute :id, Integer
10
+ attribute :date, DateTime
11
+ attribute :tag, String
12
+ attribute :boolean, Boolean
13
+ attribute :text, String
14
+ attribute :data_tag, String
15
+
16
+ def self.create(attributes = {})
17
+ end
18
+
19
+ def self.create!(attribute = {})
20
+ end
21
+
22
+ def save
23
+ end
24
+
25
+ def save!
26
+ end
27
+
28
+ def destroy
29
+ end
30
+
31
+ private
32
+
33
+ def create_record
34
+ end
35
+
36
+ def update_record
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,324 @@
1
+ module CapsuleCRM
2
+ class History
3
+ include Virtus
4
+
5
+ extend ActiveModel::Naming
6
+ include ActiveModel::Conversion
7
+ include ActiveModel::Validations
8
+
9
+ include CapsuleCRM::Associations
10
+
11
+ attribute :id, Integer
12
+ attribute :type, String
13
+ attribute :entry_date, DateTime
14
+ attribute :subject, String
15
+ attribute :note, String
16
+
17
+ has_many :attachments, class_name: 'CapsuleCRM::Attachment'
18
+ has_many :participants, class_name: 'CapsuleCRM::Participant'
19
+
20
+ belongs_to :creator, class_name: 'CapsuleCRM::Person'
21
+ belongs_to :party, class_name: 'CapsuleCRM::Party'
22
+ belongs_to :kase, class_name: 'CapsuleCRM::Case',
23
+ foreign_key: :case_id
24
+ belongs_to :opportunity, class_name: 'CapsuleCRM::Opportunity'
25
+
26
+ validates :note, presence: true
27
+ validates :party, :kase, :opportunity,
28
+ presence: { if: :belongs_to_required? }
29
+
30
+ # Public: Underscore all of the attributes keys and set the attributes of
31
+ # this history item
32
+ #
33
+ # attributes - The Hash of history attributes (default: {}):
34
+ # :id - The Integer ID of this history item
35
+ # :creator - The String username of the creator
36
+ # (CapsuleCRM::User) OR a CapsuleCRM::User object
37
+ # :type - The String type (Note, Email or Task)
38
+ # :note - The String note
39
+ # :subject - The String email subject if this history
40
+ # item is from an email
41
+ # :entry_date - The date when this history item was
42
+ # created
43
+ # :attachments - An Array of CapsuleCRM::Attachment
44
+ # objects OR an Array of CapsuleCRM::Attachment attributes
45
+ # hashes
46
+ # :participants - An Array of CapsuleCRM::Participant
47
+ # objects OR an Array of CapsuleCRM::Participant attributes
48
+ # hashes
49
+ # :party_id - The Integer ID of the party that this
50
+ # history item belongs to
51
+ # :case_id - The Integer ID of the case that this
52
+ # history item belongs to
53
+ # :opportunity_id - The Integer ID of the opportunity that
54
+ # this item belongs to
55
+ #
56
+ # Examples
57
+ #
58
+ # history = CapsuleCRM::History.new
59
+ # history.attributes = { entry_date: Time.now }
60
+ #
61
+ # or
62
+ #
63
+ # history.attributes = { entryDate: Time.now }
64
+ #
65
+ # Returns a CapsuleCRM::History object
66
+ def attributes=(attributes)
67
+ CapsuleCRM::HashHelper.underscore_keys!(attributes)
68
+ super(attributes)
69
+ self
70
+ end
71
+
72
+ # Public: find a CapsuleCRM::History by ID
73
+ #
74
+ # id - The Integer CapsuleCRM::History ID
75
+ #
76
+ # Examples
77
+ #
78
+ # CapsuleCRM::History.find(2)
79
+ #
80
+ # Returns a CapsuleCRM::History object
81
+ def self.find(id)
82
+ new CapsuleCRM::Connection.get("/api/history/#{id}")['historyItem']
83
+ end
84
+
85
+ # Public: Set the creator of this history item
86
+ #
87
+ # user - The String username OR a CapsuleCRM::User object
88
+ #
89
+ # Examples
90
+ #
91
+ # history = CapsuleCRM::History.new
92
+ # history.creator = 'a.username'
93
+ #
94
+ # user = CapsuleCRM::User.find_by_username('another.username')
95
+ # history.creator = user
96
+ #
97
+ # Returns the CapsuleCRM::History object
98
+ def creator=(user)
99
+ if user.is_a?(String)
100
+ user = CapsuleCRM::User.find_by_username(user)
101
+ end
102
+ @creator = user
103
+ self
104
+ end
105
+
106
+ # Public: create a CapsuleCRM::History item and persist it
107
+ #
108
+ # attributes - The Hash of attributes (default: {}):
109
+ # :note - The String note
110
+ # :creator - The CapsuleCRM::User who created this history
111
+ # item
112
+ # :entry_date - The date when this history item was created
113
+ #
114
+ # Examples
115
+ #
116
+ # CapsuleCRM::History.create(note: 'Some history note text')
117
+ #
118
+ # person = CapsuleCRM::User.find_by_username('some.username')
119
+ # CapsuleCRM::History.create(
120
+ # note: 'another note to add to the history', person: person,
121
+ # entry_date: Time.now
122
+ # )
123
+ #
124
+ # Returns a CapsuleCRM::History object
125
+ def self.create(attributes = {})
126
+ new(attributes).tap(&:save)
127
+ end
128
+
129
+ # Public: create a CapsuleCRM::History item and persist it. If persistence
130
+ # is not possible then raise an error
131
+ #
132
+ # attributes - The Hash of attributes (default: {}):
133
+ # :note - The String note
134
+ # :creator - The CapsuleCRM::User who created this history
135
+ # item
136
+ # :entry_date - The date when this history item was created
137
+ #
138
+ # Examples
139
+ #
140
+ # CapsuleCRM::History.create!(note: 'Some history note text')
141
+ #
142
+ # person = CapsuleCRM::User.find_by_username('some.username')
143
+ # CapsuleCRM::History.create!(
144
+ # note: 'another note to add to the history', person: person,
145
+ # entry_date: Time.now
146
+ # )
147
+ #
148
+ # CapsuleCRM::History.create! {}
149
+ # => CapsuleCRM::Errors::RecordInvalid
150
+ #
151
+ # Returns a CapsuleCRM::History object
152
+ def self.create!(attributes = {})
153
+ new(attributes).tap(&:save!)
154
+ end
155
+
156
+ # Public: update the attributes of a CapsuleCRM::History item and persist
157
+ # the changes
158
+ #
159
+ # attributes - The Hash of attributes (default: {}):
160
+ # :note - The String note
161
+ # :creator - The String creator username OR
162
+ # a CapsuleCRM::Person object
163
+ # :entry_date - The DateTime when the history item was
164
+ # created
165
+ #
166
+ # Examples
167
+ #
168
+ # history = CapsuleCRM::History.find(1)
169
+ # history.update_attributes note: 'some updated note text'
170
+ #
171
+ # Returns a CapsuleCRM::History item or false
172
+ def update_attributes(attributes = {})
173
+ self.attributes = attributes
174
+ save
175
+ end
176
+
177
+ # Public: update the attributes of a CapsuleCRM::History item and persist
178
+ # the changes. If persistence is not possible then raise an error
179
+ #
180
+ # attributes - The Hash of attributes (default: {}):
181
+ # :note - The String note
182
+ # :creator - The String creator username OR
183
+ # a CapsuleCRM::Person object
184
+ # :entry_date - The DateTime when the history item was
185
+ # created
186
+ #
187
+ # Examples
188
+ #
189
+ # history = CapsuleCRM::History.find(1)
190
+ # history.update_attributes note: 'some updated note text'
191
+ #
192
+ # history.update_attributes note: nil
193
+ # => CapsuleCRM::Errors::RecordInvalid
194
+ #
195
+ # Returns a CapsuleCRM::History item or a CapsuleCRM::Errors::RecordInvalid
196
+ def update_attributes!(attributes = {})
197
+ self.attributes = attributes
198
+ save!
199
+ end
200
+
201
+ # Public: Save this history item. If it is already existing in capsule crm
202
+ # then update it, otherwise create a new one
203
+ #
204
+ # Examples
205
+ #
206
+ # CapsuleCRM::History.new(note: 'some note text').save
207
+ #
208
+ # Returns a CapsuleCRM::History object of false
209
+ def save
210
+ if valid?
211
+ new_record? ? create_record : update_record
212
+ else
213
+ false
214
+ end
215
+ end
216
+
217
+ # Public: Save this history item. If it is already existing in capsule crm
218
+ # then update it, otherwise create a new one. If persistence is not possible
219
+ # then raise an error
220
+ #
221
+ # Examples
222
+ #
223
+ # CapsuleCRM::History.new(note: 'the note text').save!
224
+ #
225
+ # CapsuleCRM::History.new.save!
226
+ # => CapsuleCRM::Errors::RecordInvalid
227
+ #
228
+ # Returns a CapsuleCRM::History object
229
+ def save!
230
+ if valid?
231
+ new_record? ? create_record : update_record
232
+ else
233
+ raise CapsuleCRM::Errors::RecordInvalid.new(self)
234
+ end
235
+ end
236
+
237
+ # Public: Delete this history item from capsule crm.
238
+ #
239
+ # Examples
240
+ #
241
+ # CapsuleCRM::History.find(1).destroy
242
+ #
243
+ # Returns the CapsuleCRM::History object
244
+ def destroy
245
+ self.id = nil if CapsuleCRM::Connection.delete("/api/history/#{id}")
246
+ self
247
+ end
248
+
249
+ # Public: Is this history item a new record?
250
+ #
251
+ # Examples
252
+ #
253
+ # CapsuleCRM::History.new.new_record?
254
+ # => true
255
+ #
256
+ # CapsuleCRM::History.find(1).new_record?
257
+ # => false
258
+ def new_record?
259
+ !id
260
+ end
261
+
262
+ # Public: Is this history item persisted?
263
+ #
264
+ # Examples
265
+ #
266
+ # CapsuleCRM::History.new.persisted?
267
+ # => fale
268
+ #
269
+ # CapsuleCRM::History.find(1).persisted?
270
+ # => true
271
+ def persisted?
272
+ !new_record?
273
+ end
274
+
275
+ # Public: Generate the attributes hash to send to capsule
276
+ #
277
+ # Examples
278
+ #
279
+ # CapsuleCRM::History.find(1).to_capsule_json
280
+ #
281
+ # Returns a Hash of attributes
282
+ def to_capsule_json
283
+ {
284
+ historyItem: CapsuleCRM::HashHelper.camelize_keys(
285
+ {
286
+ note: note, entry_date: entry_date, creator: creator.try(:username)
287
+ }.delete_if { |key, value| value.blank? }
288
+ )
289
+ }
290
+ end
291
+
292
+ private
293
+
294
+ def belongs_to_required?
295
+ party.blank? && kase.blank? && opportunity.blank?
296
+ end
297
+
298
+ def create_record
299
+ self.attributes = CapsuleCRM::Connection.post(
300
+ "/api/#{belongs_to_api_name}/#{belongs_to_id}/history", to_capsule_json
301
+ )
302
+ self
303
+ end
304
+
305
+ def belongs_to_api_name
306
+ {
307
+ person: 'party', organization: 'party', case: 'kase',
308
+ opportunity: 'opportunity'
309
+ }.stringify_keys[belongs_to_name]
310
+ end
311
+
312
+ def belongs_to_name
313
+ (party || kase || opportunity).class.to_s.demodulize.downcase
314
+ end
315
+
316
+ def belongs_to_id
317
+ (party || kase || opportunity).id
318
+ end
319
+
320
+ def update_record
321
+ CapsuleCRM::Connection.put("/api/history/#{id}", to_capsule_json)
322
+ end
323
+ end
324
+ end
@@ -0,0 +1,21 @@
1
+ module CapsuleCRM
2
+ class Participant
3
+ include Virtus
4
+
5
+ extend ActiveModel::Naming
6
+ include ActiveModel::Validations
7
+ include ActiveModel::Conversion
8
+
9
+ attribute :name, String
10
+ attribute :email_address, String
11
+ attribute :role, String
12
+
13
+ def self._for_history(history_id)
14
+ CapsuleCRM::ResultsProxy.new([])
15
+ end
16
+
17
+ def to_capsule_json
18
+ CapsuleCRM::HashHelper.camelize_keys(attributes.dup)
19
+ end
20
+ end
21
+ end
@@ -1,6 +1,10 @@
1
1
  class CapsuleCRM::Party
2
2
  include CapsuleCRM::Taggable
3
3
 
4
+ include CapsuleCRM::Associations::HasMany
5
+
6
+ has_many :histories, class_name: 'CapsuleCRM::History'
7
+
4
8
  def self.all(options = {})
5
9
  attributes = CapsuleCRM::Connection.get('/api/party', options)
6
10
  init_collection(
@@ -30,5 +30,9 @@ module CapsuleCRM
30
30
  end
31
31
  )
32
32
  end
33
+
34
+ def self.find_by_username(username)
35
+ all.select { |user| user.username == username }.first
36
+ end
33
37
  end
34
38
  end
@@ -1,3 +1,3 @@
1
1
  module CapsuleCrm
2
- VERSION = '0.1.6'
2
+ VERSION = '0.4.6'
3
3
  end
data/lib/capsule_crm.rb CHANGED
@@ -8,7 +8,12 @@ require 'capsule_crm/associations'
8
8
  require 'capsule_crm/address'
9
9
  require 'capsule_crm/case'
10
10
  require 'capsule_crm/connection'
11
+
12
+ require 'capsule_crm/attachment'
13
+ require 'capsule_crm/country'
14
+ require 'capsule_crm/currency'
11
15
  require 'capsule_crm/email'
16
+ require 'capsule_crm/history'
12
17
  require 'capsule_crm/party'
13
18
  require 'capsule_crm/phone'
14
19
  require 'capsule_crm/tag'
@@ -22,6 +27,7 @@ require 'capsule_crm/contactable'
22
27
  require 'capsule_crm/configuration'
23
28
  require 'capsule_crm/opportunity'
24
29
  require 'capsule_crm/organization'
30
+ require 'capsule_crm/participant'
25
31
  require 'capsule_crm/person'
26
32
  require 'capsule_crm/version'
27
33
 
@@ -0,0 +1,3 @@
1
+ Fabricator(:case, class_name: :'CapsuleCRM::Case') do
2
+ name { Faker::Lorem.sentence }
3
+ end
@@ -0,0 +1,3 @@
1
+ Fabricator(:history, class_name: :'CapsuleCRM::History') do
2
+ note { Faker::Lorem.paragraph }
3
+ end
@@ -0,0 +1,3 @@
1
+ Fabricator(:person, class_name: :'CapsuleCRM::Person') do
2
+ first_name { Faker::Name.first_name }
3
+ end
@@ -1,4 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe CapsuleCRM::Associations::HasMany do
4
+
5
+ describe '#has_many' do
6
+ pending
7
+ end
4
8
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe CapsuleCRM::Country do
4
+ before { configure }
5
+
6
+ describe '.all' do
7
+ before do
8
+ stub_request(:get, /\/api\/countries$/).
9
+ to_return(body: File.read('spec/support/countries.json'))
10
+ end
11
+
12
+ subject { CapsuleCRM::Country.all }
13
+
14
+ it { should be_a(Array) }
15
+
16
+ it { subject.count.should eql(2) }
17
+
18
+ it do
19
+ subject.all? { |item| item.is_a?(CapsuleCRM::Country) }.should be_true
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe CapsuleCRM::Currency do
4
+ before { configure }
5
+
6
+ describe '.all' do
7
+ before do
8
+ stub_request(:get, /\/api\/currencies$/).
9
+ to_return(body: File.read('spec/support/currencies.json'))
10
+ end
11
+
12
+ subject { CapsuleCRM::Currency.all }
13
+
14
+ it { should be_a(Array) }
15
+
16
+ it { subject.length.should eql(3) }
17
+
18
+ it do
19
+ subject.all? { |item| item.is_a?(CapsuleCRM::Currency) }.should be_true
20
+ end
21
+
22
+ it { subject.first.code.should eql('AUD') }
23
+ end
24
+ end