d2e 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +125 -17
  3. data/lib/d2e.rb +35 -15
  4. data/lib/d2e/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebf25325129055695b160e58c493df766f0787e8
4
- data.tar.gz: f0415f0a3107a3152b7f00e2edac610b8d49582d
3
+ metadata.gz: db5e19dcaada1ee97b3d8b8b946da212d55f4205
4
+ data.tar.gz: accd01fc39e29419877ea3c960b73629d5bf39d3
5
5
  SHA512:
6
- metadata.gz: 73592465f0eab3aa88ce550150f036966000a6a518c548e3f485c6bf9435e41606e4eb4187f7bc1ea002ad523d60ed9a7aa3e1611f386b7095ec67f35c0ef27d
7
- data.tar.gz: 48e1f6036b27a5c85ef5907565a8dfb9ea1846464ecf4c41ec141f5520e050435af86cc465ca53780597b56edbc0a2244f676cff5c51d96b90ede52a0019eb05
6
+ metadata.gz: 6fcd3df8538f7cff766ba4c4e27ed40c0abb844f8e49166a9f75c33f8d9903a19876ba7ca41b6e7579c9f492ac214102e5a303c27e014431b3908c05b9abff0c
7
+ data.tar.gz: 4004690f7443b32516399c2e4a91abd3a62812840fe083cfebe20f2645868f1213b18f5726984e824d50f83bd3ba01f58e4aeae80a48613fb9ccc9f7b1a84620
data/README.md CHANGED
@@ -2,22 +2,10 @@
2
2
 
3
3
  Utility for converting diff to events.
4
4
 
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'd2e'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install d2e
18
-
19
5
  ## Usage
20
6
 
7
+ ### Basic
8
+
21
9
  ```
22
10
  require 'd2e'
23
11
  require 'json'
@@ -35,8 +23,8 @@ curr = [
35
23
  {'id' => 6, 'name' => 'Biggie', 'description' => 'Rap'},
36
24
  ]
37
25
 
38
- d2e = D2E.new(key: 'id')
39
- events = d2e.diff(prev, curr)
26
+ d2e = D2E.new(id: 'id')
27
+ events = d2e.d2e(prev, curr)
40
28
  puts JSON.pretty_generate(events)
41
29
  ```
42
30
 
@@ -78,7 +66,9 @@ output:
78
66
  },
