Module: JavyTool::Utils

Defined in:
lib/javy_tool/utils.rb

Class Method Summary collapse

Class Method Details

.android?(req) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/javy_tool/utils.rb', line 28

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



50
51
52
53
# File 'lib/javy_tool/utils.rb', line 50

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

Returns:

  • (Boolean)


37
38
39
# File 'lib/javy_tool/utils.rb', line 37

def ios?(req)
  ipad?(req) || iphone?(req)
end

.ipad?(req) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/javy_tool/utils.rb', line 31

def ipad?(req)
  !!(/(iPad).*OS\s([\d_]+)/ =~ req)
end

.iphone?(req) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/javy_tool/utils.rb', line 34

def iphone?(req)
  !ipad?(req) && !!(/(iPhone\sOS)\s([\d_]+)|iTunes-iPhone|iTunes-iPod/ =~ req)
end

.touchpad?(req) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/javy_tool/utils.rb', line 43

def touchpad?(req)
  webos?(req) && !!(/TouchPad/ =~ req)
end

.truncate_o(text, length = 16) ⇒ Object

truncate string and chinese is two chars



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

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



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

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/javy_tool/utils.rb', line 76

def upload_file(file,path=nil)
  path ||= JavyTool.options[: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)
     # if File.extname(file.original_filename).downcase == ".apk"
     #            file.original_filename.gsub(/[^\w]/,'') #
     # end
    end
    File.open(path+filename, "wb") { |f| f.write(file.read) }
    filename
  end
end

.webkit?(req) ⇒ Boolean

judge user agent

Returns:

  • (Boolean)


25
26
27
# File 'lib/javy_tool/utils.rb', line 25

def webkit?(req)
  !!(/WebKit\/([\d.]+)/ =~ req)
end

.webos?(req) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/javy_tool/utils.rb', line 40

def webos?(req)
  !!(/(webOS|hpwOS)[\s\/]([\d.]+)/ =~ req)
end

.which_os(req) ⇒ Object

get user agent



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/javy_tool/utils.rb', line 7

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