Module: Ribbon::Intercom::Utils

Defined in:
lib/ribbon/intercom/utils.rb,
lib/ribbon/intercom/utils/signer.rb,
lib/ribbon/intercom/utils/method_chain.rb

Defined Under Namespace

Modules: Mixins Classes: MethodChain, Signer

Constant Summary collapse

BASIC_TYPES =
[
  String, Symbol, TrueClass, FalseClass, Integer, Float, NilClass,
  Date, Time, DateTime,
  Hash, Array, Range
].freeze

Class Method Summary collapse

Class Method Details

.basic_type?(object) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/ribbon/intercom/utils.rb', line 16

def basic_type?(object)
  case object
  when *BASIC_TYPES then true
  else false
  end
end

.classify(str) ⇒ Object



60
61
62
# File 'lib/ribbon/intercom/utils.rb', line 60

def classify(str)
  str.split("_").map(&:capitalize).join
end

.method_identifier(subject, method) ⇒ Object

Returns an identifier for the method (e.g., A::B::C#method_name)



66
67
68
69
# File 'lib/ribbon/intercom/utils.rb', line 66

def method_identifier(subject, method)
  scope = subject.is_a?(Class) ? subject.name : subject.class.name
  scope + (subject.is_a?(Class) ? '.' : '#') + method.to_s
end

.sanitize(object) ⇒ Object

Raises an error if the object is or contains any non-basic types.



46
47
48
49
50
51
52
53
54
# File 'lib/ribbon/intercom/utils.rb', line 46

def sanitize(object)
  walk(object) { |object|
    if basic_type?(object)
      object
    else
      raise Errors::UnsafeValueError, object.inspect
    end
  }
end

.symbolize_keys(hash) ⇒ Object



56
57
58
# File 'lib/ribbon/intercom/utils.rb', line 56

def symbolize_keys(hash)
  hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end

.walk(object, context = nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ribbon/intercom/utils.rb', line 23

def walk(object, context=nil, &block)
  case object
  when Hash
    Hash[
      object.map { |key, val|
        [walk(key, :hash_key, &block), walk(val, :hash_value, &block)]
      }
    ]
  when Array
    object.map { |obj| walk(obj, :array_elem, &block) }
  when Range
    Range.new(
      walk(object.begin, :range_begin, &block),
      walk(object.end, :range_end, &block),
      object.exclude_end?
    )
  else
    yield object, context
  end
end