Class: I2PLookuper::SAMLookuper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "127.0.0.1", port = 7656, samhelper = SAMHelper) ⇒ SAMLookuper

Returns a new instance of SAMLookuper.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/i2plookuper.rb', line 100

def initialize host = "127.0.0.1", port = 7656, samhelper = SAMHelper
  @samhelper = SAMHelper
  @sock = TCPSocket.new host, port
  @status = :connected

  @sock.puts @samhelper.samcmd "HELLO", "VERSION", {
    "MIN" => "1.0"
  }
  res = @samhelper.parsesamcmd @sock.gets.chomp
  @status = "connectedand#{res[2]["RESULT"].downcase}".to_sym
  @version = res[2]["VERSION"]

  at_exit { self.close }
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



98
99
100
# File 'lib/i2plookuper.rb', line 98

def status
  @status
end

#versionObject (readonly)

Returns the value of attribute version.



98
99
100
# File 'lib/i2plookuper.rb', line 98

def version
  @version
end

Instance Method Details

#closeObject



115
116
117
118
119
120
121
# File 'lib/i2plookuper.rb', line 115

def close
  if @status != :disconnected
    @sock.puts @samhelper.samcmd "QUIT"
    @sock.close
    @status = :disconnected
  end
end

#lookup(name) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/i2plookuper.rb', line 123

def lookup name
  oldstatus = @status
  @status = :lookup

  @sock.puts @samhelper.samcmd "NAMING", "LOOKUP", {
    "NAME" => name
  }

  res = @samhelper.parsesamcmd @sock.gets

  @status = oldstatus

  return res[2]["RESULT"].downcase.to_sym, res[2].has_key?("VALUE") ? res[2]["VALUE"] : res[2]["RESULT"].tr("_", " ")
end