Module: Helper
Overview
雑多なお助けメソッド群
MacOSX 関連は確認してないので動作するか不明
Defined Under Namespace
Classes: AsyncCommand
Constant Summary collapse
- ENTITIES =
{ quot: '"', amp: "&", nbsp: " ", lt: "<", gt: ">", copy: "(c)" }
Instance Method Summary collapse
- #confirm(message) ⇒ Object
- #determine_os ⇒ Object
- #open_browser(url) ⇒ Object
- #open_browser_linux(address, error_message) ⇒ Object
- #open_directory(path, confirm_message = nil) ⇒ Object
- #os_mac? ⇒ Boolean
- #os_windows? ⇒ Boolean
-
#pretreatment_source(src, encoding = Encoding::UTF_8) ⇒ Object
ダウンロードしてきたデータを使いやすいように処理.
- #print_horizontal_rule ⇒ Object
- #replace_filename_special_chars(str, invalid_replace = false) ⇒ Object
-
#restor_entity(str) ⇒ Object
エンティティ復号.
Instance Method Details
#confirm(message) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/helper.rb', line 35 def confirm() confirm_msg = "#{message} (y/n)?: " STDOUT.print confirm_msg # Logger でロギングされないように直接標準出力に表示 while input = $stdin.gets case input[0].downcase when "y" return true when "n" return false else STDOUT.print confirm_msg end end end |
#determine_os ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/helper.rb', line 24 def determine_os case when os_windows? :windows when os_mac? :mac else :other end end |
#open_browser(url) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/helper.rb', line 72 def open_browser(url) case determine_os when :windows escaped_url = url.gsub("%", "%^").gsub("&", "^&") # MEMO: start の引数を "" で囲むと動かない `start #{escaped_url}` when :mac `open "#{url}"` else open_browser_linux(url, "ブラウザが見つかりませんでした") end end |
#open_browser_linux(address, error_message) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/helper.rb', line 50 def open_browser_linux(address, ) %w(xdg-open firefox w3m).each do |browser| system(%!#{browser} "#{address}"!) return if $?.exitstatus != 127 end error end |
#open_directory(path, confirm_message = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/helper.rb', line 58 def open_directory(path, = nil) if return unless confirm() end case determine_os when :windows `explorer "file:///#{path.encode(Encoding::Windows_31J)}"` when :mac `open "#{path}"` else open_browser_linux(path, "フォルダが開けませんでした") end end |
#os_mac? ⇒ Boolean
20 21 22 |
# File 'lib/helper.rb', line 20 def os_mac? @@os_is_mac ||= RUBY_PLATFORM =~ /darwin/ end |
#os_windows? ⇒ Boolean
16 17 18 |
# File 'lib/helper.rb', line 16 def os_windows? @@os_is_windows ||= RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin|bccwin/i end |
#pretreatment_source(src, encoding = Encoding::UTF_8) ⇒ Object
ダウンロードしてきたデータを使いやすいように処理
102 103 104 |
# File 'lib/helper.rb', line 102 def pretreatment_source(src, encoding = Encoding::UTF_8) restor_entity(src.force_encoding(encoding)).gsub("\r", "") end |
#print_horizontal_rule ⇒ Object
85 86 87 |
# File 'lib/helper.rb', line 85 def print_horizontal_rule puts "―" * 35 end |
#replace_filename_special_chars(str, invalid_replace = false) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/helper.rb', line 89 def replace_filename_special_chars(str, invalid_replace = false) result = str.tr("/:*?\"<>|.", "/:*?”〈〉|.").gsub("\\", "¥") if invalid_replace org_encoding = result.encoding result = result.encode(Encoding::Windows_31J, invalid: :replace, undef: :replace, replace: "_") .encode(org_encoding) end result end |
#restor_entity(str) ⇒ Object
エンティティ復号
110 111 112 113 114 115 116 |
# File 'lib/helper.rb', line 110 def restor_entity(str) result = str.encode("UTF-16BE", "UTF-8", :invalid => :replace, :undef => :replace, :replace => "?").encode("UTF-8") ENTITIES.each do |key, value| result.gsub!("&#{key};", value) end result end |