Class: ZohoService::ApiConnector
- Defined in:
- lib/zoho_service/api_connector.rb
Instance Attribute Summary collapse
-
#client_params ⇒ Object
readonly
Returns the value of attribute client_params.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#invalid_token ⇒ Object
readonly
Returns the value of attribute invalid_token.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Attributes inherited from Base
#childs, #errors, #full_data, #item_id, #parents, #saved, #table
Class Method Summary collapse
Instance Method Summary collapse
- #bad_response(response, url, query, headers, params) ⇒ Object
- #get_headers(params = {}) ⇒ Object
-
#initialize(token_in = nil, client_params_in = {}, debug_in = false) ⇒ ApiConnector
constructor
A new instance of ApiConnector.
- #invalid_CRMCSRFToken ⇒ Object
- #load_by_api(url, query = nil, params = {}) ⇒ Object
- #resource_path ⇒ Object
Methods inherited from Base
class_path, #connector, #delete, #delete!, #full, #get_childs, #get_parent, #init_data, #load_full, models_name, new_by_id, #parent, #save, #save!, #to_hash, #update
Constructor Details
#initialize(token_in = nil, client_params_in = {}, debug_in = false) ⇒ ApiConnector
Returns a new instance of ApiConnector.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/zoho_service/api_connector.rb', line 7 def initialize(token_in = nil, client_params_in = {}, debug_in = false) raise('Need zoho API token in params for ZohoService::ApiConnector.new') unless token_in @token = token_in @debug = debug_in @client_params = client_params_in super() @client_params[:api_url] = 'https://desk.zoho.com/api/v1'.freeze unless @client_params[:api_url] @client_params[:orgId] = organizations.first&.id unless @client_params[:orgId] @client_params[:departmentId] = departments.first&.id unless @client_params[:departmentId] @client_params[:timeout] = @client_params[:timeout] ? @client_params[:timeout].to_i : 5 end |
Instance Attribute Details
#client_params ⇒ Object (readonly)
Returns the value of attribute client_params.
6 7 8 |
# File 'lib/zoho_service/api_connector.rb', line 6 def client_params @client_params end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
6 7 8 |
# File 'lib/zoho_service/api_connector.rb', line 6 def debug @debug end |
#invalid_token ⇒ Object (readonly)
Returns the value of attribute invalid_token.
6 7 8 |
# File 'lib/zoho_service/api_connector.rb', line 6 def invalid_token @invalid_token end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
6 7 8 |
# File 'lib/zoho_service/api_connector.rb', line 6 def token @token end |
Class Method Details
.headers ⇒ Object
82 83 84 85 86 87 |
# File 'lib/zoho_service/api_connector.rb', line 82 def headers { 'User-Agent' => 'ZohoService-Ruby-On-Rails-gem-by-chaky222/' + ZohoService::VERSION, 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept-Charset'=> 'UTF-8' }.freeze end |
Instance Method Details
#bad_response(response, url, query, headers, params) ⇒ Object
75 76 77 78 79 |
# File 'lib/zoho_service/api_connector.rb', line 75 def bad_response(response, url, query, headers, params) error_str = "ZohoService API bad_response url=[#{url}], query=[#{query&.to_json}]\nparams=[#{params.to_json}]\n" error_str += response ? "code=[#{response.code}] body=[#{response.body}]\n" : "Unknown error in load_by_api.\n" raise error_str end |
#get_headers(params = {}) ⇒ Object
23 24 25 26 27 |
# File 'lib/zoho_service/api_connector.rb', line 23 def get_headers(params = {}) client_headers = { 'Authorization': 'Zoho-authtoken ' + @token } client_headers[:orgId] = @client_params[:orgId].to_s if @client_params[:orgId] self.class.headers.merge(client_headers) end |
#invalid_CRMCSRFToken ⇒ Object
70 71 72 73 |
# File 'lib/zoho_service/api_connector.rb', line 70 def invalid_CRMCSRFToken @invalid_token = true raise('Invalid CRMCSRFToken. Check your token in ApiConnector in ZohoService gem!') end |
#load_by_api(url, query = nil, params = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/zoho_service/api_connector.rb', line 29 def load_by_api(url, query = nil, params = {}) url = resource_path + '/search' if query && query[:searchStr] url = URI.encode(url) raise "Invalid CRMCSRFToken. Check your token in ApiConnector in ZohoService gem!\n" if @invalid_token request_params = { headers: get_headers(params), timeout: @client_params[:timeout], no_follow: true, limit: 1, follow_redirects: false, read_timeout: @client_params[:timeout] } response = nil begin response = if params[:method] == :post HTTParty.post(url, request_params.merge(body: query.to_json)) elsif params[:method] == :patch HTTParty.patch(url, request_params.merge(body: query.to_json)) elsif params[:method] == :delete HTTParty.delete(url, request_params) else url = url + '?' + query.to_query if query HTTParty.get(url, request_params) end rescue HTTParty::RedirectionTooDeep => e raise("Can`t Connect to zohoDesk server. RedirectionTooDeep. Check https or maybe your account blocked.\nurl=[#{url}]\nerror=[#{e}]") rescue => e raise("Can`t Connect to zohoDesk server. Unknown error. Maybe your account blocked.\nurl=[#{url}]\nerror=[#{e}]") end if response $stderr.puts "#{params[:method]} url=[#{url}] length=[#{response.to_json.length}] cnt=[#{response['data']&.count}]\n" if @debug if response.code == 200 raise "Error message in ZohoService gem: \n[#{ response['message'] }]\n" if response['message'] return response['data'] ? response['data'] : response elsif response.code == 204 # 204 - no content found or from-limit out of range return [] elsif response.code == 400 # 400 - Invalid CRMCSRFToken invalid_CRMCSRFToken() else invalid_CRMCSRFToken() if response.body&.include?('Invalid CRMCSRFToken') end end bad_response(response, url, query, get_headers(params), params) nil end |
#resource_path ⇒ Object
19 20 21 |
# File 'lib/zoho_service/api_connector.rb', line 19 def resource_path @client_params[:api_url] end |