Module: Resteze::Util::ClassMethods

Included in:
Resteze::Util
Defined in:
lib/resteze/util.rb

Instance Method Summary collapse

Instance Method Details

#capitalize_parts(str) ⇒ Object



47
48
49
# File 'lib/resteze/util.rb', line 47

def capitalize_parts(str)
  str.to_s.dasherize.split("-").reject(&:blank?).map(&:capitalize).join("-")
end

#convert_to_object(data, object_class) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/resteze/util.rb', line 6

def convert_to_object(data, object_class)
  case data
  when Array
    data.map { |i| convert_to_object(i, object_class) }
  when Hash
    object_class.construct_from(object_class.object_key.present? ? { object_class.object_key => data } : data)
  else
    data
  end
end

#normalize_headers(headers) ⇒ Object



17
18
19
# File 'lib/resteze/util.rb', line 17

def normalize_headers(headers)
  headers.transform_keys { |key| capitalize_parts(key) }
end

#normalize_id(id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/resteze/util.rb', line 21

def normalize_id(id)
  if id.is_a?(Hash)
    params_hash = id.dup
    id = params_hash.delete(:id)
  else
    params_hash = {}
  end

  [id.to_s.presence, params_hash]
end

#objects_to_ids(obj) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/resteze/util.rb', line 32

def objects_to_ids(obj)
  case obj
  when ApiResource
    obj.id
  when Hash
    res = {}
    obj.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
    res
  when Array
    obj.map { |v| objects_to_ids(v) }
  else
    obj
  end
end