module Rainforest::PathBuilder
Public Class Methods
build(path, object, params)
click to toggle source
Take a path like:
":path/:id/dogs/:dog_id"
and convert it to:
"#{object.path}/#{object.id}/dogs/#{params[:id]}" => "/objects/1/dogs/2"
Path priority is:
1. Object - this will be a class or an instance of a class. 2. Params - this is a hash of key values. All keys *must* be symbolized.
# File lib/rainforest/apibits/path_builder.rb, line 12 def self.build(path, object, params) ret = path.dup if ret.include?(":") matches = ret.scan(/:([^\/]*)/).flatten.map(&:to_sym) missing = Set.new(matches) matches.each do |match| value = determine_value(match, object, params) missing.delete(match) unless value.nil? ret.sub!(match.inspect, "#{value}") end if missing.any? raise ArgumentError.new("Could not determine the full URL. The following values of the path are missing: #{missing.to_a.join(', ')}. Try setting them in your params.") end end ret end
determine_value(match, object, params)
click to toggle source
# File lib/rainforest/apibits/path_builder.rb, line 31 def self.determine_value(match, object, params) value = object.send(match) if object && object.respond_to?(match) value ||= params[match] if params && params.has_key?(match) value end