Sha256: 328a72ac6c52b9a3fa372c01d421c88e9b1c44c9c8450cf87892bf49bae641bb

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 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 collection_url
        link = links[namespace.pluralize] or raise HypermediaError, %Q{No Hypermedia GET Url found for the resource "#{namespace.pluralize}"}

        link['href']
      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

3 entries across 3 versions & 1 rubygems

Version Path
frenetic-0.0.20.alpha.6 lib/frenetic/concerns/hal_linked.rb
frenetic-0.0.20.alpha.5 lib/frenetic/concerns/hal_linked.rb
frenetic-0.0.20.alpha.4 lib/frenetic/concerns/hal_linked.rb