Sha256: 43870c8d097e7313be4cca52510d13d03d1f70ec6f8e83024f7096a1256584da

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

require 'finapps_core'
require_relative './version'

module FinApps
  module REST
    class Client < FinAppsCore::REST::BaseClient # :nodoc:
      RESOURCES = %i[
        institutions
        institutions_forms
        orders
        order_assignments
        order_notifications
        order_refreshes
        order_reports
        order_statuses
        order_tokens
        operators
        operators_password_resets
        password_resets
        products
        sessions
        statements
        consumers
        consumer_institution_refreshes
        tenant_settings
        tenant_app_settings
        user_institutions
        user_institutions_forms
        user_institutions_statuses
        version
      ].freeze

      # @param [String] tenant_token
      # @param [Hash] options
      # @return [FinApps::REST::Client]
      def initialize(tenant_token, options = {}, logger = nil)
        not_blank(tenant_token, :tenant_token)

        merged_options = options.merge(tenant_token: tenant_token)
        super(merged_options, logger)
      end

      def method_missing(symbol, *arguments, &block)
        if RESOURCES.include? symbol
          class_name = camelize(symbol.to_s)
          variable = "@#{class_name.downcase}"
          unless instance_variable_defined? variable
            klass = Object.const_get('FinApps').const_get('REST').const_get class_name
            instance_variable_set(variable, klass.new(self))
          end
          instance_variable_get(variable)
        else
          super
        end
      end

      def respond_to_missing?(method_sym, include_private = false)
        RESOURCES.include?(method_sym) ? true : super
      end

      private

      def camelize(term)
        string = term.to_s
        string = string.sub(/^[a-z\d]*/) { $&.capitalize }
        string.gsub!(%r{(?:_|(/))([a-z\d]*)}) { Regexp.last_match(2).capitalize.to_s }
        string
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
finapps-4.0.6 lib/finapps/rest/client.rb
finapps-4.0.5 lib/finapps/rest/client.rb
finapps-4.0.4 lib/finapps/rest/client.rb