zoho_hub 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -1
- data/lib/zoho_hub/connection.rb +23 -2
- data/lib/zoho_hub/records/base_record.rb +19 -3
- data/lib/zoho_hub/version.rb +1 -1
- data/lib/zoho_hub.rb +4 -0
- data/zoho_hub.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1938ef89a8cb4c065f45041cdf03ec2717b492527981cd902c8a51d6d3ef9fa5
|
4
|
+
data.tar.gz: 508e31af153148c523aa384c8939719a9e06ce8186ed3f232c788664232ac9f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4300146ef69e512906edafef468a65519e59dc766ea8d4f107d1c8e38189ff75b1cc4d442dedd46a39551bcce00f84d42e0aec674f6c8d3b8b7dd1d2664c75f
|
7
|
+
data.tar.gz: 2f1cb9102a1d2a9d92d70cb285e539237608452a859ea6337c88d4a850b93e5631516025de9548430250f13fe85480dd2b9f38d1ee6bc4fd9a606f88aa017b24
|
data/Gemfile.lock
CHANGED
data/lib/zoho_hub/connection.rb
CHANGED
@@ -2,16 +2,18 @@
|
|
2
2
|
|
3
3
|
require 'faraday'
|
4
4
|
require 'faraday_middleware'
|
5
|
+
require 'rainbow'
|
5
6
|
|
6
7
|
require 'zoho_hub/response'
|
7
8
|
|
8
9
|
module ZohoHub
|
9
10
|
class Connection
|
10
11
|
attr_accessor :debug, :access_token, :expires_in, :api_domain, :refresh_token
|
12
|
+
attr_accessor :on_refresh_cb
|
11
13
|
|
12
14
|
BASE_PATH = '/crm/v2/'
|
13
15
|
|
14
|
-
def initialize(access_token:,
|
16
|
+
def initialize(access_token:, api_domain:, expires_in: 3600, refresh_token: nil)
|
15
17
|
@access_token = access_token
|
16
18
|
@expires_in = expires_in
|
17
19
|
@api_domain = api_domain
|
@@ -19,15 +21,26 @@ module ZohoHub
|
|
19
21
|
end
|
20
22
|
|
21
23
|
def get(path, params = {})
|
24
|
+
log "GET #{path} with #{params}"
|
25
|
+
|
22
26
|
response = with_refresh { adapter.get(path, params) }
|
23
27
|
response.body
|
24
28
|
end
|
25
29
|
|
26
30
|
def post(path, params = {})
|
31
|
+
log "POST #{path} with #{params}"
|
32
|
+
|
27
33
|
response = with_refresh { adapter.post(path, params) }
|
28
34
|
response.body
|
29
35
|
end
|
30
36
|
|
37
|
+
def put(path, params = {})
|
38
|
+
log "PUT #{path} with #{params}"
|
39
|
+
|
40
|
+
response = with_refresh { adapter.put(path, params) }
|
41
|
+
response.body
|
42
|
+
end
|
43
|
+
|
31
44
|
def access_token?
|
32
45
|
@access_token.present?
|
33
46
|
end
|
@@ -36,6 +49,12 @@ module ZohoHub
|
|
36
49
|
@refresh_token.present?
|
37
50
|
end
|
38
51
|
|
52
|
+
def log(text)
|
53
|
+
return unless ZohoHub.configuration.debug?
|
54
|
+
|
55
|
+
puts Rainbow("[ZohoHub] #{text}").magenta.bright
|
56
|
+
end
|
57
|
+
|
39
58
|
private
|
40
59
|
|
41
60
|
def with_refresh
|
@@ -45,9 +64,11 @@ module ZohoHub
|
|
45
64
|
|
46
65
|
# Try to refresh the token and try again
|
47
66
|
if response.invalid_token? && refresh_token?
|
48
|
-
|
67
|
+
log "Refreshing outdated token... #{@access_token}"
|
49
68
|
params = ZohoHub::Auth.refresh_token(@refresh_token)
|
50
69
|
|
70
|
+
@on_refresh_cb.call(params) if @on_refresh_cb.present?
|
71
|
+
|
51
72
|
@access_token = params[:access_token]
|
52
73
|
|
53
74
|
http_response = yield
|
@@ -83,11 +83,13 @@ module ZohoHub
|
|
83
83
|
end
|
84
84
|
|
85
85
|
def post(path, params = {})
|
86
|
-
puts "POSTING TO `#{path}` -- #{params}"
|
87
|
-
|
88
86
|
ZohoHub.connection.post(path, params.to_json)
|
89
87
|
end
|
90
88
|
|
89
|
+
def put(path, params = {})
|
90
|
+
ZohoHub.connection.put(path, params.to_json)
|
91
|
+
end
|
92
|
+
|
91
93
|
def exists?(id)
|
92
94
|
!find(id).nil?
|
93
95
|
rescue RecordNotFound
|
@@ -118,13 +120,27 @@ module ZohoHub
|
|
118
120
|
self.class.post(path, params)
|
119
121
|
end
|
120
122
|
|
123
|
+
def put(path, params = {})
|
124
|
+
self.class.put(path, params)
|
125
|
+
end
|
126
|
+
|
121
127
|
def save
|
122
|
-
body =
|
128
|
+
body = if new_record? # create new record
|
129
|
+
post(self.class.record_name, data: [to_params])
|
130
|
+
else # update existing record
|
131
|
+
path = File.join(self.class.record_name, id)
|
132
|
+
put(path, data: [to_params])
|
133
|
+
end
|
134
|
+
|
123
135
|
response = build_response(body)
|
124
136
|
|
125
137
|
response.data.dig(:details, :id)
|
126
138
|
end
|
127
139
|
|
140
|
+
def new_record?
|
141
|
+
!id.present?
|
142
|
+
end
|
143
|
+
|
128
144
|
def to_params
|
129
145
|
params = {}
|
130
146
|
|
data/lib/zoho_hub/version.rb
CHANGED
data/lib/zoho_hub.rb
CHANGED
data/zoho_hub.gemspec
CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_dependency 'faraday', '~> 0.15'
|
29
29
|
spec.add_dependency 'faraday_middleware', '~> 0.12'
|
30
30
|
spec.add_dependency 'multi_json', '~> 1.13'
|
31
|
+
spec.add_dependency 'rainbow', '~> 3.0'
|
31
32
|
|
32
33
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
33
34
|
spec.add_development_dependency 'dotenv', '~> 2.5'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zoho_hub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Otero
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '1.13'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rainbow
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: bundler
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|