Class: KB::PetParent

Inherits:
BaseModel show all
Includes:
Destroyable, FindOrCreatable, Findable, Updatable, Upsertable
Defined in:
lib/kb/models/pet_parent.rb

Constant Summary collapse

STRING_FIELDS =
%i[key partner_name first_name last_name prefix_phone_number phone_number email country address
zip_code nif affiliate_code city iban_last4].freeze
DATE_FIELDS =
%i[birth_date deleted_at].freeze
BOOLEAN_FIELDS =
%i[phone_number_verified email_verified].freeze
FIELDS =
[*STRING_FIELDS, *DATE_FIELDS, *BOOLEAN_FIELDS].freeze

Instance Attribute Summary

Attributes inherited from BaseModel

#persisted

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Findable

#reload

Methods inherited from BaseModel

#==, define_attribute_methods, define_attributes, #initialize, #persist!, #persisted?

Methods included from Inspectionable

#inspect

Constructor Details

This class inherits a constructor from KB::BaseModel

Class Method Details

.all(filters = {}) ⇒ Object



11
12
13
14
# File 'lib/kb/models/pet_parent.rb', line 11

def self.all(filters = {})
  filters[:partner_key] = ENV['KB_PARTNER_KEY']
  super(filters)
end

.create(attributes = {}) ⇒ Object



16
17
18
19
# File 'lib/kb/models/pet_parent.rb', line 16

def self.create(attributes = {})
  attributes[:partner_key] = ENV['KB_PARTNER_KEY']
  super(attributes)
end

.upsert(attributes) ⇒ Object



21
22
23
24
# File 'lib/kb/models/pet_parent.rb', line 21

def self.upsert(attributes)
  attributes[:partner_key] = ENV['KB_PARTNER_KEY']
  super(attributes)
end

Instance Method Details

#contractsObject



96
97
98
99
100
# File 'lib/kb/models/pet_parent.rb', line 96

def contracts
  self.class.kb_client.request("#{key}/contracts").map do |contract|
    PetContract.from_api(contract)
  end
end

#destroy!Object



78
79
80
81
82
83
84
# File 'lib/kb/models/pet_parent.rb', line 78

def destroy!
  return unless @persisted

  self.class.destroy key
  @destroyed = true
  freeze
end

#destroyed?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/kb/models/pet_parent.rb', line 74

def destroyed?
  @destroyed
end

#full_phone_numberObject



86
87
88
# File 'lib/kb/models/pet_parent.rb', line 86

def full_phone_number
  "#{phone_number_prefix}#{phone_number}"
end

#ibanObject



114
115
116
117
118
# File 'lib/kb/models/pet_parent.rb', line 114

def iban
  @iban ||= self.class.kb_client.request("#{key}/iban")['iban']
rescue Faraday::Error => e
  raise KB::Error.from_faraday(e)
end

#merge!(duplicated_pet_parent_key) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kb/models/pet_parent.rb', line 26

def merge!(duplicated_pet_parent_key)
  params = { preservable_pet_parent_key: key,
             erasable_pet_parent_key: duplicated_pet_parent_key }.transform_keys do |key|
    key.to_s.camelize(:lower)
  end

  response = KB::ClientResolver.admin.request("petparents?#{params.to_query}", method: :put)
  PetParent.from_api response
rescue Faraday::Error => e
  raise KB::Error.from_faraday(e)
end

#petsObject



90
91
92
93
94
# File 'lib/kb/models/pet_parent.rb', line 90

def pets
  self.class.kb_client.request("#{key}/pets").map do |pet|
    Pet.from_api(pet)
  end
end

#referralsObject



102
103
104
105
106
# File 'lib/kb/models/pet_parent.rb', line 102

def referrals
  self.class.kb_client.request("#{key}/referrals").map do |referral|
    Referral.from_api(referral)
  end
end

#referrersObject



108
109
110
111
112
# File 'lib/kb/models/pet_parent.rb', line 108

def referrers
  self.class.kb_client.request("#{key}/referrers").map do |referral|
    Referral.from_api(referral)
  end
end

#save!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kb/models/pet_parent.rb', line 60

def save!
  return unless changed?

  run_callbacks :save do
    self.attributes = if @persisted
                        self.class.update key, changes.transform_values(&:last)
                      else
                        self.class.create changes.transform_values(&:last)
                      end

    self
  end
end

#update_iban(iban) ⇒ Object



120
121
122
123
124
125
# File 'lib/kb/models/pet_parent.rb', line 120

def update_iban(iban)
  self.class.kb_client.request("#{key}/iban", filters: { iban: iban }, method: :put)
  reload
rescue Faraday::Error => e
  raise KB::Error.from_faraday(e)
end