Module: SynapsePay::PathBuilder

Defined in:
lib/synapse_pay/apibits/path_builder.rb

Class Method Summary collapse

Class Method Details

.build(path, object, params) ⇒ Object

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.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/synapse_pay/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) ⇒ Object



31
32
33
34
35
# File 'lib/synapse_pay/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