Class: ChartMogul::ResourcePath

Inherits:
Object
  • Object
show all
Defined in:
lib/chartmogul/resource_path.rb

Defined Under Namespace

Classes: RequiredParameterMissing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ResourcePath

Returns a new instance of ResourcePath.



10
11
12
13
14
15
# File 'lib/chartmogul/resource_path.rb', line 10

def initialize(path)
  @path = path
  @named_params = path.scan(/:\w+/).each_with_object({}) do |named_param, hash|
    hash[named_param] = named_param.gsub(':', '').to_sym
  end
end

Instance Attribute Details

#named_paramsObject (readonly)

Returns the value of attribute named_params.



6
7
8
# File 'lib/chartmogul/resource_path.rb', line 6

def named_params
  @named_params
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/chartmogul/resource_path.rb', line 5

def path
  @path
end

Instance Method Details

#apply(params = {}) ⇒ Object



17
18
19
# File 'lib/chartmogul/resource_path.rb', line 17

def apply(params = {})
  apply_named_params(params)
end

#apply_with_get_params(params = {}) ⇒ Object

For path = ‘/hello/:hello_id/say’ & params = { hello_id: 1, search: ‘cat’ } it will return ‘/hello/1/say?search=cat’



24
25
26
27
28
29
# File 'lib/chartmogul/resource_path.rb', line 24

def apply_with_get_params(params = {})
  base_path = apply_named_params(params)
  get_params = params.reject { |param_name| named_params.values.include?(param_name) }

  get_params.empty? ? base_path : "#{base_path}?#{URI.encode_www_form(get_params)}"
end