Class: AstDB::Connection
- Inherits:
-
Object
- Object
- AstDB::Connection
- Defined in:
- lib/astdb/models/connection.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #fetch_database ⇒ Object
-
#initialize(opts = {}) ⇒ Connection
constructor
A new instance of Connection.
- #set_database_values(value_hash) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 |
# File 'lib/astdb/models/connection.rb', line 7 def initialize(opts = {}) @host = opts[:host] @username = opts[:username] @password = opts[:password] end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/astdb/models/connection.rb', line 5 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
5 6 7 |
# File 'lib/astdb/models/connection.rb', line 5 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
5 6 7 |
# File 'lib/astdb/models/connection.rb', line 5 def username @username end |
Instance Method Details
#fetch_database ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/astdb/models/connection.rb', line 13 def fetch_database # fetch database dump from asterisk server output = nil Net::SSH.start(@host, @username, password: @password) do |ssh| output = ssh.exec!("asterisk -rx 'database show'") end # parse dump to hash database = {} lines = output.split("\n").collect { |l| l.rstrip } lines.each do |line| m = line.match /^\/(?<key>[^:]+):(?<value>.+)$/ next if m.nil? key = m[:key].rstrip value = m[:value].lstrip database[key] = value end database end |
#set_database_values(value_hash) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/astdb/models/connection.rb', line 40 def set_database_values(value_hash) # prepare command commands = value_hash.collect { |k, v| Entry.new(k,v).put_command } # send to server errors = {} Net::SSH.start(@host, @username, password: @password) do |ssh| value_hash.each do |key, value| command = Entry.new(key,value).put_command response = ssh.exec!("asterisk -rx '#{command}'") errors[key] = response unless response.match /Updated database successfully/ end end return errors.count > 0 ? errors : nil end |