capsule_crm 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b2ac3b80afb32c0e5e01fb90ed2677ec5bbc633
4
- data.tar.gz: 0e12ab771e075cad5a39598da55d81015111620a
3
+ metadata.gz: c009a947688f5f89bc4b47b49423502a91523bb9
4
+ data.tar.gz: 0efb281b53de063eff58eb3ae377111fe1a1399e
5
5
  SHA512:
6
- metadata.gz: 377306fa489bb3aa8e2f1c19a64d233b47763b7d6ca250fa32ac30a5a536173f1c726dc17b8d402dced4578f9694797fd776095ed811c9d8e8bc6e047d9ec2bc
7
- data.tar.gz: 6a34894ec8bd04703a4767dbebe8a91c48164f477a079729be646e8d59e21d4f074bcc7d11fc7c8b6b5ae60215ca1f38f7e53107cc439c03bc5dfaafc70aa767
6
+ metadata.gz: 4b90a009a0257c2f54885f361191cbf36afce7fe0341f71b18afc4821eaca7d23a7cc095507c71447d1e533d33c301138047b6dc0b54217dd5694d78036221c2
7
+ data.tar.gz: b621f0a7989688a34e4505fb7952b8f69f26e17f6c715acd313b6a8ea4f4a3f0139334affebb8f02fa35535904b73f32fa7327e3c62e4a42bd3e94af20714751
data/lib/capsule_crm.rb CHANGED
@@ -24,9 +24,10 @@ require 'capsule_crm/user'
24
24
  require 'capsule_crm/website'
25
25
  require 'capsule_crm/hash_helper'
26
26
  require 'capsule_crm/results_proxy'
27
+ require 'capsule_crm/errors'
27
28
  require 'capsule_crm/errors/record_invalid'
28
29
  require 'capsule_crm/errors/record_not_saved'
29
- require 'capsule_crm/errors/unauthorized'
30
+ require 'capsule_crm/errors/response_error'
30
31
  require 'capsule_crm/contacts'
31
32
  require 'capsule_crm/contactable'
32
33
  require 'capsule_crm/configuration'
@@ -38,6 +39,7 @@ require 'capsule_crm/person'
38
39
  require 'capsule_crm/milestone'
39
40
  require 'capsule_crm/track'
40
41
  require 'capsule_crm/version'
42
+ require 'capsule_crm/faraday/middleware/raise_error'
41
43
 
42
44
  module CapsuleCRM
43
45
 
@@ -29,14 +29,6 @@ module CapsuleCRM
29
29
  def self.request(method, path, params)
30
30
  faraday.send(method, path, params) do |req|
31
31
  req.headers.update default_request_headers
32
- end.tap do |response|
33
- check_response_status(response)
34
- end
35
- end
36
-
37
- def self.check_response_status(response)
38
- if response.status == 401
39
- raise CapsuleCRM::Errors::Unauthorized.new(response)
40
32
  end
41
33
  end
42
34
 
@@ -67,9 +59,10 @@ module CapsuleCRM
67
59
  end
68
60
 
69
61
  def self.faraday
70
- Faraday.new("https://#{subdomain}.capsulecrm.com").tap do |connection|
62
+ ::Faraday.new("https://#{subdomain}.capsulecrm.com").tap do |connection|
71
63
  connection.basic_auth(CapsuleCRM.configuration.api_token, '')
72
64
  connection.request :json
65
+ connection.use CapsuleCRM::Faraday::Middleware::RaiseError
73
66
  end
74
67
  end
75
68
 
@@ -0,0 +1,48 @@
1
+ require_relative 'errors/response_error'
2
+
3
+ module CapsuleCRM
4
+ module Errors
5
+ BAD_REQUEST = 400
6
+ NOT_AUTHORIZED = 401
7
+ NOT_FOUND = 404
8
+ INTERNAL_SERVER_ERROR = 500
9
+
10
+ class << self
11
+ def error_constants
12
+ self.constants.each_with_object({}) do |name, hash|
13
+ next if (code = Errors.const_get(name)).is_a?(Class)
14
+ hash[name] = code
15
+ end
16
+ end
17
+
18
+ def class_name_for_error_name(name)
19
+ name.to_s.titleize.gsub(' ', '')
20
+ end
21
+
22
+ def class_for_error_name(name)
23
+ class_name = class_name_for_error_name(name)
24
+ if const_defined?(class_name)
25
+ CapsuleCRM::Errors.const_get(class_name)
26
+ else
27
+ CapsuleCRM::Errors::InternalServerError
28
+ end
29
+ end
30
+
31
+ def class_for_error_code(code)
32
+ name = error_constants.select { |k, v| v == code }.keys.first
33
+ if name.present?
34
+ class_for_error_name(name)
35
+ else
36
+ CapsuleCRM::Errors::InternalServerError
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ CapsuleCRM::Errors.error_constants.each do |name, code|
44
+ klass = Class.new(CapsuleCRM::Errors::ResponseError)
45
+ klass.send(:define_method, :code) { code }
46
+ klass_name = CapsuleCRM::Errors.class_name_for_error_name(name)
47
+ CapsuleCRM::Errors.const_set(klass_name, klass)
48
+ end
@@ -1,6 +1,6 @@
1
1
  module CapsuleCRM
