Sha256: 872cf4354b3b62eeb6058ea8490850ba20eba38c36a0145571e279ea145e6ff1

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module TMS::Util
  module HalLinkParser

    def parse_links(_links)
      @resources = {}
      return if _links.nil?
      parse_link(_links) and return if _links.is_a?(Hash)
      _links.each { |link| parse_link(link) }
    end

    def subresources
      @resources
    end

    protected

    def metaclass
      @metaclass ||= class << self;
        self;
      end
    end

    def parse_link(link)
      link.each do |rel, href|
        if rel == 'self'
          self.href = href
        else
          klass = relation_class(rel)
          klass = self.class if ['first', 'prev', 'next', 'last'].include?(rel)
          if klass
            subresources[rel] = klass.new(self.client, href)
            setup_subresource(link)
          else
            puts "Don't know what to do with link rel '#{rel}' for class #{self.class.to_s}!"
          end

        end
      end
    end

    def relation_class(rel)
      ::TMS.const_get(classify(rel)) rescue nil
    end

    def setup_subresource(link)
      return unless link
      link.each { |rel, href| self.metaclass.send(:define_method, rel.to_sym, &lambda { subresources[rel] }) unless rel == 'self' }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tms_client-0.5.2 lib/tms_client/util/hal_link_parser.rb