Class: Gemnasium::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/gemnasium/configuration.rb

Constant Summary collapse

DEFAULT_CONFIG =
{ 'site' => 'gemnasium.com',
'use_ssl' => true,
'api_version' => 'v3',
'ignored_paths' => [] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Configuration

Initialize the configuration object from a YAML file

Parameters:

  • config_file (String)

    path to the configuration file



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



6
7
8
# File 'lib/gemnasium/configuration.rb', line 6

def api_key
  @api_key
end

#api_versionObject

Returns the value of attribute api_version.



6
7
8
# File 'lib/gemnasium/configuration.rb', line 6

def api_version
  @api_version
end

#ignored_pathsObject

Returns the value of attribute ignored_paths.



6
7
8
# File 'lib/gemnasium/configuration.rb', line 6

def ignored_paths
  @ignored_paths
end

#profile_nameObject

Keep profile name for backward compatibility with version =< 2.0



10
11
12
# File 'lib/gemnasium/configuration.rb', line 10

def profile_name
  @profile_name
end

#project_branchObject

Returns the value of attribute project_branch.



6
7
8
# File 'lib/gemnasium/configuration.rb', line 6

def project_branch
  @project_branch
end

#project_nameObject

Name is required only to create the project, make it mandatory.



8
9
10
# File 'lib/gemnasium/configuration.rb', line 8

def project_name
  @project_name
end

#project_slugObject

Slug is required only to push the dependency files, make it optional.



9
10
11
# File 'lib/gemnasium/configuration.rb', line 9

def project_slug
  @project_slug
end

#siteObject

Returns the value of attribute site.



6
7
8
# File 'lib/gemnasium/configuration.rb', line 6

def site
  @site
end

#use_sslObject

Returns the value of attribute use_ssl.



6
7
8
# File 'lib/gemnasium/configuration.rb', line 6

def use_ssl
  @use_ssl
end

Instance Method Details

#migrate!Object



67
68
69
70
71
72
73
74
75
76
77
78
# 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?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/gemnasium/configuration.rb', line 63

def needs_to_migrate?
  !profile_name.nil?
end

#store_value!(key, value, comment = nil) ⇒ Object

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.

Parameters:

  • key (String)

    key

  • value (String)

    value to store for given key

  • comment (String) (defaults to: nil)

    optional comment



47
48
49
50
51
52
53
54
55
56
57
# 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?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/gemnasium/configuration.rb', line 59

def writable?
  File.writable? path
end