15
15
# limitations under the License.
16
16
####
17
17
18
+ require 'rubygems'
19
+ require 'slop'
18
20
require 'open-uri'
19
21
require 'date'
20
22
require 'json'
@@ -65,28 +67,56 @@ def find_request(api, item)
65
67
def add_request_to_item ( api , cookie , item , newreq )
66
68
itemid = item [ 'id' ]
67
69
option_hash = { content_type : :json , accept : :json , cookies : cookie }
68
- res = api [ "items/#{ itemid } /requests" ] . post newreq . to_json , option_hash
69
- #twit = 11
70
+ begin
71
+ res = api [ "items/#{ itemid } /requests" ] . post newreq . to_json , option_hash
72
+ rescue => e
73
+ $logger. error "add_request_to_item => exception #{ e . class . name } : #{ e . message } "
74
+ if ( ej = JSON . parse ( e . response ) ) && ( eje = ej [ 'errors' ] )
75
+ eje . each do |k , v |
76
+ $logger. error "#{ k } : #{ v . first } "
77
+ end
78
+ exit ( 1 )
79
+ end
80
+ end
70
81
end
71
82
72
83
####
73
84
# Delete a request which is attached to an item
74
85
####
75
86
def delete_request ( api , cookie , item , request )
76
87
option_hash = { accept : :json , cookies : cookie }
77
- res = api [ "items/#{ item [ 'id' ] } /requests/#{ request [ 'id' ] } " ] . delete option_hash
78
- twit = 13
88
+ begin
89
+ res = api [ "items/#{ item [ 'id' ] } /requests/#{ request [ 'id' ] } " ] . delete option_hash
90
+ rescue => e
91
+ $logger. error "delete_request => exception #{ e . class . name } : #{ e . message } "
92
+ if ( ej = JSON . parse ( e . response ) ) && ( eje = ej [ 'errors' ] )
93
+ eje . each do |k , v |
94
+ $logger. error "#{ k } : #{ v . first } "
95
+ end
96
+ exit ( 1 )
97
+ end
98
+ end
79
99
end
80
100
81
101
####
82
102
# Create a new item and add a request to it
83
103
####
84
104
def add_new_item ( api , cookie , number , subject , newreq )
85
105
item = nil
86
- option_hash = { content_type : :json , accept : :json , cookies : cookie }
87
- newitem = { number : number , clause : newreq [ 'clauseno' ] , date : newreq [ 'date' ] , standard : newreq [ 'standard' ] ,
88
- subject : subject }
89
- res = api [ "items" ] . post newitem . to_json , option_hash
106
+ option_hash = { content_type : :json , accept : :json , cookies : cookie }
107
+ newitem = { number : number , clause : newreq [ 'clauseno' ] , date : newreq [ 'date' ] , standard : newreq [ 'standard' ] ,
108
+ subject : subject }
109
+ begin
110
+ res = api [ "items" ] . post newitem . to_json , option_hash
111
+ rescue => e
112
+ $logger. error "add_new_item => exception #{ e . class . name } : #{ e . message } "
113
+ if ( ej = JSON . parse ( e . response ) ) && ( eje = ej [ 'errors' ] )
114
+ eje . each do |k , v |
115
+ $logger. error "#{ k } : #{ v . first } "
116
+ end
117
+ exit ( 1 )
118
+ end
119
+ end
90
120
if res . code == 201
91
121
item = JSON . parse ( res . body )
92
122
reqres = add_request_to_item ( api , cookie , item , newreq )
@@ -196,18 +226,25 @@ def parse_request(url, creds)
196
226
end
197
227
198
228
begin
199
- config = YAML . load ( File . read ( "secrets.yml" ) )
229
+ opts = Slop . parse do |o |
230
+ o . string '-s' , '--secrets' , 'secrets YAML file name' , default : 'secrets.yml'
231
+ o . bool '-d' , '--debug' , 'debug mode'
232
+ o . bool '-a' , '--all' , 'don\'t stop at first already-existing item'
233
+ o . on '--help' do
234
+ STDERR . puts o
235
+ exit
236
+ end
237
+ end
238
+ config = YAML . load ( File . read ( opts [ :secrets ] ) )
239
+
240
+ # Set up logging
241
+ $DEBUG = opts . debug?
242
+ $logger = Logger . new ( STDERR )
243
+ $logger. level = Logger ::INFO
244
+ $logger. level = Logger ::DEBUG if $DEBUG
200
245
#
201
246
# Log in to the 802.1 Maintenance Database
202
247
#
203
- #$DEBUG = false
204
- # If $PROCESS_ALL_ITEMS is false, the script will only process new items, stopping when it finds the
205
- # first item that already exists in the database.
206
- $PROCESS_ALL_ITEMS = false
207
- $logger = Logger . new ( STDOUT )
208
- $logger. level = Logger ::INFO
209
- $logger. level = Logger ::DEBUG if $DEBUG
210
-
211
248
if $DEBUG
212
249
RestClient . proxy = "http://localhost:8888"
213
250
$logger. debug ( "Using HTTP proxy #{ RestClient . proxy } " )
@@ -218,7 +255,6 @@ def parse_request(url, creds)
218
255
# Save the session cookie
219
256
maint_cookie = { }
220
257
res . cookies . each { |ck | maint_cookie [ ck [ 0 ] ] = ck [ 1 ] if /_session/ . match ( ck [ 0 ] ) }
221
- #twit=10
222
258
223
259
#
224
260
# Parse each index page of the 802.1 Maintenance Reflector Archive
@@ -269,7 +305,7 @@ def parse_request(url, creds)
269
305
item = find_item ( maint , number )
270
306
request = { }
271
307
if item
272
- unless $PROCESS_ALL_ITEMS
308
+ unless opts [ :all ]
273
309
$logger. info 'Stopping at first already-existing item'
274
310
throw :done
275
311
end
@@ -303,8 +339,6 @@ def parse_request(url, creds)
303
339
end
304
340
end
305
341
end
306
-
307
- twit = 8
308
342
end
309
343
nextpage = pagedoc . search ( 'tr' ) [ 1 ] . children . search ( 'td' ) . children [ 4 ] . attributes [ 'href' ]
310
344
em_arch_url = nextpage ? config [ 'email_archive' ] + '/' + nextpage . value : nil
0 commit comments