class Granify::Utils

Public Class Methods

generate_path(branch, time, identifier) click to toggle source

Generate a filename

# File lib/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 = '') click to toggle source

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

# File lib/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) click to toggle source

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

# File lib/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) click to toggle source

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

# File lib/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?() click to toggle source
# File lib/utils.rb, line 119
def self.has_internet_connection?
  Utils.http_response_code < 499
end
http_response_code(url = nil) click to toggle source
# File lib/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) click to toggle source
# File lib/utils.rb, line 102
def self.json?(string)
  begin
    !!JSON.parse(string)
  rescue
    false
  end
end
mklocaldir(name) click to toggle source

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

# File lib/utils.rb, line 70
def self.mklocaldir(name)
  dir = "#{Dir.pwd}/#{name.downcase}/"

  if !Dir.exist? dir
    Dir.mkdir dir
  else
    dir
  end
end
os() click to toggle source
# File lib/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 err
    Notify.error(err.message)
  end
end
symbolize_keys(hash) click to toggle source

Convert hash keys to symbols

# File lib/utils.rb, line 65
def self.symbolize_keys(hash)
  Hash[hash.map{ |k, v| [k.to_sym, v] }]
end