class Spyke::Path

Public Class Methods

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

Public Instance Methods

join(other_path) click to toggle source
# File lib/spyke/path.rb, line 12
def join(other_path)
  self.class.new File.join(path, other_path.to_s), @params
end
to_s() click to toggle source
# File lib/spyke/path.rb, line 16
def to_s
  path
end
variables() click to toggle source
# File lib/spyke/path.rb, line 20
def variables
  @variables ||= uri_template.variables.map(&:to_sym)
end

Private Instance Methods

missing_required_variables() click to toggle source
# File lib/spyke/path.rb, line 45
def missing_required_variables
  required_variables - variables_with_values
end
optional_variables() click to toggle source
# File lib/spyke/path.rb, line 59
def optional_variables
  @pattern.scan(/\(\/?:(\w+)\)/).flatten.map(&:to_sym)
end
path() click to toggle source
# File lib/spyke/path.rb, line 34
def path
  validate_required_variables!
  uri_template.expand(@params).to_s.chomp('/')
end
pattern_with_rfc_style_parens() click to toggle source
# File lib/spyke/path.rb, line 30
def pattern_with_rfc_style_parens
  RfcConverter.new(@pattern).convert
end
required_variables() click to toggle source
# File lib/spyke/path.rb, line 55
def required_variables
  variables - optional_variables
end
uri_template() click to toggle source
# File lib/spyke/path.rb, line 26
def uri_template
  @uri_template ||= Addressable::Template.new(pattern_with_rfc_style_parens)
end
validate_required_variables!() click to toggle source
# File lib/spyke/path.rb, line 39
def validate_required_variables!
  if missing_required_variables.any?
    raise Spyke::InvalidPathError, "Missing required variables: #{missing_required_variables.join(', ')} in #{@pattern}. Mark optional variables with parens eg: (:param)"
  end
end
variables_with_values() click to toggle source
# File lib/spyke/path.rb, line 49
def variables_with_values
  @params.map do |key, value|
    key if value.present?
  end.compact
end