lib/spyke/path.rb in spyke-1.6.0 vs lib/spyke/path.rb in spyke-1.7.0
- old
+ new
@@ -1,12 +1,14 @@
require 'uri_template'
module Spyke
+ class InvalidPathError < StandardError; end
class Path
- def initialize(uri_template, params = {})
- @uri_template = URITemplate.new(:colon, uri_template)
- @params = params
+
+ def initialize(pattern, params = {})
+ @pattern = pattern
+ @params = params.symbolize_keys
end
def join(other_path)
self.class.new File.join(path, other_path.to_s), @params
end
@@ -14,15 +16,38 @@
def to_s
path
end
def variables
- @variables ||= @uri_template.variables.map(&:to_sym)
+ @variables ||= uri_template.variables.map(&:to_sym)
end
private
+ def uri_template
+ @uri_template ||= URITemplate.new(:colon, pattern_with_rfc_style_parens)
+ end
+
+ def pattern_with_rfc_style_parens
+ @pattern.gsub('(', '{').gsub(')', '}')
+ end
+
def path
- @uri_template.expand(@params).chomp('/')
+ validate_required_params!
+ uri_template.expand(@params).chomp('/')
+ end
+
+ def validate_required_params!
+ if missing_required_params.any?
+ raise Spyke::InvalidPathError, "Missing required params: #{missing_required_params.join(', ')} in #{@pattern}. Mark optional params with parens eg: (:param)"
+ end
+ end
+
+ def missing_required_params
+ required_params - @params.keys
+ end
+
+ def required_params
+ @pattern.scan(/\/:(\w+)/).flatten.map(&:to_sym)
end
end
end