class PDC::Resource::Path

Public Class Methods

new(pattern, params = {}) click to toggle source
# File lib/pdc/resource/path.rb, line 5
def initialize(pattern, params = {})
  @pattern = pattern
  @params = params.symbolize_keys
end

Public Instance Methods

expanded() click to toggle source
# File lib/pdc/resource/path.rb, line 18
def expanded
  path.to_s
end
join(other_path) click to toggle source
# File lib/pdc/resource/path.rb, line 10
def join(other_path)
  self.class.new File.join(path, other_path.to_s), @params
end
to_s() click to toggle source
# File lib/pdc/resource/path.rb, line 14
def to_s
  @pattern
end
variables() click to toggle source
# File lib/pdc/resource/path.rb, line 22
def variables
  @variables ||= uri_template.variables.map(&:to_sym)
end

Private Instance Methods

missing_required_params() click to toggle source
# File lib/pdc/resource/path.rb, line 47
def missing_required_params
  required_params - params_with_values
end
params_with_values() click to toggle source
# File lib/pdc/resource/path.rb, line 51
def params_with_values
  @params.map do |key, value|
    key if value.present?
  end.compact
end
path() click to toggle source
# File lib/pdc/resource/path.rb, line 28
def path
  validate_required_params!
  uri_template.expand(@params).chomp('/')
end
pattern_with_rfc_style_parens() click to toggle source
# File lib/pdc/resource/path.rb, line 37
def pattern_with_rfc_style_parens
  @pattern.tr('(', '{').tr(')', '}')
end
required_params() click to toggle source
# File lib/pdc/resource/path.rb, line 57
def required_params
  @pattern.scan(%r{/:(\w+)}).flatten.map(&:to_sym)
end
uri_template() click to toggle source
# File lib/pdc/resource/path.rb, line 33
def uri_template
  @uri_template ||= URITemplate.new(:colon, pattern_with_rfc_style_parens)
end
validate_required_params!() click to toggle source
# File lib/pdc/resource/path.rb, line 41
def validate_required_params!
  return unless missing_required_params.any?
  missing_params = missing_required_params.join(', ')
  raise PDC::InvalidPathError, "Missing required params: #{missing_params} in #{@pattern}"
end