Class: Netrcx
- Inherits:
-
Object
- Object
- Netrcx
- Defined in:
- lib/netrcx.rb
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Class Method Summary collapse
- .default_path ⇒ Object
-
.read(path = default_path) ⇒ Object
Read from a file path.
Instance Method Summary collapse
-
#[](host) ⇒ Netrcx::Entry
Entry for the host.
-
#default ⇒ Netrcx::Entry
Default entry.
-
#initialize(raw) ⇒ Netrcx
constructor
A new instance of Netrcx.
Constructor Details
#initialize(raw) ⇒ Netrcx
Returns a new instance of Netrcx.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/netrcx.rb', line 32 def initialize(raw) @entries = [] current = nil lastword = nil raw.each_line do |line| Shellwords.split(line).each do |word| break if word.start_with?('#') if word == 'default' @entries.push(current) if current current = Entry.new(default: true) elsif word == 'machine' @entries.push(current) if current current = Entry.new(default: false) elsif !current.nil? set_value(current, lastword, word) end lastword = word end end @entries.push(current) if current end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
30 31 32 |
# File 'lib/netrcx.rb', line 30 def entries @entries end |
Class Method Details
.default_path ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/netrcx.rb', line 11 def self.default_path home = ENV['NETRC'] home ||= Dir.home if Dir.respond_to?(:home) home ||= ENV['HOME'] if /mswin|mingw/.match?(RbConfig::CONFIG['host_os']) home ||= File.join(ENV['HOMEDRIVE'], ENV['HOMEPATH']) if ENV['HOMEDRIVE'] && ENV['HOMEPATH'] home ||= ENV['USERPROFILE'] File.join home, '_netrc' else File.join home, '.netrc' end end |
.read(path = default_path) ⇒ Object
Read from a file path.
26 27 28 |
# File 'lib/netrcx.rb', line 26 def self.read(path = default_path) File.open(path) {|io| new(io) } end |
Instance Method Details
#[](host) ⇒ Netrcx::Entry
Returns entry for the host.
63 64 65 66 |
# File 'lib/netrcx.rb', line 63 def [](host) host = host.strip entries.detect {|m| m.host == host } end |
#default ⇒ Netrcx::Entry
Returns default entry.
58 59 60 |
# File 'lib/netrcx.rb', line 58 def default entries.detect(&:default) end |