Sha256: 68124621c11933315e6b3c0d66e3d779115277d31637508130000d8ca583b789

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

module Cts
  module Mpx
    #
    # Collection of methods that will validate input based on specific tests.
    #
    module Validators
      module_function

      # Validates if a string is a long form id
      # @param [String] account_id long form id
      # @return [Boolean] true if it is a long form id, false if it is not.
      def account_id?(account_id)
        return true if account_id == 'urn:theplatform:auth:root'
        return false unless reference? account_id
        return false unless account_id.downcase.match?(/\/account\/\d+$/)

        true
      end

      # Test to check for validity of argument by type, can also accept a block.
      # @note test
      # @param [Object] data object to check
      # @param [Class] type class type to accept
      # @yield Description of block
      # @yieldreturn [boolean] true if the outcome is valid, false otherwise.
      # @return [boolean] true if the outcome is valid, false otherwise.
      def argument_error?(data, type = nil, &block)
        return block.yield if block
        return true unless type && data.is_a?(type)
        false
      end

      # Validates if a string is a reference
      # @param [String] uri reference
      # @return [Boolean] true if it is a reference, false if it is not.
      def reference?(uri)
        begin
          ref = URI.parse uri
        rescue URI::InvalidURIError
          return false
        end

        return false if ref.host == 'web.theplatform.com'
        return false unless ref.scheme == "http" || ref.scheme == "https"
        return false unless ref.host.end_with? ".theplatform.com"
        return false if ref.host.start_with? 'feed.media.theplatform'
        true
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cts-mpx-1.0.3 lib/cts/mpx/validators.rb
cts-mpx-1.0.2 lib/cts/mpx/validators.rb
cts-mpx-1.0.1 lib/cts/mpx/validators.rb