Sha256: d91dcaa25d9e278f08894b4777339749cd63f3c7739703319243bf14c802d294

Contents?: true

Size: 1.88 KB

Versions: 44

Compression:

Stored size: 1.88 KB

Contents

module ZuoraConnect
  class Login
    attr_accessor :clients, :identifier_to_ids, :default_entity
    def initialize (fields)
      self.clients = {}
      self.identifier_to_ids = {}
      if fields["tenant_type"] == "Zuora"
        login_fields = fields.map{|k,v| [k.to_sym, v]}.to_h
        login_type = fields.dig("authentication_type").blank? ? 'Basic' : fields.dig("authentication_type").capitalize
        
        raise ZuoraConnect::Exceptions::InvalidCredentialSet.new("Cannot setup application with ZSession Login.") if login_type == "Session"
        self.clients["Default"] = "::ZuoraAPI::#{login_type}".constantize.new(**login_fields)
        self.default_entity = fields["entities"][0]["id"] if (fields.dig("entities") || []).size == 1
        if fields["entities"] && fields["entities"].size > 0
          fields["entities"].each do |entity|
            params = {:entity_id => entity["id"], :entity_identifier => entity["identifier"]}.merge(login_fields)
            self.clients[entity["id"]] =  "::ZuoraAPI::#{login_type}".constantize.new(**params)
            self.identifier_to_ids[entity["identifier"]] = entity["id"]
          end
        end
        self.attr_builder("available_entities", self.clients.keys) 
      end
      fields.each do |k,v|
        self.attr_builder(k,v)
      end
      self.default_entity ||= "Default"
    end

    def attr_builder(field,val)
      singleton_class.class_eval { attr_accessor "#{field}" }
      send("#{field}=", val)
    end

    def client(id = self.default_entity)
      use_entity_name = self.identifier_to_ids.keys.include?(id)

      # Translate entity name to entity id
      id = self.identifier_to_ids[id] if use_entity_name

      client = id.blank? ? self.clients[self.default_entity] : self.clients[id]

      if client
        client.entity_header_type = use_entity_name ? :entity_name : :entity_id
      end

      client
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
zuora_connect-3.2.10 app/models/zuora_connect/login.rb
zuora_connect-3.2.9 app/models/zuora_connect/login.rb
zuora_connect-3.2.8 app/models/zuora_connect/login.rb
zuora_connect-3.2.7 app/models/zuora_connect/login.rb
zuora_connect-3.2.6 app/models/zuora_connect/login.rb
zuora_connect-3.2.5 app/models/zuora_connect/login.rb
zuora_connect-3.2.4 app/models/zuora_connect/login.rb
zuora_connect-3.2.3 app/models/zuora_connect/login.rb
zuora_connect-3.2.2 app/models/zuora_connect/login.rb
zuora_connect-3.2.0 app/models/zuora_connect/login.rb
zuora_connect-3.1.5.pre.g app/models/zuora_connect/login.rb
zuora_connect-3.1.5.pre.f app/models/zuora_connect/login.rb
zuora_connect-3.1.5.pre.e app/models/zuora_connect/login.rb
zuora_connect-3.1.5.pre.d app/models/zuora_connect/login.rb
zuora_connect-3.1.5.pre.c app/models/zuora_connect/login.rb
zuora_connect-3.1.5.pre.b app/models/zuora_connect/login.rb
zuora_connect-3.1.5.pre.a app/models/zuora_connect/login.rb
zuora_connect-3.1.5 app/models/zuora_connect/login.rb
zuora_connect-3.1.4 app/models/zuora_connect/login.rb
zuora_connect-3.1.3 app/models/zuora_connect/login.rb