Class: ActFunc::Func

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

Class Method Summary collapse

Class Method Details

.formattime(time) ⇒ Object

格式化时间



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

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 内容



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

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

判断是否含有中文



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

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

.iscn_zn_num(temp) ⇒ Object

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



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

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

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



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

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

限制文字长度



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

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

金额大写小转换



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
67
# File 'lib/act_func/func.rb', line 32

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