Class: Nimble::BaseNimble
- Inherits:
-
Object
- Object
- Nimble::BaseNimble
- Defined in:
- lib/nimble/base_nimble.rb
Class Method Summary collapse
-
.find_nimble_record(searchable_string, headers, identifier, record) ⇒ Object
find nimble record.
-
.get_find_data(searchable_string, identifier, record) ⇒ Object
generate json for searching nimble record.
-
.get_headers ⇒ Object
generate nimble headers.
-
.save(nimble_id, data, headers) ⇒ Object
save data to nimble.
-
.send_status(result = nil, error = nil) ⇒ Object
send email for failed instances.
Class Method Details
.find_nimble_record(searchable_string, headers, identifier, record) ⇒ Object
find nimble record
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/nimble/base_nimble.rb', line 49 def self.find_nimble_record(searchable_string, headers, identifier, record) begin return nil if (identifier.nil? || searchable_string.nil?) data = get_find_data(searchable_string, identifier, record) request = ::HTTParty.get( "https://app.nimble.com/api/v1/contacts?query="+CGI.escape(data.to_json) +'&tags=0&per_page=5', :headers => headers ) value = request["resources"].first["id"] rescue value = nil end return value end |
.get_find_data(searchable_string, identifier, record) ⇒ Object
generate json for searching nimble record
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nimble/base_nimble.rb', line 31 def self.get_find_data(searchable_string, identifier, record) fetch = { "and": [ { identifier.to_sym => { "is": searchable_string } }, { "record type": { "is": record } } ] } return fetch end |
.get_headers ⇒ Object
generate nimble headers
3 4 5 6 7 8 9 10 11 |
# File 'lib/nimble/base_nimble.rb', line 3 def self.get_headers # Arguments: # NIMBLE_API_KEY: (Add this key in your environments development for local) headers = { "Authorization": "Bearer " + ::Nimble.api_token, "Content-Type": 'application/json' } return headers end |
.save(nimble_id, data, headers) ⇒ Object
save data to nimble
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nimble/base_nimble.rb', line 13 def self.save(nimble_id, data, headers) if nimble_id&.present? url = "https://app.nimble.com/api/v1/contact/"+nimble_id+"?replace=1" request = ::HTTParty.put( url, :headers => headers, :body => data.to_json ) else url = "https://app.nimble.com/api/v1/contact" request = ::HTTParty.post( url, :headers => headers, :body => data.to_json ) end end |
.send_status(result = nil, error = nil) ⇒ Object
send email for failed instances
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nimble/base_nimble.rb', line 64 def self.send_status(result = nil, error = nil) if result&.success? puts 'Data succesfully created' return {success: true, result: result, errors: nil} else puts 'Error occured' error_value = result["errors"]&.values&.first&.values&.join(',') if result&.present? return {success: false, result: result, errors: error || error_value} end end |