Module: Helper

Extended by:
Helper
Included in:
Helper
Defined in:
lib/helper.rb

Overview

雑多なお助けメソッド群

Defined Under Namespace

Classes: AsyncCommand

Constant Summary collapse

ENTITIES =
{ quot: '"', amp: "&", nbsp: " ", lt: "<", gt: ">", copy: "(c)", "#39" => "'" }

Instance Method Summary collapse

Instance Method Details

#confirm(message) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/helper.rb', line 39

def confirm(message)
  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

#convert_to_windows_path(path) ⇒ Object

CYGWINのパスからwindowsのパスへと変換(cygpathを呼び出すだけ)



129
130
131
# File 'lib/helper.rb', line 129

def convert_to_windows_path(path)
  `cygpath -aw \"#{path}\"`.strip
end

#determine_osObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/helper.rb', line 26

def determine_os
  case
  when os_windows?
    :windows
  when os_mac?
    :mac
  when os_cygwin?
    :cygwin
  else
    :other
  end
end

#open_browser(url) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/helper.rb', line 78

def open_browser(url)
  case determine_os
  when :windows
    escaped_url = url.gsub("%", "%^").gsub("&", "^&")
    # MEMO: start の引数を "" で囲むと動かない
    `start #{escaped_url}`
  when :cygwin
    `cygstart #{url}`
  when :mac
    `open "#{url}"`
  else
    open_browser_linux(url, "ブラウザが見つかりませんでした")
  end
end

#open_browser_linux(address, error_message) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/helper.rb', line 54

def open_browser_linux(address, error_message)
  %w(xdg-open firefox w3m).each do |browser|
    system(%!#{browser} "#{address}"!)
    return if $?.exitstatus != 127
  end
  error error_message
end

#open_directory(path, confirm_message = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/helper.rb', line 62

def open_directory(path, confirm_message = nil)
  if confirm_message
    return unless confirm(confirm_message)
  end
  case determine_os
  when :windows
    `explorer "file:///#{path.encode(Encoding::Windows_31J)}"`
  when :cygwin
    `cygstart "#{path}"`
  when :mac
    `open "#{path}"`
  else
    open_browser_linux(path, "フォルダが開けませんでした")
  end
end

#os_cygwin?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/helper.rb', line 22

def os_cygwin?
  @@os_is_cygwin ||= RUBY_PLATFORM =~ /cygwin/i
end

#os_mac?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/helper.rb', line 18

def os_mac?
  @@os_is_mac ||= RUBY_PLATFORM =~ /darwin/
end

#os_windows?Boolean

Returns:

  • (Boolean)


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

def os_windows?
  @@os_is_windows ||= RUBY_PLATFORM =~ /mswin(?!ce)|mingw|bccwin/i
end

#pretreatment_source(src, encoding = Encoding::UTF_8) ⇒ Object

ダウンロードしてきたデータを使いやすいように処理



110
111
112
# File 'lib/helper.rb', line 110

def pretreatment_source(src, encoding = Encoding::UTF_8)
  restor_entity(src.force_encoding(encoding)).gsub("\r", "")
end


93
94
95
# File 'lib/helper.rb', line 93

def print_horizontal_rule
  puts "" * 35
end

#replace_filename_special_chars(str, invalid_replace = false) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/helper.rb', line 97

def replace_filename_special_chars(str, invalid_replace = false)
  result = str.tr("/:*?\"<>|.", "/:*?”〈〉|.").gsub("\\", "").gsub("\t", "").gsub("\n", "")
  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

エンティティ復号



118
119
120
121
122
123
124
# File 'lib/helper.rb', line 118

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