Class: Nimble::Person

Inherits:
BaseNimble show all
Defined in:
lib/nimble/person.rb

Constant Summary collapse

MAX_RETRIES =
3
RECORD_IS =
'person'

Class Method Summary collapse

Methods inherited from BaseNimble

find_nimble_record, get_find_data, get_headers, save, send_status

Class Method Details

.call(user_attributes, additional_details = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nimble/person.rb', line 6

def self.call(user_attributes, additional_details = nil)
  retiries_count = 0
  return nil unless user_attributes.class == Hash
  begin
    # fetch headers
    headers = get_headers
    # find nimble id if exists
    if (additional_details.nil? || additional_details.class != Hash)
      additional_details = {
        save_on_empty: true,
        searchable_string: "",
        identifier: nil
      }
    end
    nimble_id = find_nimble_record(additional_details[:searchable_string], headers, additional_details[:identifier], Nimble::Person::RECORD_IS)
    # create json data
    data = get_save_data(user_attributes, additional_details[:save_on_empty])
    # save data on nimble
    result = save(nimble_id, data, headers)
    # send email based on response
    send_status(result)
  rescue => e
    # display error message
    puts e.message
    # increment retry count
    retiries_count +=1
    if Nimble::Person::MAX_RETRIES >= retiries_count
      # max upto 3 retries
      retry
    else
      # send failed email
      send_status(nil, e.message)
    end
  end
end

.get_save_data(user_attributes, save_on_empty) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nimble/person.rb', line 42

def self.get_save_data(user_attributes, save_on_empty)
  # parent json

  person_json = {
    "fields": {
    },
    "record_type": "person"

  }

  user_attributes.each {
    |key, value|
    field = {
      key&.to_s.gsub('_', ' ').to_sym => [
        {
          "value": value,
          "modifier": ""
        }
      ]
    }
    if save_on_empty
      person_json[:fields] = person_json[:fields].merge(field) unless key == 'user_avatar'
    else
      unless key == 'user_avatar'
        person_json[:fields] = person_json[:fields].merge(field) if value&.present?
      end
    end
    field = {}
  }
  # avatar json
  if user_attributes[:user_avatar]&.present?
    avatar = {
      "avatar_url": user_attributes[:user_avatar]
    }
    person_json = person_json.merge(avatar)
  end
  # merge all json in parent based on existence
  return person_json
end