Class: Sidetree::Model::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/sidetree/model/service.rb

Constant Summary collapse

MAX_TYPE_LENGTH =
30

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, type, endpoint) ⇒ Service

Returns a new instance of Service.

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sidetree/model/service.rb', line 11

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

Instance Attribute Details

#endpointObject (readonly)

URI string or JSON object



8
9
10
# File 'lib/sidetree/model/service.rb', line 8

def endpoint
  @endpoint
end

#idObject (readonly)

String



6
7
8
# File 'lib/sidetree/model/service.rb', line 6

def id
  @id
end

#typeObject (readonly)

String



7
8
9
# File 'lib/sidetree/model/service.rb', line 7

def type
  @type
end

Class Method Details

.from_hash(data) ⇒ Sidetree::Model::Service

Generate service from json object.

Parameters:

  • data (Hash)

    Hash params.

Options Hash (data):

  • :id (String)

    id

  • :type (String)

    type

  • :endpoint (String || Object)

    endpoint url

Returns:

Raises:



35
36
37
# File 'lib/sidetree/model/service.rb', line 35

def self.from_hash(data)
  Service.new(data["id"], data["type"], data["serviceEndpoint"])
end

Instance Method Details

#to_hHash

Convert data to Hash object.

Returns:

  • (Hash)


41
42
43
44
45
46
47
# File 'lib/sidetree/model/service.rb', line 41

def to_h
  hash = {}
  hash["id"] = id if id
  hash["type"] = type if type
  hash["serviceEndpoint"] = endpoint if endpoint
  hash
end