Class: Func

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

Class Method Summary collapse

Class Method Details

.formattime(time) ⇒ Object

格式化时间



13
14
15
# File 'lib/func.rb', line 13

def self.formattime(time)
  time.strftime("%Y-%m-%d %H:%M:%S")
end

.get(url, params = nil, header = nil) ⇒ Object

get请求 参数url:网址,header:请求头json类型如:=> ‘application/json’, ‘X-Requested-With’ => ‘XMLHttpRequest’ 返回response对象,response.code 状态200/304/404;response.body 内容



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/func.rb', line 75

def self.get(url, params=nil, header=nil)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  request = Net::HTTP::Get.new(uri.request_uri)
  if !params.blank?
    request.form_data = params
  end
  if !header.nil?
    request.initialize_http_header(header)
  end
  http.request(request)
end

.ischinese(temp) ⇒ Object

判断是否含有中文



3
4
5
# File 'lib/func.rb', line 3

def self.ischinese(temp)
  (temp=~/[\u4e00-\u9fa5]/).nil? ? false : true
end

.iscn_zn_num(temp) ⇒ Object

判断是否只含有中英文,数字和下划线



8
9
10
# File 'lib/func.rb', line 8

def self.iscn_zn_num(temp)
  (temp=~/^[\u4e00-\u9fa5_a-zA-Z0-9]+$/).nil? ? false : true
end

.post(url, params = nil, header = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/func.rb', line 89

def self.post(url, params=nil, header=nil)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  request = Net::HTTP::Post.new(uri.request_uri)
  if !params.blank?
    request.set_form_data(params)
  end
  if !header.nil?
    request.initialize_http_header(header)
  end
  http.request(request)
end

.truncate_u(text, length = 30, truncate_string = "...") ⇒ Object

限制文字长度



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/func.rb', line 18

def self.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

.uppercase(nums) ⇒ Object

金额大写小转换



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/func.rb', line 31

def self.uppercase(nums)
  cstr = ["", "", "", "", "", "", "", "", "", ""]
  cn_nums1 = ["", "", "", "", "", "", "", "", "", "", "", ""]
  cn_nums2 = ['', '']
  s = ""
  # 整数部分
  array = nums.to_s.split(".")
  p h = array[0].to_s.split(//)
  ai = h.count
  h.each_with_index do |a, j|
    s+=cstr[a.to_i]+cn_nums1[ai-1]
    ai=ai-1
  end
  # 小数部分
  p h1 = array[1].to_s.split(//)
  aj = h1.count
  h1.each_with_index do |o, p|
    s+=cstr[o.to_i]+cn_nums2[aj-1]
    aj=aj-1
  end
  rstr = ""
  rstr=s.gsub("拾零", "")
  rstr=rstr.gsub("零拾", "");
  rstr=rstr.gsub("零佰", "");
  rstr=rstr.gsub("零仟", "");
  rstr=rstr.gsub("零萬", "");
  for i in 1..6 do
    rstr=rstr.gsub("零零", "");
    rstr=rstr.gsub("零萬", "");
    rstr=rstr.gsub("零億", "");
    rstr=rstr.gsub("零零", "");
  end
  rstr=rstr.gsub("零角", "");
  rstr=rstr.gsub("零分", "");
  rstr=rstr.gsub("零元", "");
end