Class: ReqWrap::Environment
- Inherits:
-
Object
- Object
- ReqWrap::Environment
- Defined in:
- lib/req_wrap/environment.rb
Overview
Request environment
Constant Summary collapse
- PASSWORD_FILE =
'.reqwrap_password'- ENCRYPTED_ENV_FILE_EXT =
'.enc'
Class Method Summary collapse
Instance Method Summary collapse
- #change(editor) ⇒ Object
- #delete ⇒ Object
- #encrypt(delete_original: false) ⇒ Object
- #encrypted? ⇒ Boolean
-
#initialize(env_file = nil) ⇒ Environment
constructor
A new instance of Environment.
- #load ⇒ Object
- #read ⇒ Object
- #write(content) ⇒ Object
Constructor Details
#initialize(env_file = nil) ⇒ Environment
Returns a new instance of Environment.
24 25 26 |
# File 'lib/req_wrap/environment.rb', line 24 def initialize(env_file = nil) @env_file = env_file || default_env_file end |
Class Method Details
.generate_password_file ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/req_wrap/environment.rb', line 16 def self.generate_password_file path = File.("./#{PASSWORD_FILE}") raise ArgumentError, 'Password file already exists' if File.exist?(path) File.write(path, ActiveSupport::EncryptedFile.generate_key) path end |
Instance Method Details
#change(editor) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/req_wrap/environment.rb', line 61 def change(editor) return change_encrypted_environment(editor) if encrypted? Tempfile.create(['', "-#{@env_file}"]) do |tmp_file| original_content = read tmp_file.write(original_content) tmp_file.flush launch_external_editor(editor, tmp_file.path) tmp_file.rewind new_content = tmp_file.read write(new_content) if original_content != new_content end end |
#delete ⇒ Object
50 51 52 |
# File 'lib/req_wrap/environment.rb', line 50 def delete File.delete(@env_file) if File.exist?(@env_file) end |
#encrypt(delete_original: false) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/req_wrap/environment.rb', line 54 def encrypt(delete_original: false) validate(raise_error: true) Environment.new("#{@env_file}#{ENCRYPTED_ENV_FILE_EXT}").write(read) delete if delete_original end |
#encrypted? ⇒ Boolean
28 29 30 |
# File 'lib/req_wrap/environment.rb', line 28 def encrypted? @env_file.end_with?(ENCRYPTED_ENV_FILE_EXT) end |
#load ⇒ Object
32 33 34 35 36 |
# File 'lib/req_wrap/environment.rb', line 32 def load return unless validate load_str_environment(read) end |
#read ⇒ Object
38 39 40 41 42 |
# File 'lib/req_wrap/environment.rb', line 38 def read return decrypt if encrypted? File.read(@env_file, mode: 'rb:BOM|utf-8') end |
#write(content) ⇒ Object
44 45 46 47 48 |
# File 'lib/req_wrap/environment.rb', line 44 def write(content) return encrypted_file_for(@env_file).write(content) if encrypted? File.write(@env_file, content) end |