Module: JavyTool::Utils
- Defined in:
- lib/javy_tool/utils.rb
Class Method Summary collapse
- .android?(req) ⇒ Boolean
-
.h2o(ahash) ⇒ Object
translate a Hash object to a OpenStruct object parameter: ahash => Hash return: a OpenStruct object.
- .ios?(req) ⇒ Boolean
- .ipad?(req) ⇒ Boolean
- .iphone?(req) ⇒ Boolean
-
.lottery(*args) ⇒ Object
用于抽奖 传入代表中奖概率的数组如 1,10,20,30,40 返回数组的索引 真实场景 奖项数组 prize_arr =[ [‘id’=>1,‘prize’=>‘平板电脑’,‘v’=>1], [‘id’=>2,‘prize’=>‘数码相机’,‘v’=>5], [‘id’=>3,‘prize’=>‘音箱设备’,‘v’=>10], [‘id’=>4,‘prize’=>‘4G优盘’,‘v’=>12], [‘id’=>5,‘prize’=>‘10Q币’,‘v’=>22], [‘id’=>6,‘prize’=>‘下次没准就能中哦’,‘v’=>50], ].
- .touchpad?(req) ⇒ Boolean
-
.truncate_o(text, length = 16) ⇒ Object
truncate string and chinese is two chars.
-
.truncate_u(text, length = 30, truncate_string = "...") ⇒ Object
truncate string with utf-8 encoding.
-
.upload_file(file, path = nil) ⇒ Object
upload file,default to /tmp return filename.
-
.webkit?(req) ⇒ Boolean
judge user agent.
- .webos?(req) ⇒ Boolean
-
.which_os(req) ⇒ Object
get user agent.
Class Method Details
.android?(req) ⇒ Boolean
29 30 31 |
# File 'lib/javy_tool/utils.rb', line 29 def android?(req) !!(/(Android)\s+([\d.]+)/ =~ req) end |
.h2o(ahash) ⇒ Object
translate a Hash object to a OpenStruct object parameter: ahash => Hash return: a OpenStruct object
51 52 53 54 |
# File 'lib/javy_tool/utils.rb', line 51 def h2o( ahash ) return ahash unless Hash === ahash OpenStruct.new( Hash[ *ahash.inject( [] ) { |a, (k, v)| a.push(k, h2o(v)) } ] ) end |
.ios?(req) ⇒ Boolean
38 39 40 |
# File 'lib/javy_tool/utils.rb', line 38 def ios?(req) ipad?(req) || iphone?(req) end |
.ipad?(req) ⇒ Boolean
32 33 34 |
# File 'lib/javy_tool/utils.rb', line 32 def ipad?(req) !!(/(iPad).*OS\s([\d_]+)/ =~ req) end |
.iphone?(req) ⇒ Boolean
35 36 37 |
# File 'lib/javy_tool/utils.rb', line 35 def iphone?(req) !ipad?(req) && !!(/(iPhone\sOS)\s([\d_]+)|iTunes-iPhone|iTunes-iPod/ =~ req) end |
.lottery(*args) ⇒ Object
用于抽奖 传入代表中奖概率的数组如 1,10,20,30,40 返回数组的索引 真实场景 奖项数组 prize_arr =[ [‘id’=>1,‘prize’=>‘平板电脑’,‘v’=>1], [‘id’=>2,‘prize’=>‘数码相机’,‘v’=>5], [‘id’=>3,‘prize’=>‘音箱设备’,‘v’=>10], [‘id’=>4,‘prize’=>‘4G优盘’,‘v’=>12], [‘id’=>5,‘prize’=>‘10Q币’,‘v’=>22], [‘id’=>6,‘prize’=>‘下次没准就能中哦’,‘v’=>50], ]
传给loggery的数组应为 1,5,10,12,22,50 返回的索引值即代表奖品的索引值
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/javy_tool/utils.rb', line 93 def lottery(*args) args. total = args.inject{|sum,item|sum+=item} args.flatten.each_with_index do |item,index| rand_num = rand(1..total)#.tap{|it|puts ".#{item}.#{index}..#{it}.#{total}-------"} if rand_num <= item return index else total -= item end end end |
.touchpad?(req) ⇒ Boolean
44 45 46 |
# File 'lib/javy_tool/utils.rb', line 44 def touchpad?(req) webos?(req) && !!(/TouchPad/ =~ req) end |
.truncate_o(text, length = 16) ⇒ Object
truncate string and chinese is two chars
70 71 72 73 |
# File 'lib/javy_tool/utils.rb', line 70 def truncate_o(text,length=16) text = Iconv.conv("gb2312","utf8",text)[0,length] Iconv.conv("utf8","gb2312",text) end |
.truncate_u(text, length = 30, truncate_string = "...") ⇒ Object
truncate string with utf-8 encoding
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/javy_tool/utils.rb', line 57 def truncate_u(text, length = 30, truncate_string = "...") l=0 char_array=text.unpack("U*") char_array.each_with_index do |c,i| l = l+ (c<127 ? 0.5 : 1) if l>=length return char_array[0..i].pack("U*")+(i<char_array.length-1 ? truncate_string : "") end end return text end |
.upload_file(file, path = nil) ⇒ Object
upload file,default to /tmp return filename
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/javy_tool/utils.rb', line 109 def upload_file(file,path=nil) path ||= JavyTool.[:upload_path] unless file.original_filename.empty? filename = if block_given? yield file.original_filename else Time.now.strftime("%Y%m%d%H%M%S") + rand(10000).to_s + File.extname(file.original_filename) end File.open(path+filename, "wb") { |f| f.write(file.read) } filename end end |
.webkit?(req) ⇒ Boolean
judge user agent
26 27 28 |
# File 'lib/javy_tool/utils.rb', line 26 def webkit?(req) !!(/WebKit\/([\d.]+)/ =~ req) end |
.webos?(req) ⇒ Boolean
41 42 43 |
# File 'lib/javy_tool/utils.rb', line 41 def webos?(req) !!(/(webOS|hpwOS)[\s\/]([\d.]+)/ =~ req) end |
.which_os(req) ⇒ Object
get user agent
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/javy_tool/utils.rb', line 8 def which_os(req) case req when /(Android)\s+([\d.]+)/ "android" when /(iPad).*OS\s([\d_]+)|iTunes-iPad/ "ipad" when /(iPhone\sOS)\s([\d_]+)|iTunes-iPhone|iTunes-iPod/ "iphone" when /TouchPad/ "touchpad" when /(webOS|hpwOS)[\s\/]([\d.]+)/ "webos" when /WebKit\/([\d.]+)/ "webkit" end end |