cvut/kosapi_client.rb

View on GitHub
lib/kosapi_client/entity/link.rb

Summary

Maintainability
A
0 mins
Test Coverage
module KOSapiClient
  module Entity
    class Link

      attr_reader :link_title, :link_href, :link_rel

      def initialize(title, href, rel, client = nil)
        @link_title = title
        @link_href = escape_url(href)
        @link_rel = rel
        @client = client
      end

      def self.parse(contents)
        href = contents[:xlink_href] || contents[:href]
        new(contents[:__content__], href, contents[:rel])
      end

      def link_id
        @link_href.split('/').last
      end

      def follow
        raise "HTTP client not set, cannot send request to #{link_href}" unless @client
        @client.send_request(:get, link_href)
      end

      def inject_client(client)
        @client = client
      end

      def target
        @target ||= follow
      end

      def to_hash
        { href: link_href, rel: link_rel, title: link_title }
      end

      if RUBY_VERSION >= '3.0'
        def method_missing(method, *args, **kwargs, &block)
          target.send(method, *args, **kwargs, &block)
        end
      else
        def method_missing(method, *args, &block)
          target.send(method, *args, &block)
        end
      end

      def respond_to_missing?(method_name, include_private = false)
        target.respond_to?(method_name, include_private)
      end

      def escape_url(url)
        url.gsub(';','%3B')
      end

    end
  end
end