Class: RgRefresh::Netgear
- Inherits:
-
Object
- Object
- RgRefresh::Netgear
- Defined in:
- lib/rg_refresh/netgear.rb
Constant Summary collapse
- LoginRequired =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#jar ⇒ Object
readonly
Returns the value of attribute jar.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#ports_vlans ⇒ Object
readonly
Returns the value of attribute ports_vlans.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
Instance Method Summary collapse
- #finish ⇒ Object
-
#initialize(opts) ⇒ Netgear
constructor
A new instance of Netgear.
- #login ⇒ Object
- #sync ⇒ Object
- #transition_to(mode) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Netgear
Returns a new instance of Netgear.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rg_refresh/netgear.rb', line 15 def initialize(opts) @base_uri = URI(opts.fetch(:address)) @http = Net::HTTP.new(@base_uri.host, @base_uri.port) @password = opts.fetch(:password) @jar = HTTP::CookieJar.new @ports_vlans = opts.fetch(:ports_vlans).each_value do |arr| arr.map! do |vlan| if (vlan_name = vlan.to_s[/^\.(.+)/, 1]) opts[:vlans].fetch(vlan_name.to_sym) else vlan end end end @hash = nil @state = [] end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
10 11 12 |
# File 'lib/rg_refresh/netgear.rb', line 10 def base_uri @base_uri end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
10 11 12 |
# File 'lib/rg_refresh/netgear.rb', line 10 def hash @hash end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
10 11 12 |
# File 'lib/rg_refresh/netgear.rb', line 10 def http @http end |
#jar ⇒ Object (readonly)
Returns the value of attribute jar.
10 11 12 |
# File 'lib/rg_refresh/netgear.rb', line 10 def jar @jar end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
10 11 12 |
# File 'lib/rg_refresh/netgear.rb', line 10 def password @password end |
#ports_vlans ⇒ Object (readonly)
Returns the value of attribute ports_vlans.
10 11 12 |
# File 'lib/rg_refresh/netgear.rb', line 10 def ports_vlans @ports_vlans end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
10 11 12 |
# File 'lib/rg_refresh/netgear.rb', line 10 def state @state end |
Class Method Details
.start(opts) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/rg_refresh/netgear.rb', line 35 def self.start(opts) new(opts).tap do |netgear| netgear.login netgear.sync end end |
Instance Method Details
#finish ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rg_refresh/netgear.rb', line 81 def finish begin request('/logout.cgi') rescue LoginRequired end begin http.finish rescue IOError end end |
#login ⇒ Object
42 43 44 45 46 47 |
# File 'lib/rg_refresh/netgear.rb', line 42 def login res = post('/login.cgi', { :password=>password }) fail 'Maximum sessions reached' \ if res.body.include?('Maximum sessions reached') end |
#sync ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rg_refresh/netgear.rb', line 49 def sync res = request('/8021qBasic.cgi') parse_inputs(res.body, /hash/, /port\d+/) do |name, value| if name == 'hash' @hash = value elsif name.sub!(/^port/, '') @state[name.to_i] = value.to_i end end end |
#transition_to(mode) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rg_refresh/netgear.rb', line 61 def transition_to(mode) arr = ports_vlans.fetch(mode) new_state = state .map .with_index {|vlan, port| arr[port] || vlan } form_data = new_state .map .with_index {|vlan, port| ["port#{port}".to_sym, vlan] } .to_h .merge!(:status=>'Enable', :hash=>hash) post('/8021qBasic.cgi', form_data) sync fail 'Unable to set VLANs' unless state == new_state end |