Class: RgRefresh::Netgear

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

Constant Summary collapse

LoginRequired =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_uriObject (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

#hashObject (readonly)

Returns the value of attribute hash.



10
11
12
# File 'lib/rg_refresh/netgear.rb', line 10

def hash
  @hash
end

#httpObject (readonly)

Returns the value of attribute http.



10
11
12
# File 'lib/rg_refresh/netgear.rb', line 10

def http
  @http
end

#jarObject (readonly)

Returns the value of attribute jar.



10
11
12
# File 'lib/rg_refresh/netgear.rb', line 10

def jar
  @jar
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/rg_refresh/netgear.rb', line 10

def password
  @password
end

#ports_vlansObject (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

#stateObject (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.
    netgear.sync
  end
end

Instance Method Details

#finishObject



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

#loginObject



42
43
44
45
46
47
# File 'lib/rg_refresh/netgear.rb', line 42

def 
  res = post('/login.cgi', { :password=>password })

  fail 'Maximum sessions reached' \
    if res.body.include?('Maximum sessions reached')
end

#syncObject



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