Sha256: 44a686dea0b6aecdd9730e49188e2954c79b29b064a5b008eb14fb7697c9c37c

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module CampactUserService
  class Account
    attr_reader :client, :user_id

    # user_id can either be the user's external_account_id or their email address
    def initialize(client, user_id)
      @client = client
      @user_id = user_id
    end

    def exists?
      account && !account['external_id'].nil?
    end

    def subscribed_to_newsletter?
      subscriptions = account.dig('emailaddress', 'subscriptions') || []
      subscriptions.any? {|s| s['type'] == 'newsletter' }
    end

    def allow_prefill?
      prefill = account.dig('preferences', 'prefill_forms')
      prefill.to_s == 'allowed'
    end

    def name
      account['name']
    end

    def email
      account.dig('emailaddress', 'emailaddress')
    end

    def address
      account['postaladdress']
    end

    def preferences
      account['preferences']
    end

    private

    def account
      escaped_user_id = CGI.escape(user_id)
      @account_info ||= (client.get_request("v1/accounts/#{escaped_user_id}") || {})
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
campact_user_service-4.1.1 lib/campact_user_service/account.rb
campact_user_service-4.1.0 lib/campact_user_service/account.rb