Module: CentrumFaktur::Utils

Defined in:
lib/centrum_faktur/utils.rb

Class Method Summary collapse

Class Method Details

.inline_params(params) ⇒ Object



4
5
6
# File 'lib/centrum_faktur/utils.rb', line 4

def inline_params(params)
  params.map { |k, v| "#{k}=#{v}" }.join("&")
end

.normalize_params(params) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/centrum_faktur/utils.rb', line 13

def normalize_params(params)
  raise ArgumentError.new("Only hash params can be normalized") unless params.kind_of?(Hash)
  params.inject({}) do |normalized, (key, value)|
    normalized[key] =
      if value.kind_of?(Hash)
        normalize_params(value)
      elsif value.kind_of?(Array)
        value.map { |v| v.kind_of?(Hash) ? normalize_params(v) : normalize_value(v) }
      else
        normalize_value(value)
      end
    normalized
  end
end

.normalize_value(value) ⇒ Object



28
29
30
# File 'lib/centrum_faktur/utils.rb', line 28

def normalize_value(value)
  value.respond_to?(:strftime) ? value.strftime("%Y-%m-%d") : value
end

.path_with_params(path, params) ⇒ Object



8
9
10
11
# File 'lib/centrum_faktur/utils.rb', line 8

def path_with_params(path, params)
  path = path + "?" + inline_params(params) unless params.empty?
  URI.parse(path).to_s
end