zoho_hub 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ece1e0545ceee8dd7c4f64e7d23ec915ee335d57e137e2548fef79a77b1f0f76
4
+ data.tar.gz: d41629b8939a7f361406d678a4844770f65dcbd820c5eb996ef235f44f290c25
5
+ SHA512:
6
+ metadata.gz: 3d1449cac85281a94053e38fd6d456bd1530d08cffd304a6d1673d5b587d1488e818b5a040611a06da4fa23feb9d7e350d1941f01a44bed975cf066b562a969b
7
+ data.tar.gz: 5eb0c0dc4b51157e52969dbaf76c3b654db9a8cda778cd3e96f7cbfe49d5aa0b9df469beac69cadebaa33961a84eaa2e45a35938ae2c0eb517c9641ed93c6b3f
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.env
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,39 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.5.1
5
+
6
+ # Don't force top level comments in every class
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ # A good line length is 100 chars
11
+ Metrics/LineLength:
12
+ Max: 100
13
+ AllowURI: true
14
+
15
+ Metrics/BlockLength:
16
+ Enabled: false
17
+
18
+ Metrics/ClassLength:
19
+ Max: 300
20
+
21
+ Metrics/MethodLength:
22
+ Max: 20
23
+
24
+ Metrics/AbcSize:
25
+ Max: 30
26
+
27
+ RSpec/ExampleLength:
28
+ Enabled: false
29
+
30
+ RSpec/MultipleExpectations:
31
+ Max: 10
32
+
33
+ RSpec/RepeatedExample:
34
+ Exclude:
35
+ - 'spec/policies/**/*'
36
+
37
+ RSpec/RepeatedDescription:
38
+ Exclude:
39
+ - 'spec/policies/**/*'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in zoho_hub.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,102 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zoho_hub (0.1.0)
5
+ activesupport (~> 5.2)
6
+ addressable (~> 2.5)
7
+ faraday (~> 0.15)
8
+ faraday_middleware (~> 0.12)
9
+ multi_json (~> 1.13)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (5.2.0)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 0.7, < 2)
17
+ minitest (~> 5.1)
18
+ tzinfo (~> 1.1)
19
+ addressable (2.5.2)
20
+ public_suffix (>= 2.0.2, < 4.0)
21
+ ast (2.4.0)
22
+ byebug (10.0.2)
23
+ coderay (1.1.2)
24
+ concurrent-ruby (1.0.5)
25
+ diff-lcs (1.3)
26
+ docile (1.3.1)
27
+ dotenv (2.5.0)
28
+ faraday (0.15.2)
29
+ multipart-post (>= 1.2, < 3)
30
+ faraday_middleware (0.12.2)
31
+ faraday (>= 0.7.4, < 1.0)
32
+ i18n (1.0.1)
33
+ concurrent-ruby (~> 1.0)
34
+ jaro_winkler (1.5.1)
35
+ json (2.1.0)
36
+ method_source (0.9.0)
37
+ minitest (5.11.3)
38
+ multi_json (1.13.1)
39
+ multipart-post (2.0.0)
40
+ parallel (1.12.1)
41
+ parser (2.5.1.2)
42
+ ast (~> 2.4.0)
43
+ powerpack (0.1.2)
44
+ pry (0.11.3)
45
+ coderay (~> 1.1.0)
46
+ method_source (~> 0.9.0)
47
+ pry-byebug (3.6.0)
48
+ byebug (~> 10.0)
49
+ pry (~> 0.10)
50
+ public_suffix (3.0.2)
51
+ rainbow (3.0.0)
52
+ rake (10.5.0)
53
+ rspec (3.7.0)
54
+ rspec-core (~> 3.7.0)
55
+ rspec-expectations (~> 3.7.0)
56
+ rspec-mocks (~> 3.7.0)
57
+ rspec-core (3.7.1)
58
+ rspec-support (~> 3.7.0)
59
+ rspec-expectations (3.7.0)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.7.0)
62
+ rspec-mocks (3.7.0)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.7.0)
65
+ rspec-support (3.7.1)
66
+ rubocop (0.58.1)
67
+ jaro_winkler (~> 1.5.1)
68
+ parallel (~> 1.10)
69
+ parser (>= 2.5, != 2.5.1.1)
70
+ powerpack (~> 0.1)
71
+ rainbow (>= 2.2.2, < 4.0)
72
+ ruby-progressbar (~> 1.7)
73
+ unicode-display_width (~> 1.0, >= 1.0.1)
74
+ rubocop-rspec (1.27.0)
75
+ rubocop (>= 0.56.0)
76
+ ruby-progressbar (1.9.0)
77
+ simplecov (0.16.1)
78
+ docile (~> 1.1)
79
+ json (>= 1.8, < 3)
80
+ simplecov-html (~> 0.10.0)
81
+ simplecov-html (0.10.2)
82
+ thread_safe (0.3.6)
83
+ tzinfo (1.2.5)
84
+ thread_safe (~> 0.1)
85
+ unicode-display_width (1.4.0)
86
+
87
+ PLATFORMS
88
+ ruby
89
+
90
+ DEPENDENCIES
91
+ bundler (~> 1.16)
92
+ dotenv (~> 2.5)
93
+ pry-byebug (~> 3.6)
94
+ rake (~> 10.0)
95
+ rspec (~> 3.0)
96
+ rubocop (~> 0.58)
97
+ rubocop-rspec (~> 1.27)
98
+ simplecov (~> 0.16)
99
+ zoho_hub!
100
+
101
+ BUNDLED WITH
102
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ricardo Otero
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # ZohoHub
2
+
3
+ https://www.zoho.com/crm/help/api-diff/
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'zoho_hub'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install zoho_hub
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run
28
+ the tests. You can also run `bin/console` for an interactive prompt that will allow you to
29
+ experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new
32
+ version, update the version number in `version.rb`, and then run `bundle exec rake release`, which
33
+ will create a git tag for the version, push git commits and tags, and push the `.gem` file
34
+ to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rikas/zoho_hub.
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'zoho_hub'
6
+
7
+ require 'dotenv'
8
+ Dotenv.load
9
+
10
+ ZohoHub.configure do |config|
11
+ config.client_id = ENV['ZOHO_CLIENT_ID']
12
+ config.secret = ENV['ZOHO_SECRET']
13
+ config.redirect_uri = ENV['ZOHO_OAUTH_REDIRECT_URL']
14
+ # config.debug = true
15
+ end
16
+
17
+ token_params = ZohoHub::Auth.refresh_token(ENV['ZOHO_REFRESH_TOKEN'])
18
+ ZohoHub.setup_connection(token_params)
19
+
20
+ include ZohoHub # rubocop:disable Style/MixinUsage
21
+
22
+ require 'pry'
23
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'multi_json'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+ require 'addressable'
7
+
8
+ module ZohoHub
9
+ class Auth
10
+ extend Forwardable
11
+
12
+ TOKEN_PATH = '/oauth/v2/token'
13
+ AUTH_PATH = '/oauth/v2/auth'
14
+
15
+ DEFAULT_SCOPES = %w[
16
+ ZohoCRM.modules.custom.all
17
+ ZohoCRM.settings.all
18
+ ZohoCRM.modules.contacts.all
19
+ ZohoCRM.modules.all
20
+ ].freeze
21
+
22
+ DEFAULT_ACCESS_TYPE = 'offline'
23
+
24
+ def_delegators :@configuration, :redirect_uri, :client_id, :secret
25
+
26
+ def initialize(access_type: nil, scopes: nil)
27
+ @configuration = ZohoHub.configuration
28
+ @access_type = access_type || ZohoHub.configuration.access_type
29
+ @scopes = scopes || DEFAULT_SCOPES
30
+ end
31
+
32
+ def token_full_uri
33
+ Addressable::URI.join(@configuration.base_url, TOKEN_PATH)
34
+ end
35
+
36
+ def auth_full_uri
37
+ Addressable::URI.join(@configuration.base_url, AUTH_PATH)
38
+ end
39
+
40
+ def auth_url
41
+ uri = auth_full_uri
42
+
43
+ uri.query_values = {
44
+ client_id: client_id,
45
+ scope: @scopes.join(','),
46
+ access_type: @access_type,
47
+ redirect_uri: redirect_uri,
48
+ response_type: 'code'
49
+ }
50
+
51
+ Addressable::URI.unencode(uri.to_s)
52
+ end
53
+
54
+ def token_url(grant_token)
55
+ uri = token_full_uri
56
+
57
+ uri.query_values = {
58
+ client_id: client_id,
59
+ client_secret: secret,
60
+ code: grant_token,
61
+ redirect_uri: redirect_uri,
62
+ grant_type: 'authorization_code'
63
+ }
64
+
65
+ Addressable::URI.unencode(uri.to_s)
66
+ end
67
+
68
+ def refresh_url(refresh_token)
69
+ uri = token_full_uri
70
+
71
+ uri.query_values = {
72
+ client_id: client_id,
73
+ client_secret: secret,
74
+ refresh_token: refresh_token,
75
+ grant_type: 'refresh_token'
76
+ }
77
+
78
+ Addressable::URI.unencode(uri.to_s)
79
+ end
80
+
81
+ def self.refresh_token(refresh_token)
82
+ new.refresh_token(refresh_token)
83
+ end
84
+
85
+ def refresh_token(refresh_token)
86
+ result = Faraday.post(refresh_url(refresh_token))
87
+
88
+ json = parse(result.body)
89
+ json.merge(refresh_token: refresh_token)
90
+ end
91
+
92
+ def revoke_refresh_token(refresh_token)
93
+ uri = token_full_uri
94
+
95
+ uri.query_values = { token: refresh_token }
96
+
97
+ url = Addressable::URI.unencode(uri.to_s)
98
+
99
+ result = Faraday.post(url)
100
+
101
+ parse(result.body)
102
+ end
103
+
104
+ def self.get_token(grant_token)
105
+ new.get_token(grant_token)
106
+ end
107
+
108
+ def get_token(grant_token)
109
+ result = Faraday.post(token_url(grant_token))
110
+
111
+ parse(result.body)
112
+ end
113
+
114
+ def parse(body)
115
+ MultiJson.load(body, symbolize_keys: true)
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ class Configuration
5
+ attr_accessor :client_id, :secret, :redirect_uri, :base_url, :access_type
6
+ attr_writer :debug
7
+
8
+ DEFAULT_BASE_URL = 'https://accounts.zoho.eu'
9
+
10
+ def initialize
11
+ @client_id = ''
12
+ @secret = ''
13
+ @redirect_uri = ''
14
+ @base_url = DEFAULT_BASE_URL
15
+ @access_type = Auth::DEFAULT_ACCESS_TYPE
16
+ end
17
+
18
+ def debug?
19
+ @debug
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ require 'zoho_hub/response'
7
+
8
+ module ZohoHub
9
+ class Connection
10
+ attr_accessor :debug, :access_token, :expires_in, :api_domain, :refresh_token
11
+
12
+ BASE_PATH = '/crm/v2/'
13
+
14
+ def initialize(access_token:, expires_in:, api_domain:, refresh_token: nil)
15
+ @access_token = access_token
16
+ @expires_in = expires_in
17
+ @api_domain = api_domain
18
+ @refresh_token ||= refresh_token # do not overwrite if it's already set
19
+ end
20
+
21
+ def get(path, params = {})
22
+ response = with_refresh { adapter.get(path, params) }
23
+ response.body
24
+ end
25
+
26
+ def post(path, params = {})
27
+ response = with_refresh { adapter.post(path, params) }
28
+ response.body
29
+ end
30
+
31
+ def access_token?
32
+ @access_token.present?
33
+ end
34
+
35
+ def refresh_token?
36
+ @refresh_token.present?
37
+ end
38
+
39
+ private
40
+
41
+ def with_refresh
42
+ http_response = yield
43
+
44
+ response = Response.new(http_response.body)
45
+
46
+ # Try to refresh the token and try again
47
+ if response.invalid_token? && refresh_token?
48
+ puts 'Refreshing outdated token...'
49
+ params = ZohoHub::Auth.refresh_token(@refresh_token)
50
+
51
+ @access_token = params[:access_token]
52
+
53
+ http_response = yield
54
+ end
55
+
56
+ http_response
57
+ end
58
+
59
+ def base_url
60
+ URI.join(@api_domain, BASE_PATH).to_s
61
+ end
62
+
63
+ # The authorization header that must be added to every request for authorized requests.
64
+ def authorization_header
65
+ { 'Authorization' => "Zoho-oauthtoken #{@access_token}" }
66
+ end
67
+
68
+ def adapter
69
+ Faraday.new(url: base_url) do |conn|
70
+ conn.headers = authorization_header if access_token?
71
+ conn.use FaradayMiddleware::ParseJson
72
+ conn.response :json, parser_options: { symbolize_names: true }
73
+ conn.response :logger if ZohoHub.configuration.debug?
74
+ conn.adapter Faraday.default_adapter
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ class RecordNotFound < StandardError
5
+ end
6
+
7
+ class RecordInvalid < StandardError
8
+ end
9
+
10
+ class InvalidTokenError < StandardError
11
+ end
12
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/records/base_record'
4
+
5
+ module ZohoHub
6
+ class Account < BaseRecord
7
+ attributes :id, :name, :territories, :company_number, :employee_count, :company_type, :industry
8
+ attributes :billing_city, :billing_code, :billing_country, :billing_street, :billing_state
9
+ attributes :account_type
10
+
11
+ # This is the ID to be used when the borrower has no organisation (unlikely) or belongs to
12
+ # multiple organisations.
13
+ CATCH_ALL_ID = '78265000000826001'
14
+
15
+ DEFAULTS = {
16
+ account_type: 'Prospect'
17
+ }.freeze
18
+
19
+ # The translation from attribute name to the JSON field on Zoho. The default behaviour will be
20
+ # to Camel_Case the attribute so on this list we should only have exceptions to this rule.
21
+ attribute_translation(
22
+ id: :id,
23
+ name: :Account_Name,
24
+ company_number: :company_reg_id,
25
+ employee_count: :No_of_Employees,
26
+ industry: :Industry_Category
27
+ )
28
+
29
+ def initialize(params)
30
+ attributes.each do |attr|
31
+ zoho_key = attr_to_zoho_key(attr)
32
+
33
+ send("#{attr}=", params[zoho_key] || params[attr] || DEFAULTS[attr])
34
+ end
35
+ end
36
+
37
+ def to_params
38
+ params = super
39
+
40
+ params
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/response'
4
+
5
+ module ZohoHub
6
+ class BaseRecord
7
+ class << self
8
+ def record_name(name = nil)
9
+ @record_name = name if name
10
+ @record_name ||= to_s.demodulize.pluralize
11
+ @record_name
12
+ end
13
+
14
+ def list_name(name = nil)
15
+ @list_name = name if name
16
+ @list_name ||= to_s.demodulize.pluralize.downcase
17
+ @list_name
18
+ end
19
+
20
+ def attributes(*attributes)
21
+ @attributes ||= []
22
+
23
+ return @attributes unless attributes
24
+
25
+ attr_accessor(*attributes)
26
+
27
+ @attributes += attributes
28
+ end
29
+
30
+ def attribute_translation(translation = nil)
31
+ @attribute_translation ||= {}
32
+
33
+ return @attribute_translation unless translation
34
+
35
+ @attribute_translation = translation
36
+ end
37
+
38
+ def zoho_key_translation
39
+ @attribute_translation.to_a.map(&:rotate).to_h
40
+ end
41
+
42
+ def find(id)
43
+ body = get(File.join(record_name, id.to_s))
44
+ response = build_response(body)
45
+
46
+ if response.empty?
47
+ raise RecordNotFound, "Couldn't find #{record_name.singularize} with 'id'=#{id}"
48
+ end
49
+
50
+ new(response.data)
51
+ end
52
+
53
+ def where(params)
54
+ response = get(list_name, params)
55
+ data = response[:data]
56
+
57
+ data.map { |info| new(info) }
58
+ end
59
+
60
+ def find_by(params)
61
+ records = where(params)
62
+ records.first
63
+ end
64
+
65
+ def create(params)
66
+ new(params).save
67
+ end
68
+
69
+ def all(options = {})
70
+ options[:page] ||= 1
71
+ options[:per_page] ||= 200
72
+
73
+ body = get(list_name, options)
74
+ response = build_response(body)
75
+
76
+ data = response.nil? ? [] : response.data
77
+
78
+ data.map { |info| new(info) }
79
+ end
80
+
81
+ def get(path, params = {})
82
+ ZohoHub.connection.get(path, params)
83
+ end
84
+
85
+ def post(path, params = {})
86
+ puts "POSTING TO `#{path}` -- #{params}"
87
+
88
+ ZohoHub.connection.post(path, params.to_json)
89
+ end
90
+
91
+ def exists?(id)
92
+ !find(id).nil?
93
+ rescue RecordNotFound
94
+ false
95
+ end
96
+
97
+ alias exist? exists?
98
+
99
+ def build_response(body)
100
+ response = Response.new(body)
101
+
102
+ raise InvalidTokenError, response.msg if response.invalid_token?
103
+ raise RecordInvalid, response.msg if response.invalid_data?
104
+
105
+ response
106
+ end
107
+ end
108
+
109
+ def attributes
110
+ self.class.attributes
111
+ end
112
+
113
+ def get(path, params = {})
114
+ self.class.get(path, params)
115
+ end
116
+
117
+ def post(path, params = {})
118
+ self.class.post(path, params)
119
+ end
120
+
121
+ def save
122
+ body = post(self.class.record_name, data: [to_params])
123
+ response = build_response(body)
124
+
125
+ response.data.dig(:details, :id)
126
+ end
127
+
128
+ def to_params
129
+ params = {}
130
+
131
+ attributes.each do |attr|
132
+ key = attr_to_zoho_key(attr)
133
+
134
+ params[key] = send(attr)
135
+ end
136
+
137
+ params
138
+ end
139
+
140
+ def build_response(body)
141
+ self.class.build_response(body)
142
+ end
143
+
144
+ private
145
+
146
+ def attr_to_zoho_key(attr_name)
147
+ translations = self.class.attribute_translation
148
+
149
+ return translations[attr_name.to_sym] if translations.key?(attr_name.to_sym)
150
+
151
+ attr_name.to_s.split('_').map(&:capitalize).join('_').to_sym
152
+ end
153
+
154
+ def zoho_key_to_attr(zoho_key)
155
+ translations = self.class.zoho_key_translation
156
+
157
+ return translations[zoho_key.to_sym] if translations.key?(zoho_key.to_sym)
158
+
159
+ zoho_key.to_sym
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/records/base_record'
4
+
5
+ module ZohoHub
6
+ class Campaign < BaseRecord
7
+ attributes :id, :name
8
+
9
+ def initialize(params)
10
+ @id = params[:id]
11
+ @name = params[:Campaign_Name]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/records/base_record'
4
+
5
+ module ZohoHub
6
+ class Contact < BaseRecord
7
+ attributes :id, :email, :salutation, :first_name, :mobile, :role, :last_name
8
+ attributes :account_id, :owner_id, :campaign_id, :status, :campaign_detail
9
+
10
+ attribute_translation(
11
+ id: :id,
12
+ role: :platform_cont_type,
13
+ status: :platform_cont_status,
14
+ use_proceeds: :use_proceeds
15
+ )
16
+
17
+ DEFAULTS = {
18
+ status: 'active',
19
+ campaign_detail: 'Web Sign Up'
20
+ }.freeze
21
+
22
+ def initialize(params)
23
+ attributes.each do |attr|
24
+ zoho_key = attr_to_zoho_key(attr)
25
+
26
+ send("#{attr}=", params[zoho_key] || params[attr] || DEFAULTS[attr])
27
+ end
28
+
29
+ # Setup values as they come from the Zoho API if needed
30
+ @account_id ||= params.dig(:Account_Name, :id)
31
+ @owner_id ||= params.dig(:Owner, :id)
32
+ @campaign_id ||= params.dig(:Campaign_Lookup, :id)
33
+ end
34
+
35
+ def to_params
36
+ params = super
37
+
38
+ params[:Account_Name] = { id: @account_id } if @account_id
39
+ params[:Owner] = { id: @owner_id } if @owner_id
40
+ params[:Campaign_Lookup] = { id: @campaign_id } if @campaign_id
41
+
42
+ params
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/records/base_record'
4
+
5
+ module ZohoHub
6
+ class Potential < BaseRecord
7
+ list_name 'Potentials'
8
+
9
+ attributes :id, :code, :deal_name, :amount, :description, :stage
10
+ attributes :company_age_years, :company_age_months, :term, :use_proceeds, :proceeds_detail
11
+ attributes :currency, :territory, :employee_count, :turnover, :industry, :region
12
+ attributes :review_outcome, :first_created, :last_modified, :preferred_term
13
+ attributes :campaign_id, :account_id, :contact_id, :campaign_detail
14
+
15
+ DEFAULTS = {
16
+ currency: 'GBP',
17
+ territory: 'UK all',
18
+ campaign_detail: 'Web Sign Up'
19
+ }.freeze
20
+
21
+ # The translation from attribute name to the JSON field on Zoho. The default behaviour will be
22
+ # to Camel_Case the attribute so on this list we should only have exceptions to this rule.
23
+ attribute_translation(
24
+ id: :id,
25
+ code: :Project_Ref_No,
26
+ description: :Project_description,
27
+ employee_count: :Number_of_Employees,
28
+ use_proceeds: :use_proceeds
29
+ )
30
+
31
+ def initialize(params)
32
+ attributes.each do |attr|
33
+ zoho_key = attr_to_zoho_key(attr)
34
+
35
+ send("#{attr}=", params[zoho_key] || params[attr] || DEFAULTS[attr])
36
+ end
37
+
38
+ # Setup values as they come from the Zoho API if needed
39
+ @account_id ||= params.dig(:Account_Name, :id)
40
+ @contact_id ||= params.dig(:Contact_Name, :id)
41
+ @campaign_id ||= params.dig(:Campaign_Source, :id)
42
+ end
43
+
44
+ def to_params
45
+ params = super
46
+
47
+ params[:Campaign_Source] = { id: @campaign_id } if @campaign_id
48
+ params[:Account_Name] = { id: @account_id } if @account_id
49
+ params[:Contact_Name] = { id: @contact_id } if @contact_id
50
+
51
+ params
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ class Response
5
+ def initialize(params)
6
+ @params = params || {}
7
+ end
8
+
9
+ def invalid_data?
10
+ data[:code] == 'INVALID_DATA'
11
+ end
12
+
13
+ # {:code=>"INVALID_TOKEN", :details=>{}, :message=>"invalid oauth token", :status=>"error"}
14
+ def invalid_token?
15
+ data[:code] == 'INVALID_TOKEN'
16
+ end
17
+
18
+ def empty?
19
+ @params.empty?
20
+ end
21
+
22
+ def data
23
+ data = @params[:data] if @params.dig(:data)
24
+ data ||= @params
25
+
26
+ return data.first if data.is_a?(Array) && data.size == 1
27
+
28
+ data
29
+ end
30
+
31
+ def msg
32
+ msg = data[:message]
33
+
34
+ if data.dig(:details, :expected_data_type)
35
+ expected = data.dig(:details, :expected_data_type)
36
+ field = data.dig(:details, :api_name)
37
+
38
+ msg << ", expected #{expected} for '#{field}'"
39
+ end
40
+
41
+ msg
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ VERSION = '0.1.0'
5
+ end
data/lib/zoho_hub.rb ADDED
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'active_support/core_ext/string/inflections'
5
+
6
+ require 'zoho_hub/version'
7
+ require 'zoho_hub/auth'
8
+ require 'zoho_hub/configuration'
9
+ require 'zoho_hub/connection'
10
+ require 'zoho_hub/errors'
11
+ require 'zoho_hub/records/contact'
12
+ require 'zoho_hub/records/potential'
13
+ require 'zoho_hub/records/campaign'
14
+ require 'zoho_hub/records/account'
15
+
16
+ module ZohoHub
17
+ module_function
18
+
19
+ def configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def connection
24
+ @connection
25
+ end
26
+
27
+ def setup_connection(params = {})
28
+ raise "ERROR: #{params[:error]}" if params[:error]
29
+
30
+ connection_params = params.slice(:access_token, :expires_in, :api_domain, :refresh_token)
31
+
32
+ @connection = Connection.new(connection_params)
33
+ end
34
+
35
+ def refresh_token?
36
+ return false unless connection.present?
37
+
38
+ connection.refresh_token?
39
+ end
40
+
41
+ def access_token?
42
+ return false unless connection.present?
43
+
44
+ connection.access_token?
45
+ end
46
+
47
+ def configure
48
+ yield(configuration)
49
+ end
50
+ end
data/zoho_hub.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'zoho_hub/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'zoho_hub'
10
+ spec.version = ZohoHub::VERSION
11
+ spec.authors = ['Ricardo Otero']
12
+ spec.email = ['oterosantos@gmail.com']
13
+
14
+ spec.summary = 'Simple gem to connect to Zoho CRM API V2'
15
+ spec.description = spec.summary
16
+ spec.homepage = 'https://github.com/rikas/zoho_hub'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency 'activesupport', '~> 5.2'
27
+ spec.add_dependency 'addressable', '~> 2.5'
28
+ spec.add_dependency 'faraday', '~> 0.15'
29
+ spec.add_dependency 'faraday_middleware', '~> 0.12'
30
+ spec.add_dependency 'multi_json', '~> 1.13'
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.16'
33
+ spec.add_development_dependency 'dotenv', '~> 2.5'
34
+ spec.add_development_dependency 'pry-byebug', '~> 3.6'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'rubocop', '~> 0.58'
38
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.27'
39
+ spec.add_development_dependency 'simplecov', '~> 0.16'
40
+ end
metadata ADDED
@@ -0,0 +1,251 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zoho_hub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ricardo Otero
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.15'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.13'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.16'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.16'
97
+ - !ruby/object:Gem::Dependency
98
+ name: dotenv
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.5'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.5'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.6'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.6'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '10.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '10.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.58'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.58'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '1.27'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '1.27'
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '0.16'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '0.16'
195
+ description: Simple gem to connect to Zoho CRM API V2
196
+ email:
197
+ - oterosantos@gmail.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".gitignore"
203
+ - ".rspec"
204
+ - ".rubocop.yml"
205
+ - ".ruby-version"
206
+ - ".travis.yml"
207
+ - Gemfile
208
+ - Gemfile.lock
209
+ - LICENSE.txt
210
+ - README.md
211
+ - Rakefile
212
+ - bin/console
213
+ - bin/setup
214
+ - lib/zoho_hub.rb
215
+ - lib/zoho_hub/auth.rb
216
+ - lib/zoho_hub/configuration.rb
217
+ - lib/zoho_hub/connection.rb
218
+ - lib/zoho_hub/errors.rb
219
+ - lib/zoho_hub/records/account.rb
220
+ - lib/zoho_hub/records/base_record.rb
221
+ - lib/zoho_hub/records/campaign.rb
222
+ - lib/zoho_hub/records/contact.rb
223
+ - lib/zoho_hub/records/potential.rb
224
+ - lib/zoho_hub/response.rb
225
+ - lib/zoho_hub/version.rb
226
+ - zoho_hub.gemspec
227
+ homepage: https://github.com/rikas/zoho_hub
228
+ licenses:
229
+ - MIT
230
+ metadata: {}
231
+ post_install_message:
232
+ rdoc_options: []
233
+ require_paths:
234
+ - lib
235
+ required_ruby_version: !ruby/object:Gem::Requirement
236
+ requirements:
237
+ - - ">="
238
+ - !ruby/object:Gem::Version
239
+ version: '0'
240
+ required_rubygems_version: !ruby/object:Gem::Requirement
241
+ requirements:
242
+ - - ">="
243
+ - !ruby/object:Gem::Version
244
+ version: '0'
245
+ requirements: []
246
+ rubyforge_project:
247
+ rubygems_version: 2.7.6
248
+ signing_key:
249
+ specification_version: 4
250
+ summary: Simple gem to connect to Zoho CRM API V2
251
+ test_files: []