zoho_hub 0.1.56 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -2
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +5 -2
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +5 -2
  7. data/Gemfile +0 -10
  8. data/README.md +91 -7
  9. data/bin/console +6 -7
  10. data/bin/read +44 -0
  11. data/bin/setup +0 -2
  12. data/bin/zoho_hub +62 -0
  13. data/cache/.git_keep +0 -0
  14. data/lib/zoho_hub/auth.rb +38 -47
  15. data/lib/zoho_hub/base_record.rb +122 -0
  16. data/lib/zoho_hub/configuration.rb +3 -4
  17. data/lib/zoho_hub/connection.rb +12 -17
  18. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/account.rb +1 -1
  19. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/contact.rb +3 -9
  20. data/lib/zoho_hub/deprecated_and_only_for_reference_records/funder.rb +9 -0
  21. data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/potential.rb +1 -25
  22. data/lib/zoho_hub/deprecated_and_only_for_reference_records/product.rb +9 -0
  23. data/lib/zoho_hub/deprecated_and_only_for_reference_records/quote.rb +34 -0
  24. data/lib/zoho_hub/errors.rb +0 -3
  25. data/lib/zoho_hub/module_builder.rb +61 -0
  26. data/lib/zoho_hub/oauth_callback_server.rb +26 -0
  27. data/lib/zoho_hub/response.rb +1 -4
  28. data/lib/zoho_hub/settings/field.rb +33 -0
  29. data/lib/zoho_hub/settings/module.rb +49 -0
  30. data/lib/zoho_hub/string_utils.rb +34 -0
  31. data/lib/zoho_hub/version.rb +1 -1
  32. data/lib/zoho_hub/views/variables.erb +72 -0
  33. data/lib/zoho_hub/with_attributes.rb +74 -0
  34. data/lib/zoho_hub/with_connection.rb +36 -0
  35. data/lib/zoho_hub.rb +21 -23
  36. data/zoho_hub.gemspec +21 -8
  37. metadata +180 -45
  38. data/lib/zoho_hub/records/adverse_criteria.rb +0 -43
  39. data/lib/zoho_hub/records/attachment.rb +0 -106
  40. data/lib/zoho_hub/records/base_record.rb +0 -189
  41. data/lib/zoho_hub/records/credit_score.rb +0 -36
  42. data/lib/zoho_hub/records/product.rb +0 -33
  43. data/lib/zoho_hub/records/quote.rb +0 -44
  44. data/lib/zoho_hub/records/sms_message.rb +0 -32
  45. data/lib/zoho_hub/records/vendor.rb +0 -34
  46. /data/lib/zoho_hub/{records → deprecated_and_only_for_reference_records}/campaign.rb +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7544966786b048c22ba93937fc016c518ea8c22cbb6db53afa3a607dfbe220db
4
- data.tar.gz: 8883e3ef94716a75c18bc5f3927167e9f8bddc9257b8ae7f5d76beac8663b535
3
+ metadata.gz: bb0b0886637feb21a98d0a73f40cb3571a0ec2507dfd45b7eec9fd348b365276
4
+ data.tar.gz: 70638084911ec64dc02095d4cf085453ca13ba4ea28a3c983c79e235a1a5911e
5
5
  SHA512:
