Sha256: 6b8709da42fc8e05b87b6ed94075841450cde361d5ac13dc9bf0e1cdd6a1a905

Contents?: true

Size: 978 Bytes

Versions: 2

Compression:

Stored size: 978 Bytes

Contents

# frozen_string_literal: true

require 'json'

module Excon
  module HyperMedia
    # Resource
    #
    # This HyperMedia::Resource object encapsulates the returned JSON and
    # makes it easy to access the links and attributes.
    #
    class Resource
      attr_reader :data

      def initialize(body)
        @body = body
      end

      def links
        data.fetch('_links', {}).keys.map { |name| link(name) }
      end

      def link(link_name)
        Link.new(name: link_name, hash: data)
      end

      def attributes
        attributes = data.reject do |k, _|
          k == '_links'
        end

        Struct.new(*attributes.keys.map(&:to_sym)).new(*attributes.values)
      end

      def type?(name)
        return :link if link(name).valid?
        return :attribute if attributes.respond_to?(name.to_s)

        :unknown
      end

      def data
        @data ||= JSON.parse(@body)
      rescue JSON::ParserError
        {}
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
excon-hypermedia-0.3.0 lib/excon/hypermedia/resource.rb
excon-hypermedia-0.2.0 lib/excon/hypermedia/resource.rb