class IABConsentString::Consent::Implementation::V1::VendorConsentBuilder

Builder for version 1 of vendor consent

Constants

VERSION

Public Instance Methods

build() click to toggle source

Validate supplied values and build VendorConsent object @return [IABConsentString::Consent::VendorConsent] vendor consent object

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 141
def build
  if @consentRecordCreated.nil?
    raise IABConsentString::Error::VendorConsentCreateError, "consentRecordCreated must be set", caller
  end
  if @consentRecordLastUpdated.nil?
    raise IABConsentString::Error::VendorConsentCreateError, "consentRecordLastUpdated must be set", caller
  end
  if @consentLanguage.nil?
    raise IABConsentString::Error::VendorConsentCreateError, "consentLanguage must be set", caller
  end

  if @vendorListVersion.nil? || @vendorListVersion <=0
    raise IABConsentString::Error::VendorConsentCreateError, "Invalid value for vendorListVersion:" + @vendorListVersion.to_s, caller
  end

  if @maxVendorId.nil? || @maxVendorId<=0
    raise IABConsentString::Error::VendorConsentCreateError, "Invalid value for maxVendorId:" + @maxVendorId.to_s, caller
  end

  # For range encoding, check if each range entry is valid
  if @vendorEncodingType == IABConsentString::GDPRConstants::VENDOR_ENCODING_RANGE
    if @rangeEntries.nil?
      raise IABConsentString::Error::VendorConsentCreateError, "Range entries  must be set", caller
    end
    @rangeEntries.each do |rangeEntry|
      if !rangeEntry.valid(@maxVendorId)
        raise IABConsentString::Error::VendorConsentCreateError, "Invalid range entries found", caller
      end
    end
  end

  # Calculate size of bit buffer in bits
  bitBufferSizeInBits = 0
  if (@vendorEncodingType == IABConsentString::GDPRConstants::VENDOR_ENCODING_RANGE)
    rangeEntrySectionSize = 0
    @rangeEntries.each do |rangeEntry|
      rangeEntrySectionSize += rangeEntry.size
    end
    bitBufferSizeInBits = IABConsentString::GDPRConstants::RANGE_ENTRY_OFFSET + rangeEntrySectionSize
  else
    bitBufferSizeInBits = IABConsentString::GDPRConstants::VENDOR_BITFIELD_OFFSET + @maxVendorId
  end

  # Create new bit buffer
  bitsFit = (bitBufferSizeInBits % 8) == 0
  str = ""
  for i in (0...(bitBufferSizeInBits / 8 + (bitsFit ? 0 : 1))) do
    str << 0b00000000
  end
  bits = IABConsentString::Bits.new(str.bytes.to_a)

  # Set fields in bit buffer
  bits.setInt(IABConsentString::GDPRConstants::VERSION_BIT_OFFSET, IABConsentString::GDPRConstants::VERSION_BIT_SIZE, VERSION)
  bits.setDateTimeToEpochDeciseconds(IABConsentString::GDPRConstants::CREATED_BIT_OFFSET, IABConsentString::GDPRConstants::CREATED_BIT_SIZE, @consentRecordCreated)
  bits.setDateTimeToEpochDeciseconds(IABConsentString::GDPRConstants::UPDATED_BIT_OFFSET, IABConsentString::GDPRConstants::UPDATED_BIT_SIZE, @consentRecordLastUpdated)
  bits.setInt(IABConsentString::GDPRConstants::CMP_ID_OFFSET, IABConsentString::GDPRConstants::CMP_ID_SIZE, @cmpId)
  bits.setInt(IABConsentString::GDPRConstants::CMP_VERSION_OFFSET, IABConsentString::GDPRConstants::CMP_VERSION_SIZE, @cmpVersion)
  bits.setInt(IABConsentString::GDPRConstants::CONSENT_SCREEN_SIZE_OFFSET, IABConsentString::GDPRConstants::CONSENT_SCREEN_SIZE, @consentScreenId)
  bits.setSixBitString(IABConsentString::GDPRConstants::CONSENT_LANGUAGE_OFFSET, IABConsentString::GDPRConstants::CONSENT_LANGUAGE_SIZE, @consentLanguage)
  bits.setInt(IABConsentString::GDPRConstants::VENDOR_LIST_VERSION_OFFSET, IABConsentString::GDPRConstants::VENDOR_LIST_VERSION_SIZE, @vendorListVersion)

  # Set purposes bits
  for i in (0...IABConsentString::GDPRConstants::PURPOSES_SIZE) do
    if (@allowedPurposes.include?(i+1))
      bits.setBit(IABConsentString::GDPRConstants::PURPOSES_OFFSET + i)
    else
      bits.unsetBit(IABConsentString::GDPRConstants::PURPOSES_OFFSET + i)
    end
  end

  bits.setInt(IABConsentString::GDPRConstants::MAX_VENDOR_ID_OFFSET, IABConsentString::GDPRConstants::MAX_VENDOR_ID_SIZE, @maxVendorId)
  bits.setInt(IABConsentString::GDPRConstants::ENCODING_TYPE_OFFSET, IABConsentString::GDPRConstants::ENCODING_TYPE_SIZE, @vendorEncodingType)

  # Set the bit field or range sections
  if (@vendorEncodingType == IABConsentString::GDPRConstants::VENDOR_ENCODING_RANGE)
    # Range encoding
    if (@defaultConsent)
      bits.setBit(IABConsentString::GDPRConstants::DEFAULT_CONSENT_OFFSET)
    else
      bits.unsetBit(IABConsentString::GDPRConstants::DEFAULT_CONSENT_OFFSET)
    end
    bits.setInt(IABConsentString::GDPRConstants::NUM_ENTRIES_OFFSET, IABConsentString::GDPRConstants::NUM_ENTRIES_SIZE, @rangeEntries.size)

    currentOffset = IABConsentString::GDPRConstants::RANGE_ENTRY_OFFSET

    @rangeEntries.each do |rangeEntry|
      currentOffset = rangeEntry.appendTo(bits, currentOffset)
    end
  else
    # Bit field encoding
    for i in (0...@maxVendorId) do
      if @vendorsBitField.include?(i+1)
        bits.setBit(IABConsentString::GDPRConstants::VENDOR_BITFIELD_OFFSET+i)
      else
        bits.unsetBit(IABConsentString::GDPRConstants::VENDOR_BITFIELD_OFFSET+i)
      end
    end
  end

  IABConsentString::Consent::Implementation::V1::ByteBufferBackedVendorConsent.new(bits)