6
- metadata.gz: 3922f23af49e34821ab0e35d11d5453e61776c9887aa716d1982ca381adb048a9c43315bd1bae43ee30c420a7378ef8e48dd39d9973dcac3e8b5a90e28857920
7
- data.tar.gz: 8d6b0741b78aa4790f602a30ff4ffb3f893ef3dc6459d63f7bbc4ef6f02128e30e9aedd83a713f1fe5ad1048eb987379dc08222162a925ca5b29050d17c2b58d
6
+ metadata.gz: c5fc255597ced5c966a59b0db9de72ab6a67defca6c1a62e5e168f37bc70326f2c1cccd5cf3ab44369a94372277ca42c671965e50d742b315b26126882f1af72
7
+ data.tar.gz: 89fbe288d53b9ebf9c76f8229981deffcacc9d6fe484b6187f654430eb097c7e1b57719dafcc953cafbfafeec7f1d765c87794697b3d283b168cbd2c9f35ff03
data/.gitignore CHANGED
@@ -7,9 +7,8 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /cache/*
10
11
  Gemfile.lock
11
- /cache/
12
- .DS_Store
13
12
 
14
13
  # rspec failure tracking
15
14
  .rspec_status
data/.rspec CHANGED
@@ -1,3 +1,4 @@
1
1
  --format documentation
2
2
  --color
3
3
  --require spec_helper
4
+ --order rand
data/.rubocop.yml CHANGED
@@ -1,13 +1,16 @@
1
1
  require: rubocop-rspec
2
2
 
3
3
  AllCops:
4
- TargetRubyVersion: 3.2.0
5
- NewCops: enable
4
+ TargetRubyVersion: 2.5.1
6
5
 
7
6
  # Don't force top level comments in every class
8
7
  Style/Documentation:
9
8
  Enabled: false
10
9
 
10
+ # Safe navigation is not available in older ruby versions
11
+ Style/SafeNavigation:
12
+ Enabled: false
13
+
11
14
  # A good line length is 100 chars
12
15
  Metrics/LineLength:
13
16
  Max: 100
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.2.0
1
+ 2.5.3
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.5.0
5
- before_install: gem install bundler -v 2.0.1
4
+ - 2.1.10
5
+ - 2.2.10
6
+ - 2.3.7
7
+ - 2.4.4
8
+ - 2.5.3
data/Gemfile CHANGED
@@ -6,13 +6,3 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  # Specify your gem's dependencies in zoho_hub.gemspec
8
8
  gemspec
9
-
10
- # Development dependencies
11
- gem 'bundler'
12
- gem 'dotenv', '~> 2.8'
13
- gem 'pry-byebug', '~> 3.10'
14
- gem 'rake'
15
- gem 'rspec'
16
- gem 'rubocop'
17
- gem 'rubocop-rspec'
18
- gem 'simplecov'
data/README.md CHANGED
@@ -1,6 +1,16 @@
1
1
  # ZohoHub
2
2
 
3
- https://www.zoho.com/crm/help/api-diff/
3
+ [![Build Status](https://travis-ci.com/rikas/zoho_hub.svg?branch=master)](https://travis-ci.com/rikas/zoho_hub)
4
+ [![Gem Version](https://badge.fury.io/rb/zoho_hub.svg)](https://badge.fury.io/rb/zoho_hub)
5
+
6
+ Simple wrapper around Zoho CRM version2, using [OAuth 2.0 protocol](https://www.zoho.com/crm/help/api/v2/#OAuth2_0)
7
+ for authentication.
8
+
9
+ This gem reads your Module configuration and builds the corresponding classes for you, using some
10
+ reflection mechanisms. You should then be able to use simple classes with an API close to
11
+ ActiveRecord, to do CRUD operations.
12
+
13
+ **NOTE: this gem is WIP, please try to use it and open an issue if you run into limitations / problems**
4
14
 
5
15
  ## Installation
6
16
 
@@ -18,9 +28,86 @@ Or install it yourself as:
18
28
 
19
29
  $ gem install zoho_hub
20
30
 
21
- ## Usage
31
+ ## Setup process
32
+
33
+ ### 1. Register your application
34
+
35
+ If you want to access your Zoho CRM account from your application you first need to create your
36
+ application as described here: https://www.zoho.com/crm/help/api/v2/#oauth-request.
37
+
38
+ **TODO TODO - explain "Authorized redirect URIs"**
39
+
40
+ This will give you a **Client ID** and a **secret**, that you'll use in the next step.
41
+
42
+ ### 2. Configure ZohoHub with your credentials
43
+
44
+ You need to have a configuration block like the one below (in rails add a `zoho_hub.rb` in your
45
+ `config/initializers` dir):
46
+
47
+ ```ruby
48
+ ZohoHub.configure do |config|
49
+ config.client_id = 'YOUR_ZOHO_CLIENT_ID' # obtained in 1.
50
+ config.secret = 'YOUR_ZOHO_SECRET' # obtained in 1.
51
+ config.redirect_uri = 'YOUR_ZOHO_OAUTH_REDIRECT_URL'
52
+ # config.debug = true # this will be VERY verbose, but helps to identify bugs / problems
53
+ end
54
+ ```
55
+
56
+ **Note:** if you don't know what the `redirect_url` is then **TODO TODO TODO TODO TODO**
57
+
58
+ ### 2. Authorization request
59
+
60
+ In order to access data in Zoho CRM you need to authorize ZohoHub to access your account. To do so
61
+ you have to request a specific URL with the right **scope** and **access_type**.
62
+
63
+ To get the right URL you can use this simple line of code:
64
+
65
+ ```ruby
66
+ ZohoHub::Auth.auth_url
67
+ # => "https://accounts.zoho.eu/oauth/v2/auth?access_type=offline&client_id=&redirect_uri=&response_type=code&scope=ZohoCRM.modules.custom.all,ZohoCRM.settings.all,ZohoCRM.modules.contacts.all,ZohoCRM.modules.all"
68
+ ```
69
+
70
+ If you request this generated URL you should see a screen like this one, where you have to click on "Accept":
71
+
72
+ ![](https://duaw26jehqd4r.cloudfront.net/items/1h1i3C1N0k0i02092F0S/Screen%20Shot%202018-11-25%20at%2019.18.38.png)
73
+
74
+ You can change the default scope (what data can be accessed by your application). This is the list
75
+ provided as the default scope:
76
+
77
+ ```
78
+ ZohoCRM.modules.custom.all
79
+ ZohoCRM.settings.all
80
+ ZohoCRM.modules.contacts.all
81
+ ZohoCRM.modules.all
82
+ ```
83
+
84
+ To get the URL for a different scope you can provide a `scope` argument:
85
+
86
+ ```ruby
87
+ ZohoHub::Auth.auth_url(scope: ['ZohoCRM.modules.custom.all', 'ZohoCRM.modules.all'])
88
+ # => "https://accounts.zoho.eu/oauth/v2/auth?access_type=offline&client_id=&redirect_uri=&response_type=code&scope=ZohoCRM.modules.custom.all,ZohoCRM.modules.all"
89
+ ```
90
+
91
+ #### 2.1 Offline access
92
+
93
+ By design the access tokens returned by the OAuth flow expire after a period of time (1 hour by
94
+ default), as a safety mechanism. This means that any application that wants to work with a user's
95
+ data needs the user to have recently gone through the OAuth flow, aka be online.
96
+
97
+ When you request offline access the Zoho API returns a refresh token. Refresh tokens give your
98
+ application the ability to request data on behalf of the user when the user is not present and in
99
+ front of your application.
100
+
101
+ **By default `ZohoHub::Auth.auth_url` will request offline access**
102
+
103
+ You can force "online" access by using `#auth_url` with a `access_type` argument:
104
+
105
+ ```ruby
106
+ ZohoHub::Auth.auth_url(access_type: 'online')
107
+ # => "https://accounts.zoho.eu/oauth/v2/auth?access_type=online&client_id=&redirect_uri=&response_type=code&scope=ZohoCRM.modules.custom.all,ZohoCRM.settings.all,ZohoCRM.modules.contacts.all,ZohoCRM.modules.all"
108
+ ```
22
109
 
23
- TODO: Write usage instructions here
110
+ ## 3. Generate Access Token
24
111
 
25
112
  ## Development
26
113
 
@@ -28,10 +115,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
28
115
  the tests. You can also run `bin/console` for an interactive prompt that will allow you to
29
116
  experiment.
30
117
 
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).
118
+ To install this gem onto your local machine, run `bundle exec rake install`.
35
119
 
36
120
  ## Contributing
37
121
 
data/bin/console CHANGED
@@ -8,16 +8,15 @@ require 'dotenv'
8
8
  Dotenv.load
9
9
 
10
10
  ZohoHub.configure do |config|
11
- config.client_id = ENV.fetch('ZOHO_CLIENT_ID')
12
- config.secret = ENV.fetch('ZOHO_SECRET')
13
- config.redirect_uri = ENV.fetch('ZOHO_OAUTH_REDIRECT_URL')
14
- config.debug = true
11
+ config.client_id = ENV['ZOHO_CLIENT_ID']
12
+ config.secret = ENV['ZOHO_SECRET']
13
+ config.debug = ENV['ZOHO_DEBUG'] || false
15
14
  end
16
15
 
17
- token_params = ZohoHub::Auth.refresh_token(ENV.fetch('ZOHO_REFRESH_TOKEN'))
16
+ # We assume that we already have a refresh token, to make things easier here.
17
+ puts 'Refreshing token...'
18
+ token_params = ZohoHub::Auth.refresh_token(ENV['ZOHO_REFRESH_TOKEN'])
18
19
  ZohoHub.setup_connection(token_params)
19
20
 
20
- self.send(:include, ZohoHub)
21
-
22
21
  require 'pry'
23
22
  Pry.start
data/bin/read ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'zoho_hub'
6
+ require 'zoho_hub/settings/module'
7
+ require 'zoho_hub/records/base_record'
8
+
9
+ require 'dotenv'
10
+ Dotenv.load
11
+
12
+ ZohoHub.configure do |config|
13
+ config.client_id = ENV['ZOHO_CLIENT_ID']
14
+ config.secret = ENV['ZOHO_SECRET']
15
+ config.redirect_uri = ENV['ZOHO_OAUTH_REDIRECT_URL']
16
+ end
17
+
18
+ token_params = ZohoHub::Auth.refresh_token(ENV['ZOHO_REFRESH_TOKEN'])
19
+ ZohoHub.setup_connection(token_params)
20
+
21
+ puts "Reading modules for client ID: #{ENV['ZOHO_CLIENT_ID']}..."
22
+
23
+ modules_hashes = ZohoHub::Settings::Module.all_json
24
+
25
+ puts "Found #{modules_hashes.size} modules"
26
+
27
+ modules_hashes.each do |hash|
28
+ puts "- Writing configuration for #{hash[:plural_label]}"
29
+
30
+ file_name = File.join(ZohoHub.root, 'cache', 'modules', "#{hash[:api_name]}.json")
31
+
32
+ File.open(file_name, 'w') do |file|
33
+ file.write(JSON.pretty_generate(hash))
34
+ end
35
+
36
+ next unless hash[:api_supported]
37
+
38
+ fields_array = ZohoHub::Settings::Field.all_json_for(hash[:api_name])
39
+ file_name = File.join(ZohoHub.root, 'cache', 'fields', "#{hash[:api_name]}.json")
40
+
41
+ File.open(file_name, 'w') do |file|
42
+ file.write(JSON.pretty_generate(fields_array))
43
+ end
44
+ end
data/bin/setup CHANGED
@@ -4,5 +4,3 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/bin/zoho_hub ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+
6
+ require 'zoho_hub'
7
+ require 'zoho_hub/auth'
8
+ require 'zoho_hub/oauth_callback_server'
9
+
10
+ require 'launchy'
11
+ require 'optparse'
12
+
13
+ default_port = ZohoHub::OauthCallbackServer.settings.port
14
+
15
+ options = {}
16
+ parser = OptionParser.new do |op|
17
+ op.banner = 'Usage: zoho_hub -c CLIENT_ID -s SECRET [options]'
18
+
19
+ op.on('-c', '--client-id=client_id', 'The Zoho client ID') do |client|
20
+ options[:client_id] = client
21
+ end
22
+
23
+ op.on('-s', '--secret=secret', 'The Zoho secret') do |secret|
24
+ options[:secret] = secret
25
+ end
26
+
27
+ op.on('-p', '--port=port', "The port for your callback (default #{default_port})") do |port|
28
+ options[:port] = port
29
+ end
30
+
31
+ op.on_tail('--version', 'Show version') do
32
+ puts "ZohoHub #{ZohoHub::VERSION}"
33
+ exit
34
+ end
35
+ end
36
+
37
+ parser.parse!
38
+
39
+ abort(parser.help) if options[:client_id].nil? || options[:secret].nil?
40
+
41
+ callback_path = ZohoHub::OauthCallbackServer::CALLBACK_PATH
42
+
43
+ ZohoHub::OauthCallbackServer.set(:port, options[:port]) if options[:port]
44
+
45
+ bind_port = ZohoHub::OauthCallbackServer.settings.port
46
+ bind_address = ZohoHub::OauthCallbackServer.settings.bind
47
+
48
+ callback_url = "http://#{bind_address}:#{bind_port}/#{callback_path}"
49
+
50
+ puts "Callback URL: #{callback_url}"
51
+
52
+ ZohoHub.configure do |config|
53
+ config.client_id = options[:client_id]
54
+ config.secret = options[:secret]
55
+ config.redirect_uri = callback_url
56
+ config.debug = true
57
+ end
58
+
59
+ # Open the URL in the browser
60
+ url = ZohoHub::Auth.auth_url
61
+ puts "REQUESTING: #{url}"
62
+ Launchy.open(url)
data/cache/.git_keep ADDED
File without changes
data/lib/zoho_hub/auth.rb CHANGED
@@ -2,14 +2,16 @@
2
2
 
3
3
  require 'multi_json'
4
4
  require 'faraday'
5
+ require 'faraday_middleware'
5
6
  require 'addressable'
6
7
 
7
8
  module ZohoHub
9
+ # Class that takes care of authentication using Oauth2 workflow as described here:
10
+ # https://www.zoho.com/crm/help/api/v2/#oauth-request.
8
11
  class Auth
9
12
  extend Forwardable
10
13
 
11
14
  TOKEN_PATH = '/oauth/v2/token'
12
- REVOKE_TOKEN_PATH = '/oauth/v2/token/revoke'
13
15
  AUTH_PATH = '/oauth/v2/auth'
14
16
 
15
17
  DEFAULT_SCOPES = %w[
@@ -21,22 +23,30 @@ module ZohoHub
21
23
 
22
24
  DEFAULT_ACCESS_TYPE = 'offline'
23
25
 
24
- def_delegators :@configuration, :redirect_uri, :client_id, :secret
26
+ def_delegators :@configuration, :redirect_uri, :client_id, :secret, :api_domain
25
27
 
26
- def initialize(access_type: nil, scopes: nil)
28
+ def initialize(access_type: DEFAULT_ACCESS_TYPE, scopes: DEFAULT_SCOPES)
27
29
  @configuration = ZohoHub.configuration
28
- @access_type = access_type || ZohoHub.configuration.access_type
29
- @scopes = scopes || DEFAULT_SCOPES
30
+ @access_type = access_type
31
+ @scopes = scopes
30
32
  end
31
33
 
32
- def self.auth_url
33
- new.auth_url
34
+ def token_full_uri
35
+ Addressable::URI.join(api_domain, TOKEN_PATH)
36
+ end
37
+
38
+ def auth_full_uri
39
+ Addressable::URI.join(api_domain, AUTH_PATH)
40
+ end
41
+
42
+ def self.auth_url(access_type: DEFAULT_ACCESS_TYPE, scopes: DEFAULT_SCOPES)
43
+ new(access_type: access_type, scopes: scopes).auth_url
34
44
  end
35
45
 
36
46
  def auth_url
37
47
  uri = auth_full_uri
38
48
 
39
- query = {
49
+ uri.query_values = {
40
50
  client_id: client_id,
41
51
  scope: @scopes.join(','),
42
52
  access_type: @access_type,
@@ -44,27 +54,21 @@ module ZohoHub
44
54
  response_type: 'code'
45
55
  }
46
56
 
47
- # The consent page must be presented otherwise we don't get the refresh token back.
48
- query[:prompt] = 'consent' if @access_type == DEFAULT_ACCESS_TYPE
49
-
50
- uri.query_values = query
51
-
52
57
  Addressable::URI.unencode(uri.to_s)
53
58
  end
54
59
 
55
- def auth_full_uri
56
- Addressable::URI.join(@configuration.base_url, AUTH_PATH)
57
- end
58
-
59
- def self.refresh_token(refresh_token)
60
- new.refresh_token(refresh_token)
61
- end
60
+ def token_url(grant_token)
61
+ uri = token_full_uri
62
62
 
63
- def refresh_token(refresh_token)
64
- result = Faraday.post(refresh_url(refresh_token))
63
+ uri.query_values = {
64
+ client_id: client_id,
65
+ client_secret: secret,
66
+ code: grant_token,
67
+ redirect_uri: redirect_uri,
68
+ grant_type: 'authorization_code'
69
+ }
65
70
 
66
- json = parse(result.body)
67
- json.merge(refresh_token: refresh_token)
71
+ Addressable::URI.unencode(uri.to_s)
68
72
  end
69
73
 
70
74
  def refresh_url(refresh_token)
@@ -80,16 +84,19 @@ module ZohoHub
80
84
  Addressable::URI.unencode(uri.to_s)
81
85
  end
82
86
 
83
- def parse(body)
84
- MultiJson.load(body, symbolize_keys: true)
87
+ def self.refresh_token(refresh_token)
88
+ new.refresh_token(refresh_token)
85
89
  end
86
90
 
87
- def token_full_uri
88
- Addressable::URI.join(@configuration.base_url, TOKEN_PATH)
91
+ def refresh_token(refresh_token)
92
+ result = Faraday.post(refresh_url(refresh_token))
93
+
94
+ json = parse(result.body)
95
+ json.merge(refresh_token: refresh_token)
89
96
  end
90
97
 
91
98
  def revoke_refresh_token(refresh_token)
92
- uri = revoke_token_full_uri
99
+ uri = token_full_uri
93
100
 
94
101
  uri.query_values = { token: refresh_token }
95
102
 
@@ -100,10 +107,6 @@ module ZohoHub
100
107
  parse(result.body)
101
108
  end
102
109
 
103
- def revoke_token_full_uri
104
- Addressable::URI.join(@configuration.base_url, REVOKE_TOKEN_PATH)
105
- end
106
-
107
110
  def self.get_token(grant_token)
108
111
  new.get_token(grant_token)
109
112
  end
@@ -114,20 +117,8 @@ module ZohoHub
114
117
  parse(result.body)
115
118
  end
116
119
 
117
- def token_url(grant_token)
118
- uri = token_full_uri
119
-
120
- query = {
121
- client_id: client_id,
122
- client_secret: secret,
123
- code: grant_token,
124
- redirect_uri: redirect_uri,
125
- grant_type: 'authorization_code'
126
- }
127
-
128
- uri.query_values = query
129
-
130
- Addressable::URI.unencode(uri.to_s)
120
+ def parse(body)
121
+ MultiJson.load(body, symbolize_keys: true)
131
122
  end
132
123
  end
133
124
  end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/response'
4
+ require 'zoho_hub/with_connection'
5
+ require 'zoho_hub/with_attributes'
6
+ require 'zoho_hub/string_utils'
7
+
8
+ module ZohoHub
9
+ class BaseRecord
10
+ include WithConnection
11
+ include WithAttributes
12
+
13
+ # Default nnumber of records when fetching all.
14
+ DEFAULT_RECORDS_PER_PAGE = 200
15
+
16
+ # Default page number when fetching all.
17
+ DEFAULT_PAGE = 1
18
+
19
+ # Minimum number of records to fetch when fetching all.
20
+ MIN_RECORDS = 2
21
+
22
+ class << self
23
+ def request_path(name = nil)
24
+ @request_path = name if name
25
+ @request_path ||= StringUtils.pluralize(StringUtils.demodulize(to_s))
26
+ @request_path
27
+ end
28
+
29
+ def find(id)
30
+ body = get(File.join(request_path, id.to_s))
31
+ response = build_response(body)
32
+
33
+ if response.empty?
34
+ raise RecordNotFound, "Couldn't find #{request_path.singularize} with 'id'=#{id}"
35
+ end
36
+
37
+ new(response.data)
38
+ end
39
+
40
+ def where(params)
41
+ path = File.join(request_path, 'search')
42
+
43
+ response = get(path, params)
44
+ data = response[:data]
45
+
46
+ data.map { |info| new(info) }
47
+ end
48
+
49
+ def find_by(params)
50
+ records = where(params)
51
+ records.first
52
+ end
53
+
54
+ def create(params)
55
+ new(params).save
56
+ end
57
+
58
+ def all(options = {})
59
+ options[:page] ||= DEFAULT_PAGE
60
+ options[:per_page] ||= DEFAULT_RECORDS_PER_PAGE
61
+ options[:per_page] = MIN_RECORDS if options[:per_page] < MIN_RECORDS
62
+
63
+ body = get(request_path, options)
64
+ response = build_response(body)
65
+
66
+ data = response.nil? ? [] : response.data
67
+
68
+ data.map { |json| new(json) }
69
+ end
70
+
71
+ def exists?(id)
72
+ !find(id).nil?
73
+ rescue RecordNotFound
74
+ false
75
+ end
76
+
77
+ alias exist? exists?
78
+
79
+ def build_response(body)
80
+ response = Response.new(body)
81
+
82
+ raise InvalidTokenError, response.msg if response.invalid_token?
83
+ raise RecordInvalid, response.msg if response.invalid_data?
84
+
85
+ response
86
+ end
87
+ end
88
+
89
+ def save
90
+ body = if new_record? # create new record
91
+ post(self.class.request_path, data: [to_params])
92
+ else # update existing record
93
+ path = File.join(self.class.request_path, id)
94
+ put(path, data: [to_params])
95
+ end
96
+
97
+ response = build_response(body)
98
+
99
+ response.data.dig(:details, :id)
100
+ end
101
+
102
+ def new_record?
103
+ !id
104
+ end
105
+
106
+ def to_params
107
+ params = {}
108
+
109
+ attributes.each do |attr|
110
+ key = attr_to_zoho_key(attr)
111
+
112
+ params[key] = send(attr)
113
+ end
114
+
115
+ params
116
+ end
117
+
118
+ def build_response(body)
119
+ self.class.build_response(body)
120
+ end
121
+ end
122
+ end
@@ -2,17 +2,16 @@
2
2
 
3
3
  module ZohoHub
4
4
  class Configuration
5
- attr_accessor :client_id, :secret, :redirect_uri, :base_url, :access_type
5
+ attr_accessor :client_id, :secret, :redirect_uri, :api_domain
6
6
  attr_writer :debug
7
7
 
8
- DEFAULT_BASE_URL = 'https://accounts.zoho.eu'
8
+ DEFAULT_API_DOMAIN = 'https://accounts.zoho.eu'
9
9
 
10
10
  def initialize
11
11
  @client_id = ''
12
12
  @secret = ''
13
13
  @redirect_uri = ''
14
- @base_url = DEFAULT_BASE_URL
15
- @access_type = Auth::DEFAULT_ACCESS_TYPE
14
+ @api_domain = DEFAULT_API_DOMAIN
16
15
  end
17
16
 
18
17
  def debug?