Module: Pitcgi

Defined in:
lib/pitcgi.rb,
lib/pitcgi/version.rb

Constant Summary collapse

NOT_TO_BE_INITIALIZED =
"It is likely not to be initialized. Use '#{NAME} init'."
ALREADY_SCRAMBLED =
'Already scrambled.'
NOT_SCRAMBLED =
'Not scrambled.'
CAN_NOT_USE_PROFILE_NAME =
'Can not use it for profile name.'
Directory =
Pathname.new("/etc/pitcgi").expand_path
NAME =
'pitcgi'
VERSION =
'0.1.0'
@@config_path =
Directory + "pitcgi.yaml"
@@profile_path =
Directory + "default.yaml"

Class Method Summary collapse

Class Method Details

.descrambleObject

Descramble profile.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pitcgi.rb', line 99

def self.descramble
  config = self.load_config
  config_scrambled = self.get_profile_config( config )
  if !config_scrambled[ 'scrambled' ]
    raise( NOT_SCRAMBLED )
  end
  ScrambledEggs.new.descramble_file( @@profile_path )
  config_scrambled[ 'scrambled' ] = false
  config[ get_profile_config_name( config ) ] = config_scrambled
  self.save_config( config )
  return
end

.get(name, opts = {}) ⇒ Object

If not opts specified, this just returns setting from current profile. If require specified, check keys on setting and open $EDITOR.



50
51
52
53
54
55
56
57
58
59
# File 'lib/pitcgi.rb', line 50

def self.get(name, opts={})
	ret = self.load[name] || {}
	if opts[:require]
		unless opts[:require].keys.all? {|k| ret[k] != nil }
			ret = opts[:require].update(ret)
			ret = self.set(name, :config => ret)
		end
	end
	ret || {"username"=>"", "password"=>""}
end

.scrambleObject

Scramble profile.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pitcgi.rb', line 85

def self.scramble
  config = self.load_config
  config_scrambled = self.get_profile_config( config )
  if config_scrambled[ 'scrambled' ]
    raise( ALREADY_SCRAMBLED )
  end
  ScrambledEggs.new.scramble_file( @@profile_path )
  config_scrambled[ 'scrambled' ] = true
  config[ get_profile_config_name( config ) ] = config_scrambled
  self.save_config( config )
  return
end

.set(name, opts = {}) ⇒ Object

hash and current profile.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pitcgi.rb', line 21

def self.set(name, opts={})
	profile = self.load
	if opts.key?(:data)
		result = opts[:data]
	else
		if ENV["EDITOR"].nil? || !$stdout.tty?
			return {}
		end
		c = (opts[:config] || self.get(name)).to_yaml
		t = Tempfile.new("pitcgi")
		t << c
		t.close
		system(ENV["EDITOR"], t.path)
		t.open
		result = t.read
		if result == c
			warn "No Changes"
			return profile[name]
		end
		result = YAML.load(result)
	end
	profile[name] = result
   self.save( profile )
   result
end

.switch(name, opts = {}) ⇒ Object

Switch to current profile to name. Profile is set of settings. You can switch some settings using profile.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pitcgi.rb', line 63

def self.switch(name, opts={})
	@@profile_path = Directory + "#{name}.yaml"
   if @@profile_path == @@config_path
     raise( CAN_NOT_USE_PROFILE_NAME )
   end
	begin
     config = self.load_config
     ret = config["profile"]
   rescue Errno::ENOENT
     config = {}
     ret = ""
   end
	config["profile"] = name
	begin
     self.save_config( config )
   rescue Errno::ENOENT => e
     raise e, NOT_TO_BE_INITIALIZED
   end
   ret
end