Module: HotPixiv::Utils

Defined in:
lib/hotpixiv/utils.rb

Class Method Summary collapse

Class Method Details

.create_dir(*args) ⇒ Object



31
32
33
# File 'lib/hotpixiv/utils.rb', line 31

def create_dir(*args)
  Dir::mkdir(create_path(args)) rescue nil
end

.create_path(*args) ⇒ Object



27
28
29
# File 'lib/hotpixiv/utils.rb', line 27

def create_path(*args)
  Pathname.new(args.join("/")).cleanpath.to_s.encode(Utils.os_encoding)
end

.directory?(e) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/hotpixiv/utils.rb', line 23

def directory?(e)
  !!(FileTest::directory?(e) rescue nil)
end

.file?(e) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/hotpixiv/utils.rb', line 19

def file?(e)
  !!(FileTest.exist?(e) rescue nil)
end

.load(path) ⇒ Object



35
36
37
38
# File 'lib/hotpixiv/utils.rb', line 35

def load(path)
  config = YAML.load_file(path)
  config.inject({}){|r, entry| r[entry[0].to_sym] = entry[1]; r}
end

.os_encodingObject



40
41
42
43
# File 'lib/hotpixiv/utils.rb', line 40

def os_encoding
  RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/ ?
    "Windows-31J" : "UTF-8"
end

.read_text(path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hotpixiv/utils.rb', line 7

def read_text(path)
  data = []
  if file?(path)
    open(path) do |f|
      while line = f.gets do
        data << line.chomp unless line.chomp.empty?
      end
    end
  end
  data
end