class Shaf::ALPS::RelationSerializer

Constants

IDEMPOTENT_METHODS
SAFE_METHODS
UNSAFE_METHODS

Attributes

rel[R]

Public Class Methods

call(arg) click to toggle source
# File lib/shaf/alps/relation_serializer.rb, line 12
def self.call(arg)
  new(arg).to_hash
end
new(rel) click to toggle source
# File lib/shaf/alps/relation_serializer.rb, line 16
def initialize(rel)
  @rel = rel
end

Public Instance Methods

to_hash() click to toggle source
# File lib/shaf/alps/relation_serializer.rb, line 20
def to_hash
  {
    id: rel.id,
    type: type,
  }.merge(optional_properties)
end

Private Instance Methods

extension() click to toggle source
# File lib/shaf/alps/relation_serializer.rb, line 52
def extension
  methods = rel.http_methods
  return unless methods

  [
    {
      id: :http_method,
      href: 'https://gist.github.com/sammyhenningsson/2103d839eb79a7baf8854bfb96bda7ae',
      value: methods,
    }
  ]
end
optional_properties() click to toggle source
# File lib/shaf/alps/relation_serializer.rb, line 29
def optional_properties
  descriptors = serialized_descriptors
  hash = {}
  hash[:href] = rel.href if rel.href
  hash[:doc] = { value: rel.doc } if rel.doc
  hash[:name] = rel.name.to_s if rel.name
  hash[:rt] = rel.content_type if rel.content_type
  hash[:descriptor] = descriptors unless descriptors.empty?
  hash[:ext] = extension if extension
  hash
end
serialized_descriptors() click to toggle source
# File lib/shaf/alps/relation_serializer.rb, line 65
def serialized_descriptors
  rel.attributes.map { |attr| AttributeSerializer.call(attr) }
end
type() click to toggle source
# File lib/shaf/alps/relation_serializer.rb, line 41
def type
  methods = rel.http_methods
  if methods.all? { |m| SAFE_METHODS.include? m }
    'safe'
  elsif methods.all? { |m| (SAFE_METHODS + IDEMPOTENT_METHODS).include? m }
    'idempotent'
  else
    'unsafe'
  end
end