Sha256: 639cdf4133cb3197c0870931c7bdf1db0dcfd4fd6ee1828ece0485945f9f2478

Contents?: true

Size: 1.91 KB

Versions: 9

Compression:

Stored size: 1.91 KB

Contents

require 'uri_template'

module BooticClient
  class WhinyURI
    attr_reader :variables

    def initialize(href, complain_on_undeclared_params = true)
      @href = href
      @uri = URITemplate.new(href)
      @variables = @uri.variables
      @complain_on_undeclared_params = complain_on_undeclared_params
    end

    def expand(attrs = {})
      attrs = stringify(attrs)

      missing = missing_path_variables(attrs)
      if missing.any?
        raise InvalidURLError, missing_err(missing)
      end

      undeclared = undeclared_params(attrs)
      if complain_on_undeclared_params
        if undeclared.any?
          raise InvalidURLError, undeclared_err(undeclared)
        end
      end

      uri.expand whitelisted(attrs)
    end

    private
    attr_reader :uri, :href, :complain_on_undeclared_params

    def path_variables
      @path_variables ||= (
        variables.find_all{ |v|
          Regexp.new("(\/\{#{v}\})|(\{\/#{v}\})") =~ href
        }
      )
    end

    def whitelisted(attrs = {})
      variables.each_with_object({}) do |key, hash|
        hash[key] = attrs[key] if attrs.key?(key)
      end
    end

    def missing_path_variables(attrs)
      path_variables - attrs.keys
    end

    def declared_params
      @declared_params ||= variables - path_variables
    end

    def undeclared_params(attrs)
      attrs.keys - variables
    end

    def undeclared_err(undeclared)
      msg = ["undeclared URI variables: #{format_vars(undeclared)}"]
      query_vars = variables - path_variables
      msg << "Allowed query variables are #{format_vars(query_vars)}" if query_vars.any?
      msg.join('. ')
    end

    def missing_err(missing)
      "missing required path variables: #{format_vars(missing)}"
    end

    def format_vars(vars)
      vars.map{|v| "`#{v}`"}.join(', ')
    end

    def stringify(attrs)
      attrs.each_with_object({}) do |(k, v), hash|
        hash[k.to_s] = v
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bootic_client-0.0.28 lib/bootic_client/whiny_uri.rb
bootic_client-0.0.27 lib/bootic_client/whiny_uri.rb
bootic_client-0.0.26 lib/bootic_client/whiny_uri.rb
bootic_client-0.0.25 lib/bootic_client/whiny_uri.rb
bootic_client-0.0.24 lib/bootic_client/whiny_uri.rb
bootic_client-0.0.23 lib/bootic_client/whiny_uri.rb
bootic_client-0.0.22 lib/bootic_client/whiny_uri.rb
bootic_client-0.0.21 lib/bootic_client/whiny_uri.rb
bootic_client-0.0.20 lib/bootic_client/whiny_uri.rb