end
withAllowedPurposeIds(allowedPurposeIds) click to toggle source

With allowed purpose IDs @param allowedPurposeIds [Set<Integer>] set of allowed purposes @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 72
def withAllowedPurposeIds(allowedPurposeIds)
  if allowedPurposeIds.nil?
    raise "Argument allowedPurposeIds is null"
  end
  allowedPurposeIds.each do |purposeId|
    if purposeId < 0 || purposeId > IABConsentString::GDPRConstants::PURPOSES_SIZE
      raise "Invalid purpose ID found"
    end
  end
  @allowedPurposes = allowedPurposeIds;
  self
end
withAllowedPurposes(allowedPurposes) click to toggle source

With allowed purposes @param allowedPurposes [Set<Purpose>] set of allowed purposes @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 88
def withAllowedPurposes(allowedPurposes)
  if allowedPurposes.nil?
    raise "Argument allowedPurposes is null"
  end
  @allowedPurposes = allowedPurposes.map! {|purpose| purpose.getId }
  self
end
withBitField(bitFieldEntries) click to toggle source

With bit field entries @param bitFieldEntries [Set<Integer>] set of VendorIds for which the vendors have consent @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 118
def withBitField(bitFieldEntries)
  @vendorsBitField = bitFieldEntries
  self
end
withCmpId(cmpId) click to toggle source

With CMP Id @param cmpId [Integer] Consent Manager Provider Id @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 40
def withCmpId(cmpId)
  @cmpId = cmpId
  self
end
withCmpVersion(cmpVersion) click to toggle source

With CMP version @param cmpVersion [Integer] Consent Manager Provider version @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 32
def withCmpVersion(cmpVersion)
  @cmpVersion = cmpVersion
  self
end
withConsentLanguage(consentLanguage) click to toggle source

With consent language @param consentLanguage [Char(2)] Two-letter ISO639-1 language code that CMP asked for consent in @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 56
def withConsentLanguage(consentLanguage)
  @consentLanguage = consentLanguage
  self
end
withConsentRecordCreatedOn(consentRecordCreated) click to toggle source

With creation date @param consentRecordCreated [DateTime] Epoch deciseconds when record was created @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 16
def withConsentRecordCreatedOn(consentRecordCreated)
  @consentRecordCreated = consentRecordCreated
  self
end
withConsentRecordLastUpdatedOn(consentRecordLastUpdated) click to toggle source

With update date @param consentRecordLastUpdated [DateTime] Epoch deciseconds when consent string was last updated @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 24
def withConsentRecordLastUpdatedOn(consentRecordLastUpdated)
  @consentRecordLastUpdated = consentRecordLastUpdated;
  self
end
withConsentScreenId(consentScreenId) click to toggle source

With Consent Screen Id @param consentScreenId [Integer] Consent Screen Id @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 48
def withConsentScreenId(consentScreenId)
  @consentScreenId = consentScreenId
  self
end
withDefaultConsent(defaultConsent) click to toggle source

With default consent @param defaultConsent [Boolean] Default consent for VendorIds not covered by a RangeEntry. 0=No Consent 1=Consent @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 134
def withDefaultConsent(defaultConsent)
  @defaultConsent = defaultConsent
  self
end
withMaxVendorId(maxVendorId) click to toggle source

With max vendor ID @param maxVendorId [Integer] The maximum VendorId for which consent values are given. @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 99
def withMaxVendorId(maxVendorId)
  @maxVendorId = maxVendorId
  self
end
withRangeEntries(rangeEntries) click to toggle source

With range entries @param rangeEntries [Set<RangeEntry>] List of VendorIds or a range of VendorIds for which the vendors have consent @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 126
def withRangeEntries(rangeEntries)
  @rangeEntries = rangeEntries
  self
end
withVendorEncodingType(vendorEncodingType) click to toggle source

With vendor encoding type @param vendorEncodingType [Integer] 0=BitField 1=Range @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 107
def withVendorEncodingType(vendorEncodingType)
  if (vendorEncodingType < 0 || vendorEncodingType > 1)
    raise "Illegal value for argument vendorEncodingType:" + vendorEncodingType.to_s
  end
  @vendorEncodingType = vendorEncodingType
  self
end
withVendorListVersion(vendorListVersion) click to toggle source

With vendor list version @param vendorListVersion [Integer] Version of vendor list used in most recent consent string update @return [VendorConsentBuilder] self

# File lib/iab_consent_string/consent/implementation/v1/vendor_consent_builder.rb, line 64
def withVendorListVersion(vendorListVersion)
  @vendorListVersion = vendorListVersion
  self
end