Sha256: 57f1264f9db4a2e9082c1d6ea1458f5d77a3644d11e23047eedeabd4c3b4d62c

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

require 'leadlight/link'
require 'addressable/template'

module Leadlight
  class LinkTemplate < Link

    def href_template
      @href_template ||= Addressable::Template.new(href.to_s)
    end

    HTTP_METHODS.each do |name|
      define_method(name) do |*args, &block|
        expanded_href = expand(args)
        service.public_send(name, expanded_href, *args, &block)
      end
    end

    def expand(args)
      mapping = args.last.is_a?(Hash) ? args.pop : {}
      mapping = mapping.inject({}) { |result, (k,v)| result.merge!(k.to_s => v) }
      mapping = href_template.variables.inject(mapping) do |mapping, var|
        mapping.merge!(var => args.shift) unless args.empty?
        mapping
      end
      extra_keys = (mapping.keys.map(&:to_s) - href_template.variables)
      extra_params = extra_keys.inject({}) do |params, key|
        params[key] = mapping.delete(key)
        params
      end
      assert_all_variables_mapped(href_template, mapping)
      args.push extra_params unless extra_params.empty?
      href_template.expand(mapping).to_s
    end

    private

    def assert_all_variables_mapped(template, mapping)
      supplied_keys = mapping.keys.map(&:to_s)
      needed_keys   = template.variables
      missing_keys  = needed_keys - supplied_keys
      if !missing_keys.empty?
        raise ArgumentError,
              "Missing URI template parameters: #{missing_keys.inspect}"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
leadlight-0.0.5 lib/leadlight/link_template.rb
leadlight-0.0.4 lib/leadlight/link_template.rb
leadlight-0.0.3 lib/leadlight/link_template.rb
leadlight-0.0.2 lib/leadlight/link_template.rb