zoho_hub 0.1.56 → 0.2.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.
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
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'faraday'
4
+ require 'faraday_middleware'
4
5
  require 'rainbow'
5
6
  require 'addressable'
6
7
 
@@ -21,43 +22,37 @@ module ZohoHub
21
22
  def initialize(access_token:, api_domain: DEFAULT_DOMAIN, expires_in: 3600, refresh_token: nil)
22
23
  @access_token = access_token
23
24
  @expires_in = expires_in
24
- @api_domain = api_domain || DEFAULT_DOMAIN
25
+ @api_domain = api_domain
25
26
  @refresh_token ||= refresh_token # do not overwrite if it's already set
26
27
  end
27
28
 
28
- def get(path, params = {}, &block)
29
+ def get(path, params = {})
29
30
  log "GET #{path} with #{params}"
30
31
 
31
- response = with_refresh do
32
- adapter(&block).get(path, params)
33
- end
32
+ response = with_refresh { adapter.get(path, params) }
34
33
  response.body
35
34
  end
36
35
 
37
- def post(path, params = {}, &block)
36
+ def post(path, params = {})
38
37
  log "POST #{path} with #{params}"
39
38
 
40
- response = with_refresh do
41
- adapter(&block).post(path, params)
42
- end
39
+ response = with_refresh { adapter.post(path, params) }
43
40
  response.body
44
41
  end
45
42
 
46
- def put(path, params = {}, &block)
43
+ def put(path, params = {})
47
44
  log "PUT #{path} with #{params}"
48
45
 
49
- response = with_refresh do
50
- adapter(&block).put(path, params)
51
- end
46
+ response = with_refresh { adapter.put(path, params) }
52
47
  response.body
53
48
  end
54
49
 
55
50
  def access_token?
56
- @access_token.present?
51
+ @access_token
57
52
  end
58
53
 
59
54
  def refresh_token?
60
- @refresh_token.present?
55
+ @refresh_token
61
56
  end
62
57
 
63
58
  def log(text)
@@ -78,7 +73,7 @@ module ZohoHub
78
73
  log "Refreshing outdated token... #{@access_token}"
79
74
  params = ZohoHub::Auth.refresh_token(@refresh_token)
80
75
 
81
- @on_refresh_cb.call(params) if @on_refresh_cb.present?
76
+ @on_refresh_cb.call(params) if @on_refresh_cb
82
77
 
83
78
  @access_token = params[:access_token]
84
79
 
@@ -102,7 +97,7 @@ module ZohoHub
102
97
  def adapter
103
98
  Faraday.new(url: base_url) do |conn|
104
99
  conn.headers = authorization_header if access_token?
105
- yield conn if block_given?
100
+ conn.use FaradayMiddleware::ParseJson
106
101
  conn.response :json, parser_options: { symbolize_names: true }
107
102
  conn.response :logger if ZohoHub.configuration.debug?
108
103
  conn.adapter Faraday.default_adapter
@@ -6,7 +6,7 @@ module ZohoHub
6
6
  class Account < BaseRecord
7
7
  attributes :id, :name, :territories, :company_number, :employee_count, :company_type, :industry
8
8
  attributes :billing_city, :billing_code, :billing_country, :billing_street, :billing_state
9
- attributes :account_type, :industry_section, :region
9
+ attributes :account_type
10
10
 
11
11
  # This is the ID to be used when the borrower has no organisation (unlikely) or belongs to
12
12
  # multiple organisations.
@@ -4,18 +4,14 @@ require 'zoho_hub/records/base_record'
4
4
 
5
5
  module ZohoHub
6
6
  class Contact < BaseRecord
7
- attributes :id, :email, :salutation, :first_name, :mobile, :role, :last_name, :vendor_id
8
- attributes :account_id, :owner_id, :campaign_id, :status, :campaign_detail, :phone
9
- attributes :mailing_city, :mailing_zip, :mailing_country, :mailing_street, :mailing_state
10
- attributes :date_of_birth, :relationship_to_business
7
+ attributes :id, :email, :salutation, :first_name, :mobile, :role, :last_name
8
+ attributes :account_id, :owner_id, :campaign_id, :status, :campaign_detail
11
9
 
12
10
  attribute_translation(
13
11
  id: :id,
14
12
  role: :platform_cont_type,
15
13
  status: :platform_cont_status,
16
- use_proceeds: :use_proceeds,
17
- date_of_birth: :Date_of_Birth,
18
- relationship_to_business: :Relationship_to_business
14
+ use_proceeds: :use_proceeds
19
15
  )
