Module: Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/supplejack_api/utils.rb

Overview

The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government, and is licensed under the GNU General Public License, version 3. One component is a third party component. See github.com/DigitalNZ/supplejack_api for details.

Supplejack was created by DigitalNZ at the National Library of NZ and the Department of Internal Affairs. digitalnz.org/supplejack

Instance Method Summary collapse

Instance Method Details

#array(object) ⇒ Object

Return a array no matter what.


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/supplejack_api/utils.rb', line 42

def array(object)
  case object
  when Array
    object
  when String
    object.present? ? [object] : []
  when NilClass
    []
  else
    [object]
  end
end

#call_block(dsl, &block) ⇒ Object


23
24
25
# File 'lib/supplejack_api/utils.rb', line 23

def call_block(dsl, &block)
  Sunspot::Util.instance_eval_or_call(dsl, &block)
end

#capitalize_first_word(string) ⇒ Object


69
70
71
72
73
74
# File 'lib/supplejack_api/utils.rb', line 69

def capitalize_first_word(string)
  return '' unless string.present?
  first_letter = string.to_s[0].upcase
  string[0] = first_letter
  string
end

#load_yaml(file_name) ⇒ Object


12
13
14
15
16
17
18
19
20
21
# File 'lib/supplejack_api/utils.rb', line 12

def load_yaml(file_name)
  path = File.join(Rails.root, 'config', file_name)
  if File.exist?(path)
    File.open(path) do |file|
      YAML.load(file.read)
    end
  else
    {}
  end
end

#safe?(text, version = 3) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/supplejack_api/utils.rb', line 27

def safe?(text, version = 3)
  !unsafe?(text, version)
end

#time(value) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/supplejack_api/utils.rb', line 55

def time(value)
  if value.blank?
    return nil
  elsif value.is_a?(Integer)
    number = value.to_s[0..9]
    Time.at(number.to_i)
  elsif value.is_a?(String) && value.match(/^\d{13}$/)
    number = value[0..9]
    Time.at(number.to_i)
  else
    Time.parse(value) rescue nil
  end
end

#unsafe?(text, version = 3) ⇒ Boolean

Returns:

  • (Boolean)

31
32
33
34
35
36
37
# File 'lib/supplejack_api/utils.rb', line 31

def unsafe?(text, version = 3)
  if version == 3
    text.to_s.match(/([\w]{3,40}:)/)
  else
    text.to_s.match(/([\w]{3,40}:)|(AND|OR|NOT)/)
  end
end