Module: BlockScore::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/blockscore/util.rb

Constant Summary collapse

PLURAL_LOOKUP =
{
  'candidate' => 'candidates',
  'company' => 'companies',
  'person' => 'people',
  'question_set' => 'question_sets',
  'watchlist_hit' => 'watchlist_hits'
}

Instance Method Summary collapse

Instance Method Details

#create_array(resource, arr) ⇒ Object



27
28
29
# File 'lib/blockscore/util.rb', line 27

def create_array(resource, arr)
  arr.map { |obj| create_object(resource, obj) }
end

#create_object(resource, options = {}) ⇒ Object



23
24
25
# File 'lib/blockscore/util.rb', line 23

def create_object(resource, options = {})
  to_constant("BlockScore::#{to_camelcase(resource)}").new(options)
end

#parse_json(json_obj) ⇒ Object



17
18
19
20
21
# File 'lib/blockscore/util.rb', line 17

def parse_json(json_obj)
  parse_json! json_obj
rescue JSON::ParserError
  raise Error, 'An error has occurred. If this problem persists, please message [email protected].'
end

#parse_json!(json_obj) ⇒ Object



13
14
15
# File 'lib/blockscore/util.rb', line 13

def parse_json!(json_obj)
  JSON.parse(json_obj, symbolize_names: true)
end

#to_camelcase(str) ⇒ Object



67
68
69
# File 'lib/blockscore/util.rb', line 67

def to_camelcase(str)
  str.split('_').map(&:capitalize).join('')
end

#to_constant(camel_cased_word) ⇒ Object

Taken from activesupport: git.io/vkWtR



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/blockscore/util.rb', line 36

def to_constant(camel_cased_word)
  names = camel_cased_word.split('::')

  # Trigger a built-in NameError exception including the ill-formed constant in the message.
  Object.const_get(camel_cased_word) if names.empty?

  # Remove the first blank element in case of '::ClassName' notation.
  names.shift if names.size > 1 && names.first.empty?

  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
      next candidate unless Object.const_defined?(name)

      # Go down the ancestors to check if it is owned directly. The check
      # stops when we reach Object or the end of ancestors tree.
      constant = constant.ancestors.inject do |const, ancestor|
        break const    if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)
        const
      end

      # owner is in Object, so raise
      constant.const_get(name, false)
    end
  end
end

#to_plural(str) ⇒ Object



31
32
33
# File 'lib/blockscore/util.rb', line 31

def to_plural(str)
  PLURAL_LOOKUP[str]
end

#to_underscore(str) ⇒ Object

Taken from Rulers: git.io/vkWqf



72
73
74
75
76
77
78
# File 'lib/blockscore/util.rb', line 72

def to_underscore(str)
  str.gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
end