Sha256: a30cec36c171b07c1a7cf17bc63534dc448dfb89f8a41b4769fe373c472855ec

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

module Rainforest
  module PathBuilder

    # 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.
    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

    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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rainforest-2.1.0 lib/rainforest/apibits/path_builder.rb
rainforest-2.0.2 lib/rainforest/apibits/path_builder.rb
rainforest-2.0.1 lib/rainforest/apibits/path_builder.rb
rainforest-2.0.0 lib/rainforest/apibits/path_builder.rb