2
2
  module Errors
3
- class Unauthorized < StandardError
3
+ class ResponseError < StandardError
4
4
  attr_reader :response
5
5
 
6
6
  def initialize(response)
@@ -0,0 +1,13 @@
1
+ module CapsuleCRM
2
+ module Faraday
3
+ module Middleware
4
+ class RaiseError < ::Faraday::Response::Middleware
5
+ def on_complete(env)
6
+ return if env[:status] < 400
7
+ raise CapsuleCRM::Errors.class_for_error_code(env[:status]).
8
+ new(env[:response])
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module CapsuleCrm
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -25,8 +25,8 @@ describe CapsuleCRM::Connection do
25
25
  end
26
26
  subject { CapsuleCRM::Connection.get('/api/v1/foo') }
27
27
 
28
- it 'should raise an Unauthorized error' do
29
- expect { subject }.to raise_error(CapsuleCRM::Errors::Unauthorized)
28
+ it 'should raise an NotAuthorized error' do
29
+ expect { subject }.to raise_error(CapsuleCRM::Errors::NotAuthorized)
30
30
  end
31
31
  end
32
32
  end
@@ -64,8 +64,8 @@ describe CapsuleCRM::Connection do
64
64
  stub_request(:post, /.*/).to_return(status: 401, body: '<html></html>')
65
65
  end
66
66
 
67
- it 'should raise an Unauthorized error' do
68
- expect { subject }.to raise_error(CapsuleCRM::Errors::Unauthorized)
67
+ it 'should raise an NotAuthorized error' do
68
+ expect { subject }.to raise_error(CapsuleCRM::Errors::NotAuthorized)
69
69
  end
70
70
  end
71
71
  end
@@ -88,8 +88,8 @@ describe CapsuleCRM::Connection do
88
88
  stub_request(:put, /\.*/).to_return(status: 401)
89
89
  end
90
90
 
91
- it 'should raise an Unauthorized error' do
92
- expect { subject }.to raise_error(CapsuleCRM::Errors::Unauthorized)
91
+ it 'should raise an NotAuthorized error' do
92
+ expect { subject }.to raise_error(CapsuleCRM::Errors::NotAuthorized)
93
93
  end
94
94
  end
95
95
  end
@@ -112,8 +112,8 @@ describe CapsuleCRM::Connection do
112
112
  stub_request(:delete, /.*/).to_return(status: 401)
113
113
  end
114
114
 
115
- it 'should raise an Unauthorized error' do
116
- expect { subject }.to raise_error(CapsuleCRM::Errors::Unauthorized)
115
+ it 'should raise an NotAuthorized error' do
116
+ expect { subject }.to raise_error(CapsuleCRM::Errors::NotAuthorized)
117
117
  end
118
118
  end
119
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capsule_crm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Beedle
@@ -271,9 +271,11 @@ files:
271
271
  - lib/capsule_crm/currency.rb
272
272
  - lib/capsule_crm/custom_field.rb
273
273
  - lib/capsule_crm/email.rb
274
+ - lib/capsule_crm/errors.rb
274
275
  - lib/capsule_crm/errors/record_invalid.rb
275
276
  - lib/capsule_crm/errors/record_not_saved.rb
276
- - lib/capsule_crm/errors/unauthorized.rb
277
+ - lib/capsule_crm/errors/response_error.rb
278
+ - lib/capsule_crm/faraday/middleware/raise_error.rb
277
279
  - lib/capsule_crm/hash_helper.rb
278
280
  - lib/capsule_crm/history.rb
279
281
  - lib/capsule_crm/milestone.rb