Class: Tml::Utils
- Inherits:
-
Object
- Object
- Tml::Utils
- Defined in:
- lib/tml/utils.rb
Class Method Summary collapse
-
.browser_accepted_locales(request) ⇒ Object
Author: Iain Hecker reference: github.com/iain/http_accept_language.
- .decode(data) ⇒ Object
- .encode(params) ⇒ Object
- .guid ⇒ Object
- .hash_value(hash, key) ⇒ Object
- .load_json(file_path, env = nil) ⇒ Object
- .load_yaml(file_path, env = nil) ⇒ Object
- .normalize_tr_params(label, description, tokens, options) ⇒ Object
- .split_by_sentence(text) ⇒ Object
- .split_sentences(paragraph) ⇒ Object
Class Method Details
.browser_accepted_locales(request) ⇒ Object
Author: Iain Hecker reference: github.com/iain/http_accept_language
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/tml/utils.rb', line 118 def self.browser_accepted_locales(request) request.env['HTTP_ACCEPT_LANGUAGE'].split(/\s*,\s*/).collect do |l| l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/ l.split(';q=') end.sort do |x,y| raise Tml::Exception.new('Not correctly formatted') unless x.first =~ /^[a-z\-]+$/i 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 |
.decode(data) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/tml/utils.rb', line 94 def self.decode(data) payload = URI::decode(data) payload = Base64.decode64(payload) JSON.parse(payload) rescue Exception => ex {} end |
.encode(params) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/tml/utils.rb', line 102 def self.encode(params) payload = Base64.encode64(params.to_json) URI::encode(payload) rescue Exception => ex '' end |
.guid ⇒ Object
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
65 66 67 |
# File 'lib/tml/utils.rb', line 65 def self.hash_value(hash, key) hash[key.to_s] || hash[key.to_sym] end |
.load_json(file_path, env = nil) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/tml/utils.rb', line 80 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
87 88 89 90 91 92 |
# File 'lib/tml/utils.rb', line 87 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, ) return label if label.is_a?(Hash) if description.is_a?(Hash) return { :label => label, :description => nil, :tokens => description, :options => tokens } end { :label => label, :description => description, :tokens => tokens, :options => } end |
.split_by_sentence(text) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tml/utils.rb', line 69 def self.split_by_sentence(text) sentence_regex = /[^.!?\s][^.!?]*(?:[.!?](?![\'"]?\s|$)[^.!?]*)*[.!?]?[\'"]?(?=\s|$)/ sentences = [] text.scan(sentence_regex).each do |s| sentences << s end sentences end |
.split_sentences(paragraph) ⇒ Object
109 110 111 112 |
# File 'lib/tml/utils.rb', line 109 def self.split_sentences(paragraph) sentence_regex = /[^.!?\s][^.!?]*(?:[.!?](?![\'"]?\s|$)[^.!?]*)*[.!?]?[\'"]?(?=\s|$)/ paragraph.match(sentence_regex) end |