Class: Mongo::Jira::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/jira/config.rb

Constant Summary collapse

CFG_FILE =
"#{ENV['HOME']}/.mongo_jira.json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Config

Returns a new instance of Config.

Raises:



55
56
57
58
59
60
61
62
63
# File 'lib/mongo/jira/config.rb', line 55

def initialize(file)
  raise ConfigException , "file not found #{file}" unless File::exists?(file)
  @filename=file
  @config = JSON.parse( IO.read(filename) )
  config.symbolize_keys!()
  config[:auth_type] = (config[:auth_type] || :basic).to_sym
  config[:context_path] = ''
  config[:password] = Mongo::Jira::Config::decrypt("password", config[:password]) if config[:password]
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



54
55
56
# File 'lib/mongo/jira/config.rb', line 54

def config
  @config
end

#filenameObject

Returns the value of attribute filename.



54
55
56
# File 'lib/mongo/jira/config.rb', line 54

def filename
  @filename
end

Class Method Details

.check(filename = CFG_FILE) ⇒ true, false

check the config file

Examples:

Save to default location and specific location.

Mongo::Jira::Config::check()
Mongo::Jira::Config::check("/tmp/.jira.json")

Parameters:

  • filename (String) (defaults to: CFG_FILE)

    The config file path. It defaults to “#'HOME'/.mongo_jira.json”

Returns:

  • (true, false)

    If the config is ok.

Since:

  • 0.0.1



34
35
36
# File 'lib/mongo/jira/config.rb', line 34

def self.check(filename=CFG_FILE)
  File::exists?(filename)
end

.decrypt(key, data) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/mongo/jira/config.rb', line 98

def self.decrypt(key, data)
  cipher = OpenSSL::Cipher::Cipher.new("des3")
  cipher.decrypt
  cipher.key = key * 10
  cipher.iv = '12345678'
  plaintext = cipher.update(Base64.decode64(data))
  plaintext << cipher.final
end

.encrypt(key, value) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/mongo/jira/config.rb', line 88

def self.encrypt(key, value)
  cipher = OpenSSL::Cipher::Cipher.new("des3")
  cipher.encrypt # Call this before setting key or iv
  cipher.key = key * 10
  cipher.iv = '12345678'
  text = cipher.update(value)
  text << cipher.final
  Base64.encode64(text).chomp
end

.load(filename = CFG_FILE) ⇒ Object



50
51
52
# File 'lib/mongo/jira/config.rb', line 50

def self.load(filename=CFG_FILE)
  Config.new(filename)
end

.save(config, filename) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mongo/jira/config.rb', line 38

def self.save(config, filename)
  cpy = config.clone
  if config[:password]
    cpy[:password] = Mongo::Jira::Config::encrypt('password',cpy[:password])
  else
    cpy.delete(:password)
  end
  cpy[:context_path] ||= ''

  File.open(filename , 'w') {|f| f.write(JSON.pretty_generate(cpy)) }
end

Instance Method Details

#password=(pw) ⇒ Object



84
85
86
# File 'lib/mongo/jira/config.rb', line 84

def password=(pw)
  config[:password]=pw
end

#password?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/mongo/jira/config.rb', line 80

def password?
  config[:password]
end

#savetrue, false

save the config file

Examples:

write the config file

Mongo::Jira::Config::check()
Mongo::Jira::Config::check("/tmp/.jira.json")

Parameters:

  • filename (String)

    The config file path. It defaults to “#'HOME'/.mongo_jira.json”

Returns:

  • (true, false)

    If the config is ok.

Since:

  • 0.0.1



76
77
78
# File 'lib/mongo/jira/config.rb', line 76

def save
  Mongo::Jira::Config::save(config,filename)
end