Skip to content

Commit

Permalink
Merge pull request #92 from barkibu/iban-support
Browse files Browse the repository at this point in the history
Iban support
  • Loading branch information
esaborit4code authored Jun 14, 2024
2 parents 6b0f7aa + 171f848 commit a0ddf27
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 94 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [unreleased]
- See diff: https://github.com/barkibu/kb-ruby/compare/v0.25...HEAD
- See diff: https://github.com/barkibu/kb-ruby/compare/v0.26...HEAD

# [0.26.0]
- Add `#iban` and `#update_iban` to `PetParent`
- Add /iban endpoints in fake API

# [0.25.0]
- Add support for Ruby 3.2
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
barkibu-kb (0.25.0)
barkibu-kb (0.26.0)
activemodel (>= 4.0.2)
activerecord
activesupport (>= 3.0.0)
Expand All @@ -10,8 +10,8 @@ PATH
faraday-http
faraday_middleware
i18n
barkibu-kb-fake (0.25.0)
barkibu-kb (= 0.25.0)
barkibu-kb-fake (0.26.0)
barkibu-kb (= 0.26.0)
countries
sinatra
webmock
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ KB.config.log_level = :debugger # :info by default
- returns all the KB::Referral associated with this pet parent
- `referrers`
- returns all the KB::Referral associated with any of the pet parent's pets
- `iban`
- returns the IBAN of the pet parent
- `update_iban`
- arg: `iban` string
- updates the IBAN of the pet parent and reloads the entity

#### Assessment 📄

Expand Down
28 changes: 28 additions & 0 deletions lib/kb/fake/bounded_context/pet_family/pet_parents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ def petparents_filterable_attributes
KB::PetParent::FIELDS.map { |k| k.to_s.camelize(:lower) }
end

def on_petparents_show(_version)
pet_parent = pet_parent_by_key(params).dup
return json_response 404, {} if pet_parent.nil?

pet_parent['iban_last4'] = pet_parent.delete('iban')&.chars&.last(4)&.join

json_response 200, pet_parent
end

get '/v1/petparents/:key/pets' do
json_response 200, pets_by_pet_parent_key(params['key'])
end
Expand Down Expand Up @@ -70,6 +79,25 @@ def petparents_filterable_attributes
json_response 200, resource
end

get '/v1/petparents/:key/iban' do
pet_parent = pet_parent_by_key(params)
return json_response 404, {} if pet_parent.blank?

json_response 200, { iban: pet_parent['iban'] }
end

put '/v1/petparents/:key/iban' do
pet_parent = pet_parent_by_key(params)
return json_response 404, {} if pet_parent.blank?

body = JSON.parse(request.body.read)

updated_pet_parent = pet_parent.merge body.slice('iban')
update_resource_state(:petparents, updated_pet_parent)

json_response 200, { iban: updated_pet_parent['iban'] }
end

private

def pet_parent_by_key(params)
Expand Down
10 changes: 4 additions & 6 deletions lib/kb/models/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def attributes_from_response(response)
end
end

STRING_FIELDS = %i[key pet_key urgency].freeze
FIELDS = [*STRING_FIELDS, :date, :should_stop, :finished, :conditions, :symptoms, :next_question].freeze

# Legacy Field Name From Anamnesis
alias_attribute :consultation_id, :key
alias_attribute :should_stop, :finished
Expand All @@ -42,12 +45,7 @@ def attributes_from_response(response)
attribute :date, :datetime
attribute :finished, :boolean, default: false

attribute :urgency, :string
attribute :key, :string
attribute :pet_key, :string

STRING_FIELDS = %i[key pet_key urgency].freeze
FIELDS = [*STRING_FIELDS, :date, :should_stop, :finished, :conditions, :symptoms, :next_question].freeze
define_attributes STRING_FIELDS, :string

def urgent
return false if urgency == 'low'
Expand Down
28 changes: 17 additions & 11 deletions lib/kb/models/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ class BaseModel

class << self
delegate :clear_cache_for, to: :kb_client

def define_attribute_methods(*fields)
super
fields.each do |field|
define_method :"#{field}=" do |value|
super(value).tap do
public_send "#{field}_will_change!" if public_send("#{field}_changed?")
end
end
end
end

def define_attributes(attributes, cast_type = nil)
attributes.each do |attribute|
attribute attribute, cast_type&.to_sym
end
end
end

def initialize(attributes = {})
Expand All @@ -39,16 +56,5 @@ def ==(other)
other.key == key)
end
alias eql? ==

def self.define_attribute_methods(*fields)
super
fields.each do |field|
define_method :"#{field}=" do |value|
super(value).tap do
public_send "#{field}_will_change!" if public_send("#{field}_changed?")
end
end
end
end
end
end
4 changes: 1 addition & 3 deletions lib/kb/models/breed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def self.attributes_from_response(response)

define_attribute_methods(*FIELDS)

STRING_FIELDS.each do |field|
attribute field, :string
end
define_attributes STRING_FIELDS, :string
end
end
6 changes: 6 additions & 0 deletions lib/kb/models/concerns/findable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ def find(key, params = {})
raise KB::Error.from_faraday(e)
end
end

def reload
self.class.clear_cache_for(key)
self.attributes = self.class.find(key).attributes
self
end
end
end
9 changes: 2 additions & 7 deletions lib/kb/models/hubspot_relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ def self.attributes_from_response(response)

define_attribute_methods(*FIELDS)

STRING_FIELDS.each do |field|
attribute field, :string
end

DATE_FIELDS.each do |field|
attribute field, :date
end
define_attributes STRING_FIELDS, :string
define_attributes DATE_FIELDS, :date

def self.find(model, model_key)
response = kb_client.request("#{model}/#{model_key}/relationship")
Expand Down
14 changes: 3 additions & 11 deletions lib/kb/models/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,9 @@ def self.attributes_from_response(response)

define_attribute_methods(*FIELDS)

STRING_FIELDS.each do |field|
attribute field, :string
end

BOOLEAN_FIELDS.each do |field|
attribute field, :boolean
end

DATE_FIELDS.each do |field|
attribute field, :date
end
define_attributes STRING_FIELDS, :string
define_attributes DATE_FIELDS, :date
define_attributes BOOLEAN_FIELDS, :boolean

def save!
return unless changed?
Expand Down
14 changes: 3 additions & 11 deletions lib/kb/models/pet_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@ def self.attributes_from_response(response)

define_attribute_methods(*FIELDS)

STRING_FIELDS.each do |field|
attribute field, :string
end

DATE_FIELDS.each do |field|
attribute field, :date
end

INTEGER_FIELDS.each do |field|
attribute field, :integer
end
define_attributes STRING_FIELDS, :string
define_attributes DATE_FIELDS, :date
define_attributes INTEGER_FIELDS, :integer

def save!
return unless changed?
Expand Down
32 changes: 18 additions & 14 deletions lib/kb/models/pet_parent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def self.attributes_from_response(response)

private_class_method :attributes_from_response

STRING_FIELDS = %i[key partner_name first_name last_name prefix_phone_number
phone_number email country address zip_code nif affiliate_code city].freeze
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
Expand All @@ -52,18 +52,9 @@ def self.attributes_from_response(response)
alias phone_number_prefix prefix_phone_number
alias phone_number_prefix= prefix_phone_number=

STRING_FIELDS.each do |field|
attribute field, :string
end

DATE_FIELDS.each do |field|
attribute field, :date
end

BOOLEAN_FIELDS.each do |field|
attribute field, :boolean
end

define_attributes STRING_FIELDS, :string
define_attributes DATE_FIELDS, :date
define_attributes BOOLEAN_FIELDS, :boolean
attribute :first_name, :string, default: ''

def save!
Expand Down Expand Up @@ -119,5 +110,18 @@ def referrers
Referral.from_api(referral)
end
end

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

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
end
end
9 changes: 2 additions & 7 deletions lib/kb/models/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ def self.attributes_from_response(response)
attribute :plan_life_in_months, :integer
attribute :buyable, :boolean

STRING_FIELDS.each do |field|
attribute field, :string
end

HASH_FIELDS.each do |field|
attribute field
end
define_attributes STRING_FIELDS, :string
define_attributes HASH_FIELDS

def save!
return unless changed?
Expand Down
9 changes: 2 additions & 7 deletions lib/kb/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ def self.attributes_from_response(response)

attribute :purchasable, :boolean

STRING_FIELDS.each do |field|
attribute field, :string
end

STRING_ARRAY_FIELDS.each do |field|
attribute field, :array_of_strings
end
define_attributes STRING_FIELDS, :string
define_attributes STRING_ARRAY_FIELDS, :array_of_strings
end
end
9 changes: 2 additions & 7 deletions lib/kb/models/referral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ def self.attributes_from_response(response)

define_attribute_methods(*FIELDS)

STRING_FIELDS.each do |field|
attribute field, :string
end

DATE_FIELDS.each do |field|
attribute field, :date
end
define_attributes STRING_FIELDS, :string
define_attributes DATE_FIELDS, :date

def self.create(pet_parent_key, attributes)
response = kb_client.request("#{pet_parent_key}/referrals", filters: attributes, method: :post)
Expand Down
2 changes: 1 addition & 1 deletion lib/kb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module KB
VERSION = '0.25.0'.freeze
VERSION = '0.26.0'.freeze
end
44 changes: 44 additions & 0 deletions spec/fake/models/pet_parent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,48 @@
end
end
end

describe '#iban_last4' do
context 'with an existing pet parent' do
let(:pet_parent) { described_class.new described_class.create }

context 'with no iban' do
it 'returns nil' do
expect(pet_parent.iban_last4).to be_nil
end
end

context 'with an iban' do
before do
pet_parent.update_iban('ES9121000418450200051332')
end

it 'returns the last 4 digits from the iban of the pet parent' do
expect(pet_parent.iban_last4).to eq('1332')
end
end
end
end

describe '#iban' do
context 'with an existing pet parent' do
let(:pet_parent) { described_class.new described_class.create }

context 'with no iban' do
it 'returns nil' do
expect(pet_parent.iban).to be_nil
end
end

context 'with an iban' do
before do
pet_parent.update_iban('ES9121000418450200051332')
end

it 'returns the iban of the pet parent' do
expect(pet_parent.iban).to eq('ES9121000418450200051332')
end
end
end
end
end
Loading

0 comments on commit a0ddf27

Please sign in to comment.