class Gemnasium::Configuration

Constants

DEFAULT_CONFIG

Attributes

api_key[RW]
api_version[RW]
ignored_paths[RW]
path[R]
profile_name[RW]
project_branch[RW]
project_name[RW]
project_slug[RW]
site[RW]
use_ssl[RW]

Public Class Methods

new(config_file) click to toggle source

Initialize the configuration object from a YAML file

@param config_file [String] path to the configuration file

# File lib/gemnasium/configuration.rb, line 20
def initialize config_file
  unless File.file?(config_file)
    raise Errno::ENOENT,
      "Configuration file (#{config_file}) does not exist.\nPlease run `gemnasium install`."
  end
  @path = config_file

  config_hash = DEFAULT_CONFIG.merge(YAML.load(ERB.new(File.read(config_file)).result))
  config_hash.each do |k, v|
    writer_method = "#{k}="
    if respond_to?(writer_method)
      v = convert_ignored_paths_to_regexp(v) if k.to_s == 'ignored_paths'
      send(writer_method, v)
    end
  end

  raise 'Your configuration file does not contain all mandatory parameters or contain invalid values. Please check the documentation.' unless is_valid?
end

Public Instance Methods

migrate!() click to toggle source
# File lib/gemnasium/configuration.rb, line 67
def migrate!
  # replace profile_name key with project_slug key (no value)
  content = File.readlines(path).map do |line|
    if line =~ /\Aprofile_name:.*\Z/
      "project_slug:"
    else
      line
    end
  end.map{ |l| l.rstrip }.join("\n") + "\n"

  File.write path, content
end
needs_to_migrate?() click to toggle source
# File lib/gemnasium/configuration.rb, line 63
def needs_to_migrate?
  !profile_name.nil?
end
store_value!(key, value, comment = nil) click to toggle source

Store a key-value pair in the configuration file with an optional comment. Try to preserve the comments and the indentation of the file. We assume the configuration file already features the given key.

@param key [String] key @param value [String] value to store for given key @param comment [String] optional comment

# File lib/gemnasium/configuration.rb, line 47
def store_value!(key, value, comment = nil)
  pattern = /\A#{ key }:.*\Z/
  new_line = "#{ key }: #{ value }"
  new_line += " # #{ comment }" if comment

  content = File.readlines(path).map do |line|
    line.rstrip.sub pattern, new_line
  end.join("\n") + "\n"

  File.write path, content
end
writable?() click to toggle source
# File lib/gemnasium/configuration.rb, line 59
def writable?
  File.writable? path
end

Private Instance Methods

convert_ignored_paths_to_regexp(paths) click to toggle source
# File lib/gemnasium/configuration.rb, line 99
def convert_ignored_paths_to_regexp(paths)
  return [] unless paths.kind_of? Array

  paths.inject([]) do |regexp_array, path|
    path = path.insert(0,'^')       # All path start from app root
               .gsub('*','[^/]+')   # Replace `*` to whatever char except slash
               .gsub('.','\.')      # Escape dots
    regexp_array << Regexp.new(path)
  end
end
is_valid?() click to toggle source

Check that mandatory parameters are not nil and contain valid values

@return [Boolean] if configuration is valid

# File lib/gemnasium/configuration.rb, line 87
def is_valid?
  site_option_valid               = !site.nil? && !site.empty?
  api_key_option_valid            = !api_key.nil? && !api_key.empty?
  use_ssl_option_valid            = !use_ssl.nil? && !!use_ssl == use_ssl # Check this is a boolean
  api_version_option_valid        = !api_version.nil? && !api_version.empty?
  project_name_option_valid       = !project_name.nil? && !project_name.empty?
  ignored_paths_option_valid      = ignored_paths.kind_of?(Array)

  site_option_valid && api_key_option_valid && use_ssl_option_valid && api_version_option_valid &&
  project_name_option_valid && ignored_paths_option_valid
end