Sha256: f87d51d2fff9afcd41aa4ee01200e2055bf13c1633592392b4ffd6647f435e1e

Contents?: true

Size: 739 Bytes

Versions: 2

Compression:

Stored size: 739 Bytes

Contents

require 'rest_in_peace/errors'

module RESTinPeace
  class TemplateSanitizer

    class IncompleteParams < RESTinPeace::DefaultError; end

    def initialize(url_template, params)
      @url_template = url_template
      @params = params.dup
    end

    def url
      return @url if @url
      @url = @url_template.dup
      tokens.each do |token|
        param = @params.delete(token.to_sym)
        raise IncompleteParams, "Unknown parameter for token :#{token} found" unless param
        @url.sub!(%r{:#{token}}, param.to_s)
      end
      @url
    end

    def tokens
      @url_template.scan(%r{:([a-z_]+)}).flatten
    end

    def leftover_params
      @params.delete_if { |param| tokens.include?(param.to_s) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rest-in-peace-2.0.1 lib/rest_in_peace/template_sanitizer.rb
rest-in-peace-2.0.0 lib/rest_in_peace/template_sanitizer.rb