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.



12
13
14
15
16
17
# File 'lib/chartmogul/resource_path.rb', line 12

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

Instance Attribute Details

#named_paramsObject (readonly)

Returns the value of attribute named_params.



8
9
10
# File 'lib/chartmogul/resource_path.rb', line 8

def named_params
  @named_params
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#apply(params = {}) ⇒ Object



19
20
21
# File 'lib/chartmogul/resource_path.rb', line 19

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’



26
27
28
29
30
31
# File 'lib/chartmogul/resource_path.rb', line 26

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