class ResourceKit::EndpointResolver

Attributes

path[R]
query_param_keys[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/resource_kit/endpoint_resolver.rb, line 7
def initialize(options = {})
  @path = options[:path]
  @query_param_keys = options[:query_param_keys] || []
end

Public Instance Methods

resolve(values = {}) click to toggle source
# File lib/resource_kit/endpoint_resolver.rb, line 12
def resolve(values = {})
  uri = Addressable::URI.parse(path)
  new_path = generated_path(uri.path, values)

  uri.path = normalized_path_components(new_path)
  uri.query = append_query_values(uri, values) unless query_param_keys.empty?

  uri.to_s
end

Private Instance Methods

append_query_values(uri, values) click to toggle source
# File lib/resource_kit/endpoint_resolver.rb, line 37
def append_query_values(uri, values)
  pre_vals = uri.query_values || {}
  params = query_param_keys.each_with_object(pre_vals) do |key, query_values|
    query_values[key] = values[key] if values.has_key?(key)
  end

  URI.encode_www_form(params)
end
generated_path(supplied_path, values) click to toggle source
# File lib/resource_kit/endpoint_resolver.rb, line 24
def generated_path(supplied_path, values)
  values.inject(supplied_path) do |np, (key, value)|
    np.gsub(":#{key}", value.to_s)
  end
end
normalized_path_components(*components) click to toggle source
# File lib/resource_kit/endpoint_resolver.rb, line 30
def normalized_path_components(*components)
  components.reject(&:empty?).map do |piece|
    # Remove leading and trailing forward slashes
    piece.gsub(/(^\/)|(\/$)/, '')
  end.join('/').insert(0, '/')
end