Class: Tml::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/tml/utils.rb

Class Method Summary collapse

Class Method Details

.browser_accepted_locales(header) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tml/utils.rb', line 128

def self.browser_accepted_locales(header)
  header.split(/\s*,\s*/).collect do |l|
    l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
    l.split(';q=')
  end.sort do |x,y|
    unless x.first =~ /^[a-z\-]+$/i
      raise Tml::Exception.new('Not correctly formatted')
    end
    y.last.to_f <=> x.last.to_f
  end.collect do |l|
    l.first.downcase.gsub(/-[a-z]+$/i) { |x| x.upcase }
  end
rescue
  []
end


70
71
72
# File 'lib/tml/utils.rb', line 70

def self.cookie_name(app_key)
  "trex_#{app_key}"
end

.decode(payload, secret = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/tml/utils.rb', line 98

def self.decode(payload, secret = nil)
  # payload = URI::decode(payload)
  payload = Base64.decode64(payload)
  data = JSON.parse(payload)
  # TODO: Verify signature
  data
rescue Exception => ex
  {}
end

.encode(params, secret = nil) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/tml/utils.rb', line 108

def self.encode(params, secret = nil)
  # TODO: Add signature
  payload = Base64.encode64(params.to_json)
  # URI::encode(payload)
  payload
rescue Exception => ex
  ''
end

.guidObject



61
62
63
# File 'lib/tml/utils.rb', line 61

def self.guid
  (0..16).to_a.map{|a| rand(16).to_s(16)}.join
end

.hash_value(hash, key) ⇒ Object



74
75
76
# File 'lib/tml/utils.rb', line 74

def self.hash_value(hash, key)
  hash[key.to_s] || hash[key.to_sym]
end

.interval_timestamp(interval) ⇒ Object



65
66
67
68
# File 'lib/tml/utils.rb', line 65

def self.interval_timestamp(interval)
  t = Time.now.to_i
  t - (t % interval.to_i)
end

.load_json(file_path, env = nil) ⇒ Object



84
85
86
87
88
89
# File 'lib/tml/utils.rb', line 84

def self.load_json(file_path, env = nil)
  json = JSON.parse(File.read(file_path))
  return json if env.nil?
  return yml['defaults'] if env == 'defaults'
  yml['defaults'].rmerge(yml[env] || {})
end

.load_yaml(file_path, env = nil) ⇒ Object



91
92
93
94
95
96
# File 'lib/tml/utils.rb', line 91

def self.load_yaml(file_path, env = nil)
  yaml = YAML.load_file(file_path)
  return yaml if env.nil?
  return yaml['defaults'] if env == 'defaults'
  yaml['defaults'].rmerge(yaml[env] || {})
end

.normalize_tr_params(label, description, tokens, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tml/utils.rb', line 41

def self.normalize_tr_params(label, description, tokens, options)
  return label if label.is_a?(Hash)

  if description.is_a?(Hash) or description.is_a?(Array)
    return {
      :label        => label,
      :description  => nil,
      :tokens       => description,
      :options      => tokens || {}
    }
  end

  {
    :label        => label,
    :description  => description,
    :tokens       => tokens || {},
    :options      => options || {}
  }
end

.remove_nils(hash) ⇒ Object



78
79
80
81
82
# File 'lib/tml/utils.rb', line 78

def self.remove_nils(hash)
  return unless hash
  proc = Proc.new { |k, v| v.kind_of?(Hash) ? (v.delete_if(&proc); nil) : v.blank? }
  hash.delete_if(&proc)
end

.split_sentences(text) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/tml/utils.rb', line 117

def self.split_sentences(text)
  sentence_regex = /[^.!?\s][^.!?]*(?:[.!?](?![\'"]?\s|$)[^.!?]*)*[.!?]?[\'"]?(?=\s|$)/

  sentences = []
  text.scan(sentence_regex).each do |s|
    sentences << s
  end

  sentences
end

.ungzip(tarfile) ⇒ Object



144
145
146
147
148
149
# File 'lib/tml/utils.rb', line 144

def self.ungzip(tarfile)
  z = Zlib::GzipReader.new(tarfile)
  unzipped = StringIO.new(z.read)
  z.close
  unzipped
end

.untar(io, destination) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/tml/utils.rb', line 151

def self.untar(io, destination)
  Gem::Package::TarReader.new io do |tar|
    tar.each do |tarfile|
      destination_file = File.join destination, tarfile.full_name

      if tarfile.directory?
        FileUtils.mkdir_p destination_file
      else
        destination_directory = File.dirname(destination_file)
        FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
        File.open destination_file, "wb" do |f|
          f.print tarfile.read
        end
      end
    end
  end
end