20
16
 
21
17
  DEFAULTS = {
@@ -34,7 +30,6 @@ module ZohoHub
34
30
  @account_id ||= params.dig(:Account_Name, :id)
35
31
  @owner_id ||= params.dig(:Owner, :id)
36
32
  @campaign_id ||= params.dig(:Campaign_Lookup, :id)
37
- @vendor_id ||= params.dig(:Vendor_Name, :id)
38
33
  end
39
34
 
40
35
  def to_params
@@ -43,7 +38,6 @@ module ZohoHub
43
38
  params[:Account_Name] = { id: @account_id } if @account_id
44
39
  params[:Owner] = { id: @owner_id } if @owner_id
45
40
  params[:Campaign_Lookup] = { id: @campaign_id } if @campaign_id
46
- params[:Vendor_Name] = { id: @vendor_id } if @vendor_id
47
41
 
48
42
  params
49
43
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ class Vendor < BaseRecord
5
+ def initialize(params)
6
+ puts Rainbow(params).red.bright
7
+ end
8
+ end
9
+ end
@@ -9,14 +9,6 @@ module ZohoHub
9
9
  attributes :currency, :territory, :employee_count, :turnover, :industry, :region
10
10
  attributes :review_outcome, :first_created, :last_modified, :preferred_term, :project_notes
11
11
  attributes :campaign_id, :account_id, :contact_id, :campaign_detail, :reviewers_comment
12
- attributes :accounting_software, :banking_provider, :turnover_figure
13
- attributes :guarantee_types, :home_ownership_status
14
- attributes :accountant_id, :vat_registration, :published_all_of_market
15
- attributes :credit_risk_band, :live_ccjs, :satisfied_ccjs
16
- attributes :state, :number_of_connections
17
- attributes :submitted_to_lender_panel, :last_attachment_uploaded, :funds_timeline
18
- attributes :cancellation_reason, :stripe_payment_reference
19
- attributes :portal_session_id, :portal_session_url, :portal_agent_session_url
20
12
 
21
13
  DEFAULTS = {
22
14
  currency: 'GBP',
@@ -31,21 +23,7 @@ module ZohoHub
31
23
  code: :Project_Ref_No,
32
24
  description: :Project_description,
33
25
  employee_count: :Number_of_Employees,
34
- use_proceeds: :use_proceeds,
35
- vat_registration: :Pick_List_15,
36
- live_ccjs: :Live_CCJs,
37
- satisfied_ccjs: :Satisfied_CCJs,
38
- published_all_of_market: :Published_to_All_of_Market,
39
- state: :State1,
40
- submitted_to_lender_panel: :Submitted_to_Lender_Panel1,
41
- number_of_connections: :Number_of_Connections,
42
- turnover_figure: :Turnover_figure,
43
- last_attachment_uploaded: :Last_Attachment_Uploaded,
44
- funds_timeline: :When_will_you_needs_the_funds,
45
- cancellation_reason: :If_Application_Cancelled_select_reason,
46
- portal_session_id: :Portal_Session_ID,
47
- portal_session_url: :Portal_Session_URL,
48
- portal_agent_session_url: :Portal_Agent_Session_URL,
26
+ use_proceeds: :use_proceeds
49
27
  )
50
28
 
51
29
  def initialize(params)
@@ -59,7 +37,6 @@ module ZohoHub
59
37
  @account_id ||= params.dig(:Account_Name, :id)
60
38
  @contact_id ||= params.dig(:Contact_Name, :id)
61
39
  @campaign_id ||= params.dig(:Campaign_Source, :id)
62
- @accountant_id ||= params.dig(:Accountant_Name, :id)
63
40
  end
64
41
 
65
42
  def to_params
@@ -68,7 +45,6 @@ module ZohoHub
68
45
  params[:Campaign_Source] = { id: @campaign_id } if @campaign_id
69
46
  params[:Account_Name] = { id: @account_id } if @account_id
70
47
  params[:Contact_Name] = { id: @contact_id } if @contact_id
71
- params[:Accountant_Name] = { id: @accountant_id } if @accountant_id
72
48
 
73
49
  params
74
50
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ class Product < BaseRecord
5
+ def initialize(params)
6
+ puts Rainbow(params).red.bright
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/records/base_record'
4
+
5
+ module ZohoHub
6
+ class Quote < BaseRecord
7
+ attributes :id, :stage, :subject
8
+ attributes :potential_id
9
+
10
+ attribute_translation(
11
+ id: :id,
12
+ stage: :Quote_Stage
13
+ )
14
+
15
+ def initialize(params)
16
+ puts Rainbow(params).bright.red
17
+ attributes.each do |attr|
18
+ zoho_key = attr_to_zoho_key(attr)
19
+
20
+ send("#{attr}=", params[zoho_key] || params[attr])
21
+ end
22
+
23
+ @potential_id ||= params.dig(:Deal_Name, :id)
24
+ @lender_organisation_id ||= params.dig(:Account_Name, :id)
25
+ end
26
+
27
+ def to_params
28
+ params = super
29
+
30
+ params[:Deal_Name] = { id: @potential_id } if @potential_id
31
+ params[:Account_Name] = { id: @lender_organisation_id } if @lender_organisation_id
32
+ end
33
+ end
34
+ end
@@ -12,7 +12,4 @@ module ZohoHub
12
12
 
13
13
  class ZohoAPIError < StandardError
14
14
  end
15
-
16
- class AttachmentLinkTakenError < StandardError
17
- end
18
15
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/string_utils'
4
+
5
+ module ZohoHub
6
+ class ModuleBuilder
7
+ class << self
8
+ def build_from_cache
9
+ cached_module_definitions.map do |file|
10
+ json = MultiJson.load(File.read(file), symbolize_keys: true)
11
+
12
+ create_module(json)
13
+ end
14
+ end
15
+
16
+ def cached_module_definitions
17
+ Dir[File.join(ZohoHub.root, 'cache', 'modules', '**')]
18
+ end
19
+
20
+ def create_module(json)
21
+ fields = cached_module_fields(json[:api_name])
22
+
23
+ klass = Class.new(ZohoHub::BaseRecord) do
24
+ request_path json[:api_name]
25
+
26
+ translations = {}
27
+ fields.each do |field|
28
+ key = StringUtils.underscore(field[:api_name])
29
+
30
+ translations[key] = field[:api_name]
31
+ end
32
+
33
+ if translations.any?
34
+ attributes(*translations.keys)
35
+ attribute_translation(translations)
36
+ end
37
+
38
+ def initialize(params)
39
+ attributes.each do |attr|
40
+ zoho_key = attr_to_zoho_key(attr)
41
+
42
+ send("#{attr}=", params[zoho_key] || params[attr])
43
+ end
44
+ end
45
+ end
46
+
47
+ ZohoHub.const_set(StringUtils.camelize(json[:singular_label]), klass)
48
+ end
49
+
50
+ def cached_module_fields(module_name)
51
+ file = File.join(ZohoHub.root, 'cache', 'fields', "#{module_name}.json")
52
+
53
+ return [] unless File.exist?(file)
54
+
55
+ json_content = File.read(file)
56
+
57
+ MultiJson.load(json_content, symbolize_keys: true)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sinatra/base'
4
+
5
+ module ZohoHub
6
+ class OauthCallbackServer < Sinatra::Base
7
+ enable :logging
8
+
9
+ CALLBACK_PATH = 'oauth2callback'
10
+
11
+ before do
12
+ content_type :json
13
+ end
14
+
15
+ get "/#{CALLBACK_PATH}" do
16
+ grant_token = params[:code]
17
+
18
+ # This will trigger a post request to get both the token and the refresh token
19
+ @variables = ZohoHub::Auth.get_token(grant_token)
20
+
21
+ puts "Variables: #{@variables.inspect}"
22
+
23
+ erb :variables
24
+ end
25
+ end
26
+ end
@@ -7,9 +7,7 @@ module ZohoHub
7
7
  end
8
8
 
9
9
  def invalid_data?
10
- if data.is_a?(Array)
11
- return data.first[:code] == 'MANDATORY_NOT_FOUND'
12
- end
10
+ return false if data.is_a?(Array)
13
11
 
14
12
  data[:code] == 'INVALID_DATA'
15
13
  end
@@ -42,7 +40,6 @@ module ZohoHub
42
40
 
43
41
  def msg
44
42
  msg = data[:message]
45
- msg << ", error in #{data.dig(:details, :api_name)}" if data.dig(:code) == 'INVALID_DATA'
46
43
 
47
44
  if data.dig(:details, :expected_data_type)
48
45
  expected = data.dig(:details, :expected_data_type)
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/with_connection'
4
+ require 'zoho_hub/with_attributes'
5
+
6
+ module ZohoHub
7
+ module Settings
8
+ class Field
9
+ include WithConnection
10
+ include WithAttributes
11
+
12
+ REQUEST_PATH = 'settings/fields'
13
+
14
+ attributes :custom_field, :lookup, :convert_mapping, :visible, :field_label, :length,
15
+ :view_type, :read_only, :api_name, :unique, :data_type, :formula, :currency, :id,
16
+ :decimal_place, :pick_list_values, :auto_number
17
+
18
+ def self.all_for(module_name)
19
+ fields = all_json_for(module_name)
20
+ fields.map { |json| new(json) }
21
+ end
22
+
23
+ def self.all_json_for(module_name)
24
+ response = get(REQUEST_PATH, module: module_name)
25
+ response[:fields]
26
+ end
27
+
28
+ def initialize(json = {})
29
+ attributes.each { |attr| send("#{attr}=", json[attr]) }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zoho_hub/with_connection'
4
+ require 'zoho_hub/with_attributes'
5
+
6
+ require 'zoho_hub/settings/field'
7
+
8
+ module ZohoHub
9
+ module Settings
10
+ # Zoho CRM has standard modules such as, Leads, Accounts, Contacts, Deals, Forecasts,
11
+ # Activities, etc,. Using Zoho CRM REST API, you can retrieve the list of available modules.
12
+ #
13
+ # convertable: Describes if the user can convert the record into another type of record.
14
+ # For example: Convert Leads in to Deals.
15
+ # creatable: Checks if the user can create a record in the current module.
16
+ # generated_type: Describes the type of module which would be generated by the user. There are
17
+ # 4 types: default, web, custom, linking.
18
+ # api_supported: The modules which are currently not accessible by APIs have value as "false".
19
+ # If the modules are supported in the future, the value automatically changes
20
+ # to "true".
21
+ # modified_time: The date and time of changes made by the user.
22
+ #
23
+ # More details: https://www.zoho.com/crm/help/api/v2/#Modules-APIs
24
+ class Module
25
+ include WithConnection
26
+ include WithAttributes
27
+
28
+ REQUEST_PATH = 'settings/modules'
29
+
30
+ attributes :convertable, :editable, :deletable, :web_link, :singular_label, :modified_time,
31
+ :viewable, :api_supported, :creatable, :plural_label, :api_name, :modified_by,
32
+ :generated_type, :id, :module_name, :fields
33
+
34
+ def self.all
35
+ modules = all_json
36
+ modules.map { |json| new(json) }
37
+ end
38
+
39
+ def self.all_json
40
+ response = get(REQUEST_PATH)
41
+ response[:modules]
42
+ end
43
+
44
+ def initialize(json = {})
45
+ attributes.each { |attr| send("#{attr}=", json[attr]) }
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ class StringUtils
5
+ class << self
6
+ def demodulize(text)
7
+ text.split('::').last
8
+ end
9
+
10
+ def pluralize(text)
11
+ "#{text}s"
12
+ end
13
+
14
+ def camelize(text)
15
+ result = text.split(/[_\s]/)
16
+
17
+ return result.first if result.size == 1
18
+
19
+ result.map(&:capitalize).join
20
+ end
21
+
22
+ def underscore(text)
23
+ return text unless text =~ /[A-Z-]/
24
+
25
+ result = text.dup
26
+ result.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
27
+ result.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
28
+ result.tr!('-', '_')
29
+ result.downcase!
30
+ result
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ZohoHub
4
- VERSION = '0.1.56'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -0,0 +1,72 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en" dir="ltr">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Zoho CRM authentication</title>
6
+ <style media="screen">
7
+ body {
8
+ font-family: sans-serif;
9
+ padding: 20px 50px;
10
+ }
11
+
12
+ ul.variables {
13
+ padding: 20px;
14
+ background-color: #eaeaea;
15
+ font-family: monospace;
16
+ list-style: none;
17
+ line-height: 1.5em;
18
+ margin-bottom: 50px;
19
+ }
20
+
21
+ li.error {
22
+ color: #aa0000;
23
+ }
24
+
25
+ div.config {
26
+ padding: 20px;
27
+ background-color: #eaeaea;
28
+ font-family: monospace;
29
+ line-height: 1.5em;
30
+ }
31
+
32
+ pre {
33
+ margin: 0;
34
+ padding: 0;
35
+ }
36
+
37
+ code {
38
+ padding: 0;
39
+ margin: 0;
40
+ }
41
+ </style>
42
+ </head>
43
+
44
+ <body>
45
+ <h2>Here's what you have to add to your environment variables:</h2>
46
+
47
+ <ul class="variables">
48
+ <li>ZOHO_CLIENT_ID=<%= ZohoHub.configuration.client_id %></li>
49
+ <li>ZOHO_SECRET=<%= ZohoHub.configuration.secret %></li>
50
+ <li>ZOHO_API_DOMAIN=<%= @variables[:api_domain] %></li>
51
+ <% if @variables[:refresh_token] %>
52
+ <li>ZOHO_REFRESH_TOKEN=<%= @variables[:refresh_token] %></li>
53
+ <% else %>
54
+ <li class="error">
55
+ ZOHO_REFRESH_TOKEN=zoho didn't send the refresh token. Please review your configuration.
56
+ Check <a href="https://github.com/rikas/zoho_hub">the documentation</a> to understand why
57
+ this is important and how to solve the problem.
58
+ </li>
59
+ <% end %>
60
+ </ul>
61
+
62
+ <h2>Then you need a configuration block like this one:</h2>
63
+ <div class="config">
64
+ <pre><code>ZohoHub.configure do |config|
65
+ config.client_id = ENV['ZOHO_CLIENT_ID']
66
+ config.secret = ENV['ZOHO_SECRET']
67
+ config.api_domain = ENV['ZOHO_API_DOMAIN']
68
+ config.refresh_token = ENV['ZOHO_REFRESH_TOKEN']
69
+ end</code></pre>
70
+ </div>
71
+ </body>
72
+ </html>
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ # Allows adding attributes to a class, as <tt>attr_accessors</tt> that can then be listed from the
5
+ # class or an instance of that class.
6
+ #
7
+ # === Example
8
+ #
9
+ # class User
10
+ # include ZohoHub::WithAttributes
11
+ #
12
+ # attributes :first_name, :last_name, :email
13
+ # end
14
+ #
15
+ # User.attributes # => [:first_name, :last_name, :email]
16
+ # User.new.attributes # => [:first_name, :last_name, :email]
17
+ #
18
+ # user = User.new
19
+ # user.first_name = 'Ricardo'
20
+ # user.last_name = 'Otero'
21
+ # user.first_name # => "Ricardo"
22
+ module WithAttributes
23
+ def self.included(base)
24
+ base.extend ClassMethods
25
+ end
26
+
27
+ module ClassMethods
28
+ def attributes(*attributes)
29
+ @attributes ||= []
30
+
31
+ return @attributes unless attributes
32
+
33
+ attr_accessor(*attributes)
34
+
35
+ @attributes += attributes
36
+ end
37
+
38
+ def attribute_translation(translation = nil)
39
+ @attribute_translation ||= {}
40
+
41
+ return @attribute_translation unless translation
42
+
43
+ @attribute_translation = translation
44
+ end
45
+
46
+ def zoho_key_translation
47
+ @attribute_translation.to_a.map(&:rotate).to_h
48
+ end
49
+ end
50
+
51
+ # Returns the list of attributes defined for the instance class.
52
+ def attributes
53
+ self.class.attributes
54
+ end
55
+
56
+ private
57
+
58
+ def attr_to_zoho_key(attr_name)
59
+ translations = self.class.attribute_translation
60
+
61
+ return translations[attr_name.to_sym] if translations.key?(attr_name.to_sym)
62
+
63
+ attr_name.to_s.split('_').map(&:capitalize).join('_').to_sym
64
+ end
65
+
66
+ def zoho_key_to_attr(zoho_key)
67
+ translations = self.class.zoho_key_translation
68
+
69
+ return translations[zoho_key.to_sym] if translations.key?(zoho_key.to_sym)
70
+
71
+ zoho_key.to_sym
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZohoHub
4
+ # Adds the ability to do API requests (GET / PUT and POST requests) when included in a class.
5
+ module WithConnection
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def get(path, params = {})
12
+ ZohoHub.connection.get(path, params)
13
+ end
14
+
15
+ def post(path, params = {})
16
+ ZohoHub.connection.post(path, params.to_json)
17
+ end
18
+
19
+ def put(path, params = {})
20
+ ZohoHub.connection.put(path, params.to_json)
21
+ end
22
+ end
23
+
24
+ def get(path, params = {})
25
+ self.class.get(path, params)
26
+ end
27
+
28
+ def post(path, params = {})
29
+ self.class.post(path, params)
30
+ end
31
+
32
+ def put(path, params = {})
33
+ self.class.put(path, params)
34
+ end
35
+ end
36
+ end