Sha256: 65df78c571714a4d30f25956305ee7c7bedfde125220bc3512b90dd48706d59f

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

require 'active_support/concern'
require 'addressable/template'

class Frenetic
  module HalLinked
    extend ActiveSupport::Concern

    def links
      @params['_links']
    end

    def member_url( params = {} )
      resource = @resource_type || self.class.to_s.demodulize.underscore

      link = links[resource] || links['self'] or raise HypermediaError, %Q{No Hypermedia GET Url found for the resource "#{resource}"}

      self.class.parse_link link, params
    end

    module ClassMethods
      def links
        api.description['_links']
      end

      def member_url( params = {} )
        link = links[namespace] or raise HypermediaError, %Q{No Hypermedia GET Url found for the resource "#{namespace}"}

        parse_link link, params
      end

      def parse_link( link, params = {} )
        if link['templated']
          expand_link link, params
        else
          link['href']
        end
      end

    private

      def expand_link( link, params )
        tmpl = Addressable::Template.new link['href']

        if params && !params.is_a?(Hash)
          params = infer_url_template_values tmpl, params
        end

        tmpl.expand( params ).to_s
      end

      def infer_url_template_values( tmpl, params )
        key = tmpl.variables.first

        { key => params }
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
frenetic-0.0.20.alpha.3 lib/frenetic/concerns/hal_linked.rb
frenetic-0.0.20.alpha.2 lib/frenetic/concerns/hal_linked.rb
frenetic-0.0.20.alpha.1 lib/frenetic/concerns/hal_linked.rb
frenetic-0.0.20.alpha.0 lib/frenetic/concerns/hal_linked.rb