Sha256: f311106675cec6be65df9b7279bfca403ef6b1aec0f291912f417c61745d582a

Contents?: true

Size: 1.21 KB

Versions: 20

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'uri'

module ChartMogul
  class ResourcePath
    attr_reader :path
    attr_reader :named_params

    class RequiredParameterMissing < StandardError; end

    def initialize(path)
      @path = path
      @named_params = path.scan(/:\w+/).each_with_object({}) do |named_param, hash|
        hash[named_param] = named_param.delete(':').to_sym
      end
    end

    def apply(params = {})
      apply_named_params(params)
    end

    # For path = '/hello/:hello_id/say' & params = { hello_id: 1, search: 'cat' }
    # it will return '/hello/1/say?search=cat'

    def apply_with_get_params(params = {})
      base_path = apply_named_params(params)
      get_params = params.reject { |param_name| named_params.values.include?(param_name) }

      get_params.empty? ? base_path : "#{base_path}?#{URI.encode_www_form(get_params)}"
    end

    private

    def apply_named_params(params)
      path.dup.tap do |path|
        named_params.each do |named_param, param_key|
          unless params.key?(param_key)
            raise(RequiredParameterMissing, "#{named_param} is required")
          end

          path.gsub!(named_param, params[param_key].to_s)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
chartmogul-ruby-3.3.1 lib/chartmogul/resource_path.rb
chartmogul-ruby-3.3.0 lib/chartmogul/resource_path.rb
chartmogul-ruby-3.2.0 lib/chartmogul/resource_path.rb
chartmogul-ruby-3.1.0 lib/chartmogul/resource_path.rb
chartmogul-ruby-3.0.2 lib/chartmogul/resource_path.rb
chartmogul-ruby-3.0.0 lib/chartmogul/resource_path.rb
chartmogul-ruby-2.9.0 lib/chartmogul/resource_path.rb
chartmogul-ruby-2.1.0 lib/chartmogul/resource_path.rb
chartmogul-ruby-2.0.0 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.7.3 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.7.2 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.7.1 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.6.9 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.6.8 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.6.7 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.6.6 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.6.4 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.6.3 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.6.2 lib/chartmogul/resource_path.rb
chartmogul-ruby-1.6.1 lib/chartmogul/resource_path.rb