79
67
  {
80
68
  "type": "update",
81
- "id": 2,
69
+ "id": {
70
+ "id": 2
71
+ },
82
72
  "diff": {
83
73
  "description": [
84
74
  "Bass",
@@ -89,6 +79,124 @@ output:
89
79
  ]
90
80
  ```
91
81
 
82
+ ### Multiple Primary Keys
83
+
84
+ ```
85
+ prev = [
86
+ {'groupName' => 'web', 'ipProtocol' => 'TCP', 'inOut' => 'IN', 'fromPort' => 80, 'toPort' => 80, 'ipRange' => '0.0.0.0/0'},
87
+ {'groupName' => 'web', 'ipProtocol' => 'TCP', 'inOut' => 'IN', 'fromPort' => 443, 'toPort' => 443, 'ipRange' => '0.0.0.0/0'},
88
+ {'groupName' => 'mon', 'ipProtocol' => 'ICMP', 'inOut' => 'OUT', 'group' => 'web'},
89
+ ]
90
+ curr = [
91
+ {'groupName' => 'web', 'ipProtocol' => 'TCP', 'inOut' => 'IN', 'fromPort' => 443, 'toPort' => 443, 'ipRange' => '0.0.0.0/0'},
92
+ {'groupName' => 'mon', 'ipProtocol' => 'ICMP', 'inOut' => 'OUT', 'group' => 'web', 'description' => 'for ping monitoring'},
93
+ {'groupName' => 'app', 'ipProtocol' => 'TCP', 'inOut' => 'IN', 'fromPort' => 8080, 'toPort' => 8080, 'group' => 'web'},
94
+ ]
95
+
96
+ d2e = D2E.new(id: ['groupName', 'ipProtocol', 'inOut', 'fromPort', 'toPort', 'ipRange', 'group'])
97
+ events = d2e.d2e(prev, curr)
98
+ puts JSON.pretty_generate(events)
99
+ ```
100
+
101
+ output:
102
+
103
+ ```
104
+ [
105
+ {
106
+ "type": "create",
107
+ "item": {
108
+ "groupName": "app",
109
+ "ipProtocol": "TCP",
110
+ "inOut": "IN",
111
+ "fromPort": 8080,
112
+ "toPort": 8080,
113
+ "group": "web"
114
+ }
115
+ },
116
+ {
117
+ "type": "delete",
118
+ "item": {
119
+ "groupName": "web",
120
+ "ipProtocol": "TCP",
121
+ "inOut": "IN",
122
+ "fromPort": 80,
123
+ "toPort": 80,
124
+ "ipRange": "0.0.0.0/0"
125
+ }
126
+ },
127
+ {
128
+ "type": "update",
129
+ "id": {
130
+ "groupName": "mon",
131
+ "ipProtocol": "ICMP",
132
+ "inOut": "OUT",
133
+ "fromPort": null,
134
+ "toPort": null,
135
+ "ipRange": null,
136
+ "group": "web"
137
+ },
138
+ "diff": {
139
+ "description": [
140
+ null,
141
+ "for ping monitoring"
142
+ ]
143
+ }
144
+ }
145
+ ]
146
+ ```
147
+
148
+ ### Ignoring Specific Keys
149
+
150
+ ```
151
+ prev = [
152
+ {'groupName' => 'web', 'groupDescription' => 'Web Instances', 'ipPermissions' => nil},
153
+ {'groupName' => 'apl', 'groupDescription' => 'Apl Instances', 'ipPermissions' => nil},
154
+ {'groupName' => 'db', 'groupDescription' => 'DB Instances', 'ipPermissions' => nil},
155
+ ]
156
+ curr = [
157
+ {'groupName' => 'web', 'groupDescription' => 'Web Instances', 'ipPermissions' => {'item' => ['some ingresses']}},
158
+ {'groupName' => 'apl', 'groupDescription' => 'Apl Instances', 'ipPermissions' => {'item' => ['other ingresses']}},
159
+ {'groupName' => 'db', 'groupDescription' => 'Description changed', 'ipPermissions' => {'item' => ['blah blah']}},
160
+ ]
161
+
162
+ d2e = D2E.new(id: 'groupName', ignore: 'ipPermissions')
163
+ events = d2e.d2e(prev, curr)
164
+ puts JSON.pretty_generate(events)
165
+ ```
166
+
167
+ output:
168
+
169
+ ```
170
+ [
171
+ {
172
+ "type": "update",
173
+ "id": {
174
+ "groupName": "db"
175
+ },
176
+ "diff": {
177
+ "groupDescription": [
178
+ "DB Instances",
179
+ "Description changed"
180
+ ]
181
+ }
182
+ }
183
+ ]
184
+ ```
185
+
186
+ ## Installation
187
+
188
+ Add this line to your application's Gemfile:
189
+
190
+ gem 'd2e'
191
+
192
+ And then execute:
193
+
194
+ $ bundle
195
+
196
+ Or install it yourself as:
197
+
198
+ $ gem install d2e
199
+
92
200
  ## Contributing
93
201
 
94
202
  1. Fork it ( https://github.com/[my-github-username]/d2e/fork )
data/lib/d2e.rb CHANGED
@@ -2,34 +2,33 @@ require "d2e/version"
2
2
 
3
3
  class D2E
4
4
  def initialize(options)
5
- @key = options[:key]
5
+ @id = [options[:id]].flatten
6
+ if options[:ignore]
7
+ @ignore = [options[:ignore]].flatten
8
+ end
6
9
  end
7
10
 
8
- def diff(prev, curr)
11
+ def d2e(prev, curr)
9
12
  events = []
10
13
 
11
- prev_ids = prev.map {|item| item[@key] }
12
- curr_ids = curr.map {|item| item[@key] }
13
-
14
- deleted = prev_ids - curr_ids
15
- created = curr_ids - prev_ids
16
- updated = prev_ids & curr_ids
14
+ deleted = difference(prev, curr)
15
+ created = difference(curr, prev)
16
+ remained = intersection(prev, curr)
17
17
 
18
- created.each do |id|
19
- item = curr.find {|item| item[@key] == id }
18
+ created.each do |item|
20
19
  events << {'type' => 'create', 'item' => item}
21
20
  end
22
21
 
23
- deleted.each do |id|
24
- item = prev.find {|item| item[@key] == id }
22
+ deleted.each do |item|
25
23
  events << {'type' => 'delete', 'item' => item}
26
24
  end
27
25
 
28
- updated.each do |id|
26
+ remained.each do |prev_item, curr_item|
29
27
  diff = {}
30
- prev_item = prev.find {|item| item[@key] == id }
31
- curr_item = curr.find {|item| item[@key] == id }
32
28
  keys = prev_item.keys | curr_item.keys
29
+ if @ignore
30
+ keys = keys - @ignore
31
+ end
33
32
  keys.each do |key|
34
33
  prev_value = prev_item[key]
35
34
  curr_value = curr_item[key]
@@ -38,11 +37,32 @@ class D2E
38
37
  end
39
38
  end
40
39
  if !diff.empty?
40
+ id = Hash[@id.map {|id| [id, prev_item[id]] }]
41
41
  events << {'type' => 'update', 'id' => id, 'diff' => diff}
42
42
  end
43
43
  end
44
44
 
45
45
  events
46
46
  end
47
+
48
+ def difference(list_a, list_b)
49
+ list = []
50
+ list_a.each do |item_a|
51
+ if !list_b.find {|item_b| @id.all? {|id| item_a[id] == item_b[id] } }
52
+ list << item_a
53
+ end
54
+ end
55
+ list
56
+ end
57
+
58
+ def intersection(list_a, list_b)
59
+ list = []
60
+ list_a.each do |item_a|
61
+ if item_b = list_b.find {|item_b| @id.all? {|id| item_a[id] == item_b[id] } }
62
+ list << [item_a, item_b]
63
+ end
64
+ end
65
+ list
66
+ end
47
67
  end
48
68
 
@@ -1,3 +1,3 @@
1
1
  class D2E
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: d2e
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - tily
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-28 00:00:00.000000000 Z
11
+ date: 2015-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler