nimble_api_ak 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eeadf7eb29c5d4f014d3050d9eef479d01588a3fc868ca0f4729b21e8f7d47ec
4
+ data.tar.gz: ee0ecac3afd520cbc472f45d8a3d08175e0c8bd52d317b043786f434652c80a5
5
+ SHA512:
6
+ metadata.gz: 35895d646092bd7cadcc7a3cc351a1906b3c1cdea6097e9c72aee36c0a131c876992e78c985d3408ef759a61bccc1eeac5eabe9854add06a8d2ea9d4aa7fc0a8
7
+ data.tar.gz: d3d7be39af7d6070e409e7644082ddbbcc9d56a784fb032a7379cac0c236889bdc1230934a2cbc6e2167991b6eb6bc9c2484169703eed6ca5d99096a02b93ed4
@@ -0,0 +1,74 @@
1
+ class Nimble::BaseNimble
2
+ # generate nimble headers
3
+ def self.get_headers
4
+ # Arguments:
5
+ # NIMBLE_API_KEY: (Add this key in your environments development for local)
6
+ headers = {
7
+ "Authorization": "Bearer " + ::Nimble.api_token,
8
+ "Content-Type": 'application/json'
9
+ }
10
+ return headers
11
+ end
12
+ # save data to nimble
13
+ def self.save(nimble_id, data, headers)
14
+ if nimble_id&.present?
15
+ url = "https://app.nimble.com/api/v1/contact/"+nimble_id+"?replace=1"
16
+ request = ::HTTParty.put(
17
+ url,
18
+ :headers => headers,
19
+ :body => data.to_json
20
+ )
21
+ else
22
+ url = "https://app.nimble.com/api/v1/contact"
23
+ request = ::HTTParty.post(
24
+ url,
25
+ :headers => headers,
26
+ :body => data.to_json
27
+ )
28
+ end
29
+ end
30
+ # generate json for searching nimble record
31
+ def self.get_find_data(searchable_string, identifier, record)
32
+ fetch = {
33
+ "and": [
34
+ {
35
+ identifier.to_sym => {
36
+ "is": searchable_string
37
+ }
38
+ },
39
+ {
40
+ "record type": {
41
+ "is": record
42
+ }
43
+ }
44
+ ]
45
+ }
46
+ return fetch
47
+ end
48
+ # find nimble record
49
+ def self.find_nimble_record(searchable_string, headers, identifier, record)
50
+ begin
51
+ return nil if (identifier.nil? || searchable_string.nil?)
52
+ data = get_find_data(searchable_string, identifier, record)
53
+ request = ::HTTParty.get(
54
+ "https://app.nimble.com/api/v1/contacts?query="+CGI.escape(data.to_json) +'&tags=0&per_page=5',
55
+ :headers => headers
56
+ )
57
+ value = request["resources"].first["id"]
58
+ rescue
59
+ value = nil
60
+ end
61
+ return value
62
+ end
63
+ # send email for failed instances
64
+ def self.send_status(result = nil, error = nil)
65
+ if result&.success?
66
+ puts 'Data succesfully created'
67
+ return {success: true, result: result, errors: nil}
68
+ else
69
+ puts 'Error occured'
70
+ error_value = result["errors"]&.values&.first&.values&.join(',') if result&.present?
71
+ return {success: false, result: result, errors: error || error_value}
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,72 @@
1
+
2
+ # frozen_string_literal: true
3
+ class Nimble::Company < Nimble::BaseNimble
4
+ # MAX_RETRIES = 3
5
+ RECORD_IS = 'company'
6
+ # CALLING METHOD
7
+ def self.call(company_attributes, additional_details = nil)
8
+ # intialize retries
9
+ return nil unless company_attributes.class == Hash
10
+ retiries_count = 0
11
+ begin
12
+ # fetch headers
13
+ headers = get_headers
14
+ # find nimble id if exists
15
+ if (additional_details.nil? || additional_details.class != Hash)
16
+ additional_details = {
17
+ save_on_empty: true,
18
+ searchable_string: "",
19
+ identifier: nil
20
+ }
21
+ end
22
+ nimble_id = find_nimble_record(additional_details[:searchable_string], headers, additional_details[:identifier], Nimble::Company::RECORD_IS)
23
+ # create json data
24
+ data = get_save_data(company_attributes, additional_details[:save_on_empty])
25
+ # save data on nimble
26
+ result = save(nimble_id, data, headers)
27
+ # send email based on response
28
+ send_status(result)
29
+ rescue => e
30
+ # display error message
31
+ puts e.message
32
+ # increment retry count
33
+ # retiries_count +=1
34
+ # if Nimble::Company::MAX_RETRIES >= retiries_count
35
+ # # max upto 3 retries
36
+ # retry
37
+ # else
38
+ # send failed email
39
+ send_status(nil, e.message)
40
+ # end
41
+ end
42
+ end
43
+
44
+ def self.get_save_data(company_attributes, save_on_empty)
45
+ # parent json
46
+ company_json = {
47
+ "fields": {
48
+ },
49
+ "record_type": "company"
50
+
51
+ }
52
+
53
+ company_attributes.each {
54
+ |key, value|
55
+ field = {
56
+ key&.to_s.gsub('_', ' ').to_sym => [
57
+ {
58
+ "value": value,
59
+ "modifier": ""
60
+ }
61
+ ]
62
+ }
63
+ if save_on_empty
64
+ company_json[:fields] = company_json[:fields].merge(field)
65
+ else
66
+ company_json[:fields] = company_json[:fields].merge(field) if value&.present?
67
+ end
68
+ field = {}
69
+ }
70
+ return company_json
71
+ end
72
+ end
@@ -0,0 +1,81 @@
1
+
2
+ # frozen_string_literal: true
3
+ class Nimble::Person < Nimble::BaseNimble
4
+ MAX_RETRIES = 3
5
+ RECORD_IS = 'person'
6
+ def self.call(user_attributes, additional_details = nil)
7
+ retiries_count = 0
8
+ return nil unless user_attributes.class == Hash
9
+ begin
10
+ # fetch headers
11
+ headers = get_headers
12
+ # find nimble id if exists
13
+ if (additional_details.nil? || additional_details.class != Hash)
14
+ additional_details = {
15
+ save_on_empty: true,
16
+ searchable_string: "",
17
+ identifier: nil
18
+ }
19
+ end
20
+ nimble_id = find_nimble_record(additional_details[:searchable_string], headers, additional_details[:identifier], Nimble::Person::RECORD_IS)
21
+ # create json data
22
+ data = get_save_data(user_attributes, additional_details[:save_on_empty])
23
+ # save data on nimble
24
+ result = save(nimble_id, data, headers)
25
+ # send email based on response
26
+ send_status(result)
27
+ rescue => e
28
+ # display error message
29
+ puts e.message
30
+ # increment retry count
31
+ retiries_count +=1
32
+ if Nimble::Person::MAX_RETRIES >= retiries_count
33
+ # max upto 3 retries
34
+ retry
35
+ else
36
+ # send failed email
37
+ send_status(nil, e.message)
38
+ end
39
+ end
40
+ end
41
+
42
+ def self.get_save_data(user_attributes, save_on_empty)
43
+ # parent json
44
+
45
+ person_json = {
46
+ "fields": {
47
+ },
48
+ "record_type": "person"
49
+
50
+ }
51
+
52
+ user_attributes.each {
53
+ |key, value|
54
+ field = {
55
+ key&.to_s.gsub('_', ' ').to_sym => [
56
+ {
57
+ "value": value,
58
+ "modifier": ""
59
+ }
60
+ ]
61
+ }
62
+ if save_on_empty
63
+ person_json[:fields] = person_json[:fields].merge(field) unless key == 'user_avatar'
64
+ else
65
+ unless key == 'user_avatar'
66
+ person_json[:fields] = person_json[:fields].merge(field) if value&.present?
67
+ end
68
+ end
69
+ field = {}
70
+ }
71
+ # avatar json
72
+ if user_attributes[:user_avatar]&.present?
73
+ avatar = {
74
+ "avatar_url": user_attributes[:user_avatar]
75
+ }
76
+ person_json = person_json.merge(avatar)
77
+ end
78
+ # merge all json in parent based on existence
79
+ return person_json
80
+ end
81
+ end
@@ -0,0 +1,14 @@
1
+ module Nimble
2
+ def self.api_token
3
+ @api_token
4
+ end
5
+ def self.api_token=(token)
6
+ @api_token = token
7
+ end
8
+ require 'httparty'
9
+ require 'nimble/base_nimble'
10
+ require 'nimble/person'
11
+ require 'nimble/company'
12
+
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nimble_api_ak
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ankit Kalia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: "\n nimble api is used to create person on the nimble platform using
28
+ api's\n "
29
+ email:
30
+ - ankit.kalia002@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/nimble/base_nimble.rb
36
+ - lib/nimble/company.rb
37
+ - lib/nimble/person.rb
38
+ - lib/nimble_api_ak.rb
39
+ homepage: https://github.com/Ankitkalia002/nimble-ak-api
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options:
45
+ - README.md
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.1.2
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: nimble api is used to create person and company on the nimble platform using
63
+ api's
64
+ test_files: []