zoho_hub 0.1.0 → 0.1.3

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
  SHA256:
3
- metadata.gz: ece1e0545ceee8dd7c4f64e7d23ec915ee335d57e137e2548fef79a77b1f0f76
4
- data.tar.gz: d41629b8939a7f361406d678a4844770f65dcbd820c5eb996ef235f44f290c25
3
+ metadata.gz: 1938ef89a8cb4c065f45041cdf03ec2717b492527981cd902c8a51d6d3ef9fa5
4
+ data.tar.gz: 508e31af153148c523aa384c8939719a9e06ce8186ed3f232c788664232ac9f1
5
5
  SHA512:
6
- metadata.gz: 3d1449cac85281a94053e38fd6d456bd1530d08cffd304a6d1673d5b587d1488e818b5a040611a06da4fa23feb9d7e350d1941f01a44bed975cf066b562a969b
7
- data.tar.gz: 5eb0c0dc4b51157e52969dbaf76c3b654db9a8cda778cd3e96f7cbfe49d5aa0b9df469beac69cadebaa33961a84eaa2e45a35938ae2c0eb517c9641ed93c6b3f
6
+ metadata.gz: e4300146ef69e512906edafef468a65519e59dc766ea8d4f107d1c8e38189ff75b1cc4d442dedd46a39551bcce00f84d42e0aec674f6c8d3b8b7dd1d2664c75f
7
+ data.tar.gz: 2f1cb9102a1d2a9d92d70cb285e539237608452a859ea6337c88d4a850b93e5631516025de9548430250f13fe85480dd2b9f38d1ee6bc4fd9a606f88aa017b24
data/Gemfile.lock CHANGED
@@ -1,12 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zoho_hub (0.1.0)
4
+ zoho_hub (0.1.3)
5
5
  activesupport (~> 5.2)
6
6
  addressable (~> 2.5)
7
7
  faraday (~> 0.15)
8
8
  faraday_middleware (~> 0.12)
9
9
  multi_json (~> 1.13)
10
+ rainbow (~> 3.0)
10
11
 
11
12
  GEM
12
13
  remote: https://rubygems.org/
@@ -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:, expires_in:, api_domain:, refresh_token: nil)
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
- puts 'Refreshing outdated token...'
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 = post(self.class.record_name, data: [to_params])
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ZohoHub
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.3'
5
5
  end
data/lib/zoho_hub.rb CHANGED
@@ -24,6 +24,10 @@ module ZohoHub
24
24
  @connection
25
25
  end
26
26
 
27
+ def on_refresh(&block)
28
+ @connection.on_refresh_cb = block
29
+ end
30
+
27
31
  def setup_connection(params = {})
28
32
  raise "ERROR: #{params[:error]}" if params[:error]
29
33
 
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.0
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-20 00:00:00.000000000 Z
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