Class: NetrcReader
- Inherits:
-
Object
- Object
- NetrcReader
- Defined in:
- lib/netrc_reader.rb
Defined Under Namespace
Classes: Machine
Constant Summary collapse
- VERSION =
'0.1.1'- Error =
Class.new(StandardError)
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #config(name) ⇒ Object
- #config!(name) ⇒ Object
- #find!(name) ⇒ Object
- #home_path ⇒ Object
-
#initialize(path = nil) ⇒ NetrcReader
constructor
A new instance of NetrcReader.
- #machine_names ⇒ Object
- #netrc_file(path = nil) ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ NetrcReader
Returns a new instance of NetrcReader.
4 5 6 7 |
# File 'lib/netrc_reader.rb', line 4 def initialize(path = nil) @path = netrc_file(path) @data = IO.readlines(@path) end |
Class Method Details
.read(path = nil) ⇒ Object
9 10 11 |
# File 'lib/netrc_reader.rb', line 9 def self.read(path = nil) new(path) end |
Instance Method Details
#[](name) ⇒ Object
17 18 19 |
# File 'lib/netrc_reader.rb', line 17 def [](name) Machine.new(name, config(name)) if machine_names.include?(name) end |
#config(name) ⇒ Object
33 34 35 |
# File 'lib/netrc_reader.rb', line 33 def config(name) @data.find { |l| l.match(/machine #{name} login.*/) } end |
#config!(name) ⇒ Object
37 38 39 |
# File 'lib/netrc_reader.rb', line 37 def config!(name) config(name) || fail(NetrcReader::Error, "#{name} was not found: #{@path}") end |
#find!(name) ⇒ Object
21 22 23 |
# File 'lib/netrc_reader.rb', line 21 def find!(name) Machine.new(name, config!(name)) end |
#home_path ⇒ Object
29 30 31 |
# File 'lib/netrc_reader.rb', line 29 def home_path Dir.respond_to?(:home) ? Dir.home : ENV['HOME'] end |
#machine_names ⇒ Object
13 14 15 |
# File 'lib/netrc_reader.rb', line 13 def machine_names @data.map { |l| l.match(/machine (.*) login.*/)[1] } end |
#netrc_file(path = nil) ⇒ Object
25 26 27 |
# File 'lib/netrc_reader.rb', line 25 def netrc_file(path = nil) File.(path || File.join(ENV['NETRC'] || home_path, '.netrc')) end |