Sha256: c4a0a34ddc8bea0137cb1eb5f03d1a5a2d7d01e998e730f34080b4d08e0bcbc2

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'time'
require 'base64'
require 'json'
require 'fractal_api/base_model'

module FractalApi
  class Consent < BaseModel
    attributes :consent_id, :company_id, :bank_id, :permission,
               :date_created, :consent_type, :status, :sign_in_url

    def self.create(bank_id, company_id, redirect_url)
      params = { bankId: bank_id.to_s, redirect: redirect_url, permissions: 'READALLBANKDATA' }
      result = post('/banking/v2/consents', params: params, headers: { 'X-Company-Id' => company_id })

      build(json: result.body)
    end

    def self.build(json:)
      super.tap do |record|
        record.date_created = Time.parse(record.date_created) if record.date_created
      end
    end

    def self.build_from_state(state)
      decoded = JSON.parse(Base64.decode64(state), symbolize_names: true)

      new(
        consent_id: decoded[:accReq],
        company_id: decoded[:cId],
        bank_id: decoded[:bId]
      )
    rescue JSON::ParserError
      nil
    end

    def update(code:, id_token:, state:)
      url = format_url(
        '/banking/v2/consents/:consent_id',
        consent_id: consent_id
      )

      result = put(
        url,
        params: { bankId: bank_id, code: code, id_token: id_token, state: state },
        headers: { 'X-Company-Id' => company_id }
      )

      result.body
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fractal_api-0.1.5 lib/fractal_api/consent.rb
fractal_api-0.1.4 lib/fractal_api/consent.rb
fractal_api-0.1.3 lib/fractal_api/consent.rb