Sha256: 0e72a98598ad0ac4e4454e845bb61c7d25e499301f0b6b85dbc9dd593257a003

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'excon/hypermedia/resource_object/embedded'
require 'excon/hypermedia/resource_object/links'
require 'excon/hypermedia/resource_object/properties'

module Excon
  module HyperMedia
    # ResourceObject
    #
    # Represents a resource.
    #
    class ResourceObject
      RESERVED_PROPERTIES = %w(_links _embedded).freeze

      def initialize(data)
        @data = data
      end

      def _properties
        @_properties ||= Properties.new(@data.reject { |k, _| RESERVED_PROPERTIES.include?(k) })
      end

      # _links
      #
      # The reserved "_links" property is OPTIONAL.
      #
      # It is an object whose property names are link relation types (as
      # defined by [RFC5988]) and values are either a Link Object or an array
      # of Link Objects.  The subject resource of these links is the Resource
      # Object of which the containing "_links" object is a property.
      #
      # @see https://tools.ietf.org/html/draft-kelly-json-hal-08#section-4.1.1
      #
      def _links
        @_links ||= Links.new(@data['_links'])
      end

      # _embedded
      #
      # The reserved "_embedded" property is OPTIONAL
      #
      # It is an object whose property names are link relation types (as
      # defined by [RFC5988]) and values are either a Resource Object or an
      # array of Resource Objects.
      #
      # Embedded Resources MAY be a full, partial, or inconsistent version of
      # the representation served from the target URI.
      #
      # @see https://tools.ietf.org/html/draft-kelly-json-hal-08#section-4.1.2
      #
      def _embedded
        @_embedded ||= Embedded.new(@data['_embedded'])
      end

      def [](key)
        _properties[key]
      end

      def method_missing(method_name, *_)
        _properties.send(method_name)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
excon-hypermedia-0.5.1 lib/excon/hypermedia/resource_object.rb
excon-hypermedia-0.5.0 lib/excon/hypermedia/resource_object.rb