Class: Netrcx

Inherits:
Object
  • Object
show all
Defined in:
lib/netrcx.rb

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#entriesObject (readonly)

Returns the value of attribute entries.



30
31
32
# File 'lib/netrcx.rb', line 30

def entries
  @entries
end

Class Method Details

.default_pathObject



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.

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

#defaultNetrcx::Entry

Returns default entry.

Returns:

  • default entry



58
59
60
# File 'lib/netrcx.rb', line 58

def default
  entries.detect(&:default)
end