Module: Rester::Utils

Defined in:
lib/rester/utils.rb

Class Method Summary collapse

Class Method Details

.classify(str) ⇒ Object



49
50
51
# File 'lib/rester/utils.rb', line 49

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

.extract_method_verb(meth) ⇒ Object

Determines the HTTP method/verb based on the method name. Defaults to GET but if the method ends with “!” it uses POST.



9
10
11
12
13
14
15
16
17
# File 'lib/rester/utils.rb', line 9

def extract_method_verb(meth)
  meth = meth.to_s

  if meth[-1] == '!'
    [:post, meth[0..-2]]
  else
    [:get, meth]
  end
end

.join_paths(*paths) ⇒ Object



19
20
21
22
# File 'lib/rester/utils.rb', line 19

def join_paths(*paths)
  paths.map(&:to_s).reject { |p| p.nil? || p.empty? }
    .join('/').gsub(/\/+/, '/')
end

.symbolize_keys(hash) ⇒ Object



45
46
47
# File 'lib/rester/utils.rb', line 45

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

.underscore(str) ⇒ Object



53
54
55
# File 'lib/rester/utils.rb', line 53

def underscore(str)
  str.scan(/[A-Z][a-z]*/).map(&:downcase).join('_')
end

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



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

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