Module: Glassfrog::Utils

Included in:
Base, Client, REST::Request
Defined in:
lib/glassfrog/utils/utils.rb

Overview

Utilites for handling requests.

Instance Method Summary collapse

Instance Method Details

#extract_id(object, klass) ⇒ Integer?

Grabs ID out of an object.

Parameters:

  • object (Integer, String, URI, Glassfrog::Base)

    The object to fetch ID from.

  • klass (Class)

    The expected class.

Returns:

  • (Integer, nil)

    ID or nil.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/glassfrog/utils/utils.rb', line 39

def extract_id(object, klass)
  case object
  when ::Integer
    object
  when ::String
    object.split('/').last.to_i
  when ::Hash
    object[:id] || object[:ID]
  when URI, Addressable::URI
    object.path.split('/').last.to_i
  when klass
    object.id
  else
    nil
  end
end

#parameterize(value) ⇒ Symbol

Turns a string into a lowercase, underscored symbol for use as a parameter.

Parameters:

  • value (String)

    The string.

Returns:

  • (Symbol)

    The symbol.



11
12
13
# File 'lib/glassfrog/utils/utils.rb', line 11

def parameterize(value)
  value.to_s.downcase.tr(" ", "_").to_sym
end

#symbolize_keys(object) ⇒ Hash, ...

Turns all the values into symbols in an array or all the keys in a hash.

Parameters:

  • object (Hash, Array, Object)

    Object to be symbolized.

Returns:

  • (Hash, Array, Object)

    Symbolized object.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/glassfrog/utils/utils.rb', line 20

def symbolize_keys(object)
  if object.is_a?(Array)
    object.each_with_index do |val, index|
      object[index] = symbolize_keys(val)
    end
  elsif object.is_a?(Hash)
    object.keys.each do |key|
      object[key.to_sym] = symbolize_keys(object.delete(key))
    end
  end
  object
end