Class: NetrcReader

Inherits:
Object
  • Object
show all
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

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_pathObject



29
30
31
# File 'lib/netrc_reader.rb', line 29

def home_path
  Dir.respond_to?(:home) ? Dir.home : ENV['HOME']
end

#machine_namesObject



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.expand_path(path || File.join(ENV['NETRC'] || home_path, '.netrc'))
end