Sha256: d6755c2acf157a872464ae16f68ce8bf1b00e4075cebba5722e390a4d96ffada

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module JSONAPI
  # c.f. http://jsonapi.org/format/#document-resource-identifier-objects
  class ResourceIdentifier
    attr_reader :id, :type

    def initialize(resource_identifier_hash, options = {})
      @hash = resource_identifier_hash

      validate!(resource_identifier_hash)

      @id = resource_identifier_hash['id']
      @type = resource_identifier_hash['type']
    end

    def to_hash
      @hash
    end

    private

    def validate!(resource_identifier_hash)
      case
      when !resource_identifier_hash.key?('id')
        fail InvalidDocument,
             "a resource identifier object MUST contain an 'id'"
      when !resource_identifier_hash['id'].is_a?(String)
        fail InvalidDocument, "the value of 'id' MUST be a string"
      when !resource_identifier_hash.key?('type')
        fail InvalidDocument,
             "a resource identifier object MUST contain a 'type'"
      when !resource_identifier_hash['type'].is_a?(String)
        fail InvalidDocument, "the value of 'type' MUST be a string"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jsonapi-0.1.1.beta2 lib/jsonapi/resource_identifier.rb
jsonapi-0.1.1.beta1 lib/jsonapi/resource_identifier.rb