Class: Evertils::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/evertils/utils.rb

Class Method Summary collapse

Class Method Details

.generate_path(branch, time, identifier) ⇒ Object

Generate a filename



56
57
58
59
60
61
62
# File 'lib/evertils/utils.rb', line 56

def self.generate_path(branch, time, identifier)
  # create the directory if needed
  Logs.mkdir identifier

  # create a new log file with a path based on the input parameters
  #Log.new(identifier, branch, time)
end

.get_all_files(ext, ignore_paths = '') ⇒ Object

Gets a list of all files in the project with a specific extension



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/evertils/utils.rb', line 18

def self.get_all_files(ext, ignore_paths = '')
  @cache[:files] ||= Hash.new
  @cache[:files][ext] ||= []

  if @cache[:files][ext].empty?
    Dir["**/*.#{ext}"].each do |file|
      if !ignore_paths.empty?
        # file is a widget
        if /\/#{ignore_paths}/.match(file)
          @cache[:files][ext].push file
        end
      else
        # file is not a widget
        @cache[:files][ext].push file
      end
    end
  end

  @cache[:files][ext]
end

.get_files(ext) ⇒ Object

Gets a list of files from the current directory with a specific extension



6
7
8
9
10
11
12
13
14
15
# File 'lib/evertils/utils.rb', line 6

def self.get_files(ext)
  @cache[:files] ||= Hash.new
  @cache[:files][ext] ||= []

  Dir["*.#{ext}"].each do |file|
    @cache[:files][ext].push file
  end

  @cache[:files][ext]
end

.get_files_from_git(ext) ⇒ Object

Gets a list of files from the current git commit queue with a specific extension



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/evertils/utils.rb', line 41

def self.get_files_from_git(ext)
  @cache[:files] ||= Hash.new
  @cache[:files][ext] ||= []

  modified_files = `git status --porcelain`.split("\n")
  modified_files.each do |file|
    if file.match(/#{ext}/)
      @cache[:files][ext].push file.strip.match(/[A-Z ]+(.*)/)[1]
    end
  end

  @cache[:files][ext]
end

.has_internet_connection?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/evertils/utils.rb', line 119

def self.has_internet_connection?
  Utils.http_response_code < 499
end

.http_response_code(url = nil) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/evertils/utils.rb', line 110

def self.http_response_code(url = nil)
  begin
    request = Net::HTTP.get_response(URI.parse(url || "http://google.com"))
    request.code.to_i
  rescue
    500
  end
end

.json?(string) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/evertils/utils.rb', line 102

def self.json?(string)
  begin
    !!JSON.parse(string)
  rescue
    false
  end
end

.mklocaldir(name) ⇒ Object

Create a directory wherever the script is called from, if required



70
71
72
73
74
75
76
77
78
# File 'lib/evertils/utils.rb', line 70

def self.mklocaldir(name)
  dir = "#{Dir.pwd}/#{name.downcase}/"

  if !Dir.exist? dir
    Dir.mkdir dir
  else
    dir
  end
end

.osObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/evertils/utils.rb', line 80

def self.os
  begin
    @os ||= (
      host_os = RbConfig::CONFIG['host_os']
      case host_os
      when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
        :windows
      when /darwin|mac os/
        :macosx
      when /linux/
        :linux
      when /solaris|bsd/
        :unix
      else
        raise TypeError, "unknown os: #{host_os.inspect}"
      end
    )
  rescue e
    Notify.error("#{e}\n#{e.backtrace.join("\n")}", show_time: false)
  end
end

.symbolize_keys(hash) ⇒ Object

Convert hash keys to symbols



65
66
67
# File 'lib/evertils/utils.rb', line 65

def self.symbolize_keys(hash)
  Hash[hash.map{ |k, v| [k.to_sym, v] }]
end