Class: InternetHakai::Util
- Inherits:
-
Object
- Object
- InternetHakai::Util
- Defined in:
- lib/internethakai/util.rb
Class Method Summary collapse
- .convert_encoding(str, params) ⇒ Object
- .encode_method(meth) ⇒ Object
-
.get_encode(encode) ⇒ Object
Kconv::kconv(str, Kconv::UTF8, self::get_encode(encoding)).
- .get_encode_nkf(encode) ⇒ Object
- .get_encode_nkf_input(encode) ⇒ Object
- .get_query(path) ⇒ Object
- .hash2poststring(body) ⇒ Object
- .parse_post_string(ar) ⇒ Object
- .parse_url(url) ⇒ Object
-
.recover_encoding(str, params) ⇒ Object
Kconv::kconv(str, self::get_encode(encoding), Kconv::UTF8).
- .set_params(url, params) ⇒ Object
- .time(msg, &b) ⇒ Object
Class Method Details
.convert_encoding(str, params) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/internethakai/util.rb', line 71 def self::convert_encoding(str, params) return if str.nil? encoding = params["encoding"].upcase opt = "-x#{self::get_encode_nkf(encoding)}W" NKF::nkf(opt, str) #Kconv::kconv(str, self::get_encode(encoding), Kconv::UTF8) end |
.encode_method(meth) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/internethakai/util.rb', line 6 def self::encode_method(meth) case meth when 'GET' GET when 'POST' POST when 'PUT' PUT when 'DELETE' DELETE end end |
.get_encode(encode) ⇒ Object
Kconv::kconv(str, Kconv::UTF8, self::get_encode(encoding))
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/internethakai/util.rb', line 85 def self::get_encode encode case encode when "SHIFT_JIS", "SJIS-WIN", "SHIFTJIS" Kconv::SJIS when "UTF-8" Kconv::UTF8 when "JIS" Kconv::JIS when "EUC-JP" Kconv::EUC else Kconv::UNKNOWN end end |
.get_encode_nkf(encode) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/internethakai/util.rb', line 99 def self::get_encode_nkf encode case encode when "SHIFT_JIS", "SJIS-WIN", "SHIFTJIS" 's' when "UTF-8" 'w' when "JIS" 'j' when "EUC-JP" 'e' else nil end end |
.get_encode_nkf_input(encode) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/internethakai/util.rb', line 113 def self::get_encode_nkf_input encode case encode when "SHIFT_JIS", "SJIS-WIN", "SHIFTJIS" 'S' when "UTF-8" 'W' when "JIS" 'J' when "EUC-JP" 'E' else nil end end |
.get_query(path) ⇒ Object
67 68 69 70 |
# File 'lib/internethakai/util.rb', line 67 def self::get_query path #ワンライナーで書けた! Hash[ *path[/\?(.*)/, 1].to_s.split("&").map{|kv| ((v = kv.split("=")).size==2 ? v : []).map(&URI::method(:decode))}.flatten ] end |
.hash2poststring(body) ⇒ Object
24 25 26 27 28 |
# File 'lib/internethakai/util.rb', line 24 def self::hash2poststring(body) body.map do |key,value| "#{URI.encode(key)}=#{URI.encode(value.to_s)}" end.join("&") end |
.parse_post_string(ar) ⇒ Object
62 63 64 65 66 |
# File 'lib/internethakai/util.rb', line 62 def self::parse_post_string ar ar.map do |key,value| "#{URI.encode(key)}=#{URI.encode(value.to_s)}" end.join("&") end |
.parse_url(url) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/internethakai/util.rb', line 45 def self::parse_url url hostpath = url.slice(url.index('://')+3, url.size) if pathstart = hostpath.index('/') host = hostpath.slice(0, pathstart) path = hostpath.slice(pathstart, hostpath.size) else host = hostpath path = '/' end if portstart = host.index(':') host, port = host.split(':') port = port.to_i else port = 80 end [host, port, path] end |
.recover_encoding(str, params) ⇒ Object
Kconv::kconv(str, self::get_encode(encoding), Kconv::UTF8)
78 79 80 81 82 83 84 |
# File 'lib/internethakai/util.rb', line 78 def self::recover_encoding(str, params) return if str.nil? encoding = params["encoding"].upcase opt = "-xw#{self::get_encode_nkf_input(encoding)}" NKF::nkf(opt, str) #Kconv::kconv(str, Kconv::UTF8, self::get_encode(encoding)) end |
.set_params(url, params) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/internethakai/util.rb', line 29 def self::set_params(url, params) unless url.include? "?" url += "?" else url[url.size-1] != "&" url += "&" end q = [] params.each do |k,v| one = URI::encode(k.to_s) one += "=" one += URI::encode(v.to_s) q << one end url + q.join("&") end |
.time(msg, &b) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/internethakai/util.rb', line 18 def self::time(msg, &b) t = Time::now b.call d = (Time::now - t)*1000 puts "#{msg}: #{d}" end |