iosparse 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/iosparse.gemspec +3 -2
  2. data/lib/iosparse.rb +22 -18
  3. metadata +4 -3
data/iosparse.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "iosparse"
5
- s.version = "0.0.4"
5
+ s.version = "0.0.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Ari Mizrahi"]
9
- s.date = "2013-05-01"
9
+ s.date = "2013-05-02"
10
10
  s.description = "Parse Cisco IOS configurations"
11
11
  s.email = "codemunchies@gmail.com"
12
12
  s.files = ["lib/iosparse.rb", "iosparse.gemspec", "Gemfile", "README.md"]
@@ -14,5 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.homepage = "http://github.com/codemunchies/iosparse"
15
15
  s.rubygems_version = "1.8.25"
16
16
  s.summary = "Parse Cisco IOS configurations"
17
+ s.license = "GPL-3"
17
18
  end
18
19
 
data/lib/iosparse.rb CHANGED
@@ -7,11 +7,18 @@ class IOSParse
7
7
  # Store all blocks in the configuration
8
8
  @blocks = Array.new
9
9
  # Load the file
10
- config = File.open(filename, 'r')
11
- # Stream IO.read the file and scan for blocks, lines inbetween "!"
12
- IO.read(config).scan(/(^[^!]+)/).each do |block|
13
- # Push the blocks to the @blocks array
14
- @blocks << block
10
+ begin
11
+ config = File.open(filename, 'r')
12
+ # Stream IO.read the file and scan for blocks, lines inbetween "!"
13
+ IO.read(config).scan(/(^[^!]+)/).each do |block|
14
+ # Push the blocks to the @blocks array
15
+ @blocks << block
16
+ end
17
+ rescue
18
+ # TODO: raise an error here 'bad file or path'
19
+ ensure
20
+ # Make sure we close the file
21
+ config.close
15
22
  end
16
23
  end
17
24
 
@@ -68,7 +75,7 @@ class IOSParse
68
75
  #
69
76
  # Find all object-groups
70
77
  #
71
- def object_group
78
+ def object_groups
72
79
  # Regex to parse out object-groups
73
80
  group = /object-group[\s\S]+/
74
81
  # Call find_blocks() passing regex, parent name, and parse boolean
@@ -85,7 +92,7 @@ class IOSParse
85
92
  output = Array.new
86
93
  @blocks.each do |block|
87
94
  # Use regex to filter via scan() and flatten then push to array
88
- output << block.to_s.scan(filter).flatten if block.to_s.include?(parent)
95
+ output << normalize_block(block.to_s.scan(filter).flatten) if block.to_s.include?(parent)
89
96
  end
90
97
  # Some parents are not within "!" blocks and require some extra work
91
98
  if parse == true then
@@ -107,7 +114,7 @@ class IOSParse
107
114
  block.to_s.match(filter).to_s.split('\n').each do |line|
108
115
  # Skip unless the parent is in the line
109
116
  next unless line.match(filter)
110
- # Push matches to array
117
+ # Push matches to array and normalize the matched lines
111
118
  output << normalize_line(line.match(filter))
112
119
  end
113
120
  end
@@ -152,22 +159,19 @@ class IOSParse
152
159
 
153
160
  #
154
161
  # Normalize block to standardize returned arrays
162
+ # All new line characters should be '\n'
155
163
  #
156
- def normalize_block(block)
157
- # Array to store normalized data
158
- output = Array.new
159
- block.each do |data|
160
- # Normalize data
161
- output << "[\"#{data}\\\\n\"]"
162
- end
163
- # Return the normalized array
164
- output
164
+ def normalize_block(data)
165
+ # Return normalized data
166
+ data.to_s.gsub("\\\\n", '\n') if data.to_s.include?('\\\\n')
165
167
  end
166
168
 
167
169
  #
168
170
  # Normalize line to standardize returned arrays
171
+ # All new line characters should be '\n'
172
+ #
169
173
  def normalize_line(data)
170
174
  # Return normalized data
171
- "[\"#{data}\\\\n\"]"
175
+ "[\"#{data}\\n\"]" unless data.to_s.include?('\n')
172
176
  end
173
177
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iosparse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-01 00:00:00.000000000 Z
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Parse Cisco IOS configurations
15
15
  email: codemunchies@gmail.com
@@ -22,7 +22,8 @@ files:
22
22
  - Gemfile
23
23
  - README.md
24
24
  homepage: http://github.com/codemunchies/iosparse
25
- licenses: []
25
+ licenses:
26
+ - GPL-3
26
27
  post_install_message:
27
28
  rdoc_options: []
28
29
  require_paths: