Slide 17
Slide 17 text
ModelɿBase
module CordaRecord
module FinderMethods
extend ActiveSupport::Concern
module ClassMethods
def new_from_corda_params(params)
new(find_mapping(params))
end
def find_mapping(*)
raise NotImplementedError
end
def find(id)
response = corda_client.get "#{api_name}/#{id}"
new_from_corda_params(JSON.parse(response.body))
rescue Faraday::ResourceNotFound
raise Errors::RecordNotFound, "Cannot find #{name}"
end
def find_by(attributes)
body = find_response(attributes)
return nil if body.empty?
new_from_corda_params(body.first)
rescue Faraday::ResourceNotFound
nil
end
……..
private
def find_response(attributes)
response = corda_client.get "#{api_name}/" do |req|
req.params = attributes.stringify_keys.transform_keys { |key| key.camelize(:lower) }.symbolize_keys
end
JSON.parse(response.body)
end
end
end
end
module Corda
class ContractBase < CordaRecord::Base # rubocop:disable Metrics/ClassLength
include CordaRecord::FinderMethods
attribute :corda_id, :string
attribute :client_transaction_id, :string
attribute :title, :string
…….
def self.find_mapping(json) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
normalize_json(json)
proposer = proposer_json(json)
proposee = proposee_json(json)
{
corda_id: json['linerId'],
client_contract_id: json['guestContractId'],
title....
}
end
def self.proposer_json(json)
proposer = parse_jsons_and_merge(json['proposerCompanyInfo'],
json['proposerDepartmentInfo'])
user_infos = json['userInfo'].deep_symbolize_keys
proposer.merge(userInfo: user_infos[:proposer])
end
…….
end
end