Sha256: bf6b64a9d7693d35156e0330aab76632551d9e910abe5f1a1f85a37d7abdade0

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

module Sidetree
  module Model
    class Service
      MAX_TYPE_LENGTH = 30

      attr_reader :id # String
      attr_reader :type # String
      attr_reader :endpoint # URI string or JSON object

      # @raise [Sidetree::Error]
      def initialize(id, type, endpoint)
        Sidetree::Validator.validate_id!(id)
        raise Error, "type should be String." unless type.is_a?(String)
        if type.length > MAX_TYPE_LENGTH
          raise Error,
                "Service endpoint type length #{type.length} exceeds max allowed length of #{MAX_TYPE_LENGTH}."
        end
        if endpoint.is_a?(Array)
          raise Error, "Service endpoint value cannot be an array."
        end

        Sidetree::Validator.validate_uri!(endpoint) if endpoint.is_a?(String)
        @id = id
        @type = type
        @endpoint = endpoint
      end

      # Generate service from json object.
      # @param [Hash] data Hash params.
      # @option data [String] :id id
      # @option data [String] :type type
      # @option data [String || Object] :endpoint endpoint url
      # @raise [Sidetree::Error]
      # @return [Sidetree::Model::Service]
      def self.from_hash(data)
        Service.new(data["id"], data["type"], data["serviceEndpoint"])
      end

      # Convert data to Hash object.
      # @return [Hash]
      def to_h
        hash = {}
        hash["id"] = id if id
        hash["type"] = type if type
        hash["serviceEndpoint"] = endpoint if endpoint
        hash
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sidetree-0.1.5 lib/sidetree/model/service.rb
sidetree-0.1.4 lib/sidetree/model/service.rb
sidetree-0.1.3 lib/sidetree/model/service.rb
sidetree-0.1.2 lib/sidetree/model/service.rb
sidetree-0.1.1 lib/sidetree/model/service.rb