Class: Tr8n::Utils
- Inherits:
-
Object
- Object
- Tr8n::Utils
- Defined in:
- lib/tr8n/utils.rb
Class Method Summary collapse
-
.browser_accepted_locales(request) ⇒ Object
Author: Iain Hecker reference: github.com/iain/http_accept_language.
- .decode_and_verify_params(signed_request, secret) ⇒ 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
- .sign_and_encode_params(params, secret) ⇒ Object
- .split_by_sentence(text) ⇒ Object
Class Method Details
.browser_accepted_locales(request) ⇒ Object
Author: Iain Hecker reference: github.com/iain/http_accept_language
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/tr8n/utils.rb', line 114 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 Tr8n::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_and_verify_params(signed_request, secret) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tr8n/utils.rb', line 96 def self.decode_and_verify_params(signed_request, secret) signed_request = URI::decode(signed_request) signed_request = Base64.decode64(signed_request) encoded_sig, payload = signed_request.split('.', 2) expected_sig = OpenSSL::HMAC.digest('sha256', secret, payload) expected_sig = Base64.encode64(expected_sig) if expected_sig != encoded_sig raise Tr8n::Exception.new("Bad signature") end JSON.parse(Base64.decode64(payload)) end |
.guid ⇒ Object
53 54 55 |
# File 'lib/tr8n/utils.rb', line 53 def self.guid (0..16).to_a.map{|a| rand(16).to_s(16)}.join end |
.hash_value(hash, key) ⇒ Object
57 58 59 |
# File 'lib/tr8n/utils.rb', line 57 def self.hash_value(hash, key) hash[key.to_s] || hash[key.to_sym] end |
.load_json(file_path, env = nil) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/tr8n/utils.rb', line 72 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
79 80 81 82 83 84 |
# File 'lib/tr8n/utils.rb', line 79 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tr8n/utils.rb', line 33 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 |
.sign_and_encode_params(params, secret) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/tr8n/utils.rb', line 86 def self.sign_and_encode_params(params, secret) payload = params.merge(:algorithm => 'HMAC-SHA256', :ts => Time.now.to_i).to_json payload = Base64.encode64(payload) sig = OpenSSL::HMAC.digest('sha256', secret, payload) encoded_sig = Base64.encode64(sig) URI::encode(Base64.encode64("#{encoded_sig}.#{payload}")) end |
.split_by_sentence(text) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tr8n/utils.rb', line 61 def self.split_by_sentence(text) sentence_regex = /[^.!?\s][^.!?]*(?:[.!?](?![\'"]?\s|$)[^.!?]*)*[.!?]?[\'"]?(?=\s|$)/ sentences = [] text.scan(sentence_regex).each do |s| sentences << s end sentences end |