govuk-registers-api-client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +13 -0
  3. data/LICENSE +21 -0
  4. data/README.md +805 -0
  5. data/lib/register_client_manager.rb +61 -0
  6. metadata +64 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 959a3fe6555fd80a48da2138aadaa41877e2c3c406a6688fac9f994805f4500d
4
+ data.tar.gz: 1a7499afdcfa437839871d149d133b3d8f3ea428539c009ed1632188373c7d0a
5
+ SHA512:
6
+ metadata.gz: 29934a7dd3ebea408e6d2507d9c05d97c005a4d7a1f338266cf9399a2c6749fe261a9e3b68bdf07fc8a2bf13ea6b928f4775bc93fadbbc3dcdad5651c4ecd109
7
+ data.tar.gz: b6eaec466707feb72a7a2a78182542d001729d76115c3d252e6cf4c93041d2a2822e9c084a771083cbb877e2956640c1788b8780d90acc03b9cfb2f06b384aa1
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ group :development do
5
+ gem 'rake'
6
+ end
7
+
8
+ gem 'rest-client', '>= 2.0.1'
9
+
10
+ group :development, :test do
11
+ gem 'rspec', '~> 3.7'
12
+ gem 'webmock'
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Crown Copyright (Government Digital Service)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,805 @@
1
+ # GOV.UK Registers Ruby Client
2
+
3
+ You can use this Ruby client to integrate your service with [GOV.UK Registers](https://registers.cloudapps.digital/).
4
+
5
+ Registers are authoritative lists of information. The data is owned by [custodians](https://www.gov.uk/government/collections/registers-guidance#creating-and-managing-registers) inside departments and services. For example, the [Country register](https://country.register.gov.uk/) is maintained by a custodian in the Foreign and Commonwealth Office (FCO).
6
+
7
+ ## Table of Contents
8
+
9
+ - [Installation](#installation)
10
+ - [Get started](#get-started)
11
+ - [Reference](#reference)
12
+ * [`RegisterClientManager`](#registerclientmanager)
13
+ * [`RegisterClient`](#registerclient)
14
+ * [Collections](#collections)
15
+ + [EntryCollection](#entrycollection)
16
+ + [Entry](#entry)
17
+ + [ItemCollection](#itemcollection)
18
+ + [Item](#item)
19
+ + [RecordCollection](#recordcollection)
20
+ + [Record](#record)
21
+ + [RecordMapCollection](#recordmapcollection)
22
+
23
+ ## Installation
24
+
25
+ In your Gemfile, add:
26
+ ```
27
+ gem 'govuk-register-api-client'
28
+ ```
29
+
30
+ ## Get started
31
+
32
+ The `RegisterClientManager` is the entry point of the Registers Ruby client:
33
+
34
+ ```
35
+ require 'register_client_manager'
36
+
37
+ registers_client = RegistersClient::RegisterClientManager.new({
38
+ page_size: 10
39
+ })
40
+ ```
41
+
42
+ The `RegisterClientManager` maintains individual instances of [`RegisterClient`](#registerclient) for each register you access via the [`get_register`](#getregister) method.
43
+
44
+ When creating a new `RegisterClientManager`, you can pass a configuration object to specify the following:
45
+ - `page_size`: number of results returned per page when using the `page` method of any of the collection classes (see below for more information) - default is `100`
46
+
47
+ ## Reference
48
+
49
+ ### <a id="registerclientmanager"></a>`RegisterClientManager`
50
+
51
+ ##### <a id="getregister"></a>`get_register(register, phase, data_store = nil)`
52
+
53
+ Gets the `RegisterClient` instance for the given `register` name and `phase`.
54
+
55
+ The `data_store` parameter specifies the data store to use accessing a particular register. You can omit this parameter, which will make it default to the `InMemoryDataStore` value. You can also create a custom data store to include the `DataStore` module and to implement the methods it defines. For example, to insert register data directly into your Postgres database.
56
+
57
+ <details>
58
+ <summary>
59
+ Example use (click here to expand):
60
+ </summary>
61
+
62
+ ```
63
+
64
+ registers_client.get_register('country', 'beta', nil)
65
+
66
+ ```
67
+ </details>
68
+ <details>
69
+ <summary>
70
+ Expected output (click here to expand):
71
+ </summary>
72
+
73
+ ```
74
+
75
+ A RegisterClient instance e.g. #<RegistersClient::RegisterClient:0x00007f893c55f740>
76
+
77
+ ```
78
+ </details>
79
+
80
+ ### <a id="registerclient"></a>`RegisterClient`
81
+
82
+ _Note: All examples use the [Country register](https://country.register.gov.uk/)._
83
+
84
+ #### `get_entries`
85
+
86
+ Get all entries from the register.
87
+
88
+ <details>
89
+ <summary>
90
+ Example use (click here to expand):
91
+ </summary>
92
+
93
+ ```
94
+
95
+ register_data = registers_client.get_register('country', 'beta')
96
+
97
+ register_data.get_entries
98
+
99
+ ```
100
+ </details>
101
+ <details>
102
+ <summary>
103
+ Expected output (click here to expand):
104
+ </summary>
105
+
106
+ ```
107
+
108
+ An EntryCollection instance.
109
+
110
+ ```
111
+ </details>
112
+
113
+ #### `get_records`
114
+
115
+ Get all records from the register.
116
+
117
+ <details>
118
+ <summary>
119
+ Example use (click here to expand):
120
+ </summary>
121
+
122
+
123
+ ```
124
+
125
+ register_data = registers_client.get_register('country', 'beta')
126
+
127
+ register_data.get_records
128
+
129
+ ```
130
+ </details>
131
+ <details>
132
+
133
+ <summary>
134
+ Expected output (click here to expand):
135
+ </summary>
136
+
137
+ ```
138
+
139
+ A RecordCollection instance.
140
+
141
+ ```
142
+
143
+ </details>
144
+
145
+ #### `get_metadata_records`
146
+
147
+ Get all metadata records of the register. This includes the register definition, field definitions and custodian.
148
+
149
+ <details>
150
+ <summary>
151
+ Example use (click here to expand):
152
+ </summary>
153
+
154
+
155
+ ```
156
+
157
+ register_data = registers_client.get_register('country', 'beta')
158
+
159
+ register_data.get_metadata_records
160
+
161
+ ```
162
+ </details>
163
+ <details>
164
+
165
+ <summary>
166
+ Expected output (click here to expand):
167
+ </summary>
168
+
169
+ ```
170
+
171
+ A RecordCollection instance.
172
+
173
+ ```
174
+ </details>
175
+
176
+ #### `get_field_definitions`
177
+
178
+ Get definitions for the fields used in the register.
179
+
180
+ <details>
181
+ <summary>
182
+ Example use (click here to expand):
183
+ </summary>
184
+
185
+
186
+ ```
187
+
188
+ register_data = registers_client.get_register('country', 'beta')
189
+
190
+ register_data.get_field_definitions
191
+
192
+ ```
193
+
194
+ </details>
195
+ <details>
196
+
197
+ <summary>
198
+ Expected output (click here to expand):
199
+ </summary>
200
+
201
+ ```
202
+
203
+ A RecordCollection instance.
204
+
205
+ ```
206
+
207
+ </details>
208
+
209
+ #### `get_register_definition`
210
+
211
+ Get the definition of the register.
212
+
213
+ <details>
214
+ <summary>
215
+ Example use (click here to expand):
216
+ </summary>
217
+
218
+
219
+ ```
220
+
221
+ register_data = registers_client.get_register('country', 'beta')
222
+
223
+ register_data.get_register_definition
224
+
225
+ ```
226
+ </details>
227
+ <details>
228
+ <summary>
229
+ Expected output (click here to expand):
230
+ </summary>
231
+
232
+ ```
233
+
234
+ A Record instance.
235
+
236
+ ```
237
+
238
+ </details>
239
+
240
+ #### `get_custodian`
241
+
242
+ Get the name of the current custodian for the register.
243
+
244
+ <details>
245
+ <summary>
246
+ Example use (click here to expand):
247
+ </summary>
248
+
249
+
250
+ ```
251
+
252
+ register_data = registers_client.get_register('country', 'beta')
253
+
254
+ register_data.get_custodian
255
+
256
+ ```
257
+
258
+ </details>
259
+ <details>
260
+
261
+ <summary>
262
+ Expected output (click here to expand):
263
+ </summary>
264
+
265
+ ```
266
+
267
+ A Record instance.
268
+
269
+ ```
270
+
271
+ </details>
272
+
273
+ #### `get_records_with_history`
274
+
275
+ Get current and previous versions of records in the register.
276
+
277
+ <details>
278
+ <summary>
279
+ Example use (click here to expand):
280
+ </summary>
281
+
282
+ ```
283
+
284
+ register_data = registers_client.get_register('country', 'beta')
285
+
286
+ germany = register_data.get_records_with_history
287
+
288
+ ```
289
+
290
+ </details>
291
+
292
+ <details>
293
+ <summary>
294
+ Expected output (click here to expand):
295
+ </summary>
296
+
297
+ ```
298
+
299
+ A RecordMapCollection instance.
300
+
301
+ ```
302
+
303
+ </details>
304
+
305
+ #### `get_current_records`
306
+
307
+ Get all current records from the register.
308
+
309
+ <details>
310
+ <summary>
311
+ Example use (click here to expand):
312
+ </summary>
313
+
314
+ ```
315
+
316
+ register_data = registers_client.get_register('country', 'beta')
317
+
318
+ register_data.get_current_records
319
+
320
+ ```
321
+ </details>
322
+ <details>
323
+ <summary>
324
+ Expected output (click here to expand):
325
+ </summary>
326
+
327
+ ```
328
+
329
+ A RecordCollection instance.
330
+
331
+ ```
332
+
333
+ </details>
334
+
335
+ #### `get_expired_records`
336
+
337
+ Get all expired records from the register.
338
+
339
+ <details>
340
+ <summary>
341
+ Example use (click here to expand)
342
+ </summary>
343
+
344
+ ```
345
+
346
+ register_data = registers_client.get_register('country', 'beta')
347
+
348
+ register_data.get_expired_records
349
+
350
+ ```
351
+ </details>
352
+ <details>
353
+ <summary>
354
+ Expected output (click here to expand)
355
+ </summary>
356
+
357
+ ```
358
+
359
+ A RecordCollection instance.
360
+
361
+ ```
362
+
363
+ </details>
364
+
365
+ #### `refresh_data`
366
+
367
+ Downloads register data. Call this method when you want to refresh data.
368
+
369
+ ## Collections
370
+
371
+ The majority of the methods available in the `RegisterClient` return one of three types of collection object. These collections all include `Enumerable` and implement the `each` method.
372
+
373
+ [`EntryCollection`](https://github.com/openregister/govuk-registers-api-client/blob/master/lib/entry_collection.rb),
374
+ [`ItemCollection`](https://github.com/openregister/govuk-registers-api-client/blob/master/lib/item_collection.rb) and [`RecordCollection`](https://github.com/openregister/govuk-registers-api-client/blob/master/lib/record_collection.rb) are all `Enumerable` and implement the same [Collections](#collections) interface.
375
+
376
+ ### `EntryCollection`
377
+
378
+ A collection of `Entry` objects.
379
+
380
+ #### `each`
381
+
382
+ Yields each `Entry` object in the collection.
383
+
384
+ #### `page(int page=1)`
385
+
386
+ Returns all `Entry` objects in the collection, according to the specified `page` number (defaults to `1`).
387
+
388
+ If there are fewer results than the current `page_size`, all results are returned.
389
+
390
+ ### `Entry`
391
+
392
+ #### `entry_number`
393
+
394
+ Gets the entry number of the entry.
395
+
396
+ <details>
397
+ <summary>
398
+ Example use (click here to expand):
399
+ </summary>
400
+
401
+ ```
402
+
403
+ register_data = registers_client.get_register('country', 'beta')
404
+
405
+ entry = register_data.get_entries.select {|entry| entry.key == 'CZ'}.first
406
+ entry.entry_number
407
+
408
+ ```
409
+ </details>
410
+ <details>
411
+ <summary>
412
+ Expected output (click here to expand):
413
+ </summary>
414
+
415
+ ```
416
+ 52
417
+ ```
418
+ </details>
419
+
420
+ ##### `key`
421
+
422
+ Gets the key of the entry.
423
+
424
+ <details>
425
+ <summary>
426
+ Example use (click here to expand):
427
+ </summary>
428
+
429
+ ```
430
+ register_data = registers_client.get_register('country', 'beta')
431
+
432
+ entry = register_data.get_entries.select {|entry| entry.key == 'CZ'}.first
433
+ entry.key
434
+
435
+ ```
436
+ </details>
437
+ <details>
438
+ <summary>
439
+ Expected output (click here to expand):
440
+ </summary>
441
+
442
+ ```
443
+ CZ
444
+ ```
445
+ </details>
446
+
447
+ ##### `timestamp`
448
+
449
+ Gets the timestamp of when the entry was appended to the register.
450
+
451
+ <details>
452
+ <summary>
453
+ Example use (click here to expand):
454
+ </summary>
455
+
456
+ ```
457
+
458
+ register_data = registers_client.get_register('country', 'beta')
459
+
460
+ entry = register_data.get_entries.select {|entry| entry.key == 'CZ'}.first
461
+ entry.timestamp
462
+
463
+ ```
464
+ </details>
465
+ <details>
466
+ <summary>
467
+ Expected output (click here to expand):
468
+ </summary>
469
+
470
+ ```
471
+ 2016-04-05T13:23:05Z
472
+ ```
473
+ </details>
474
+
475
+ ##### `item_hash`
476
+
477
+ Gets the SHA-256 hash of the item which the entry points to.
478
+
479
+ <details>
480
+ <summary>
481
+ Example use (click here to expand):
482
+ </summary>
483
+
484
+ ```
485
+
486
+ register_data = registers_client.get_register('country', 'beta')
487
+
488
+ entry = register_data.get_entries.select {|entry| entry.key == 'CZ'}.first
489
+ entry.item_hash
490
+
491
+ ```
492
+ </details>
493
+ <details>
494
+ <summary>
495
+ Expected output (click here to expand):
496
+ </summary>
497
+
498
+ ```
499
+ sha-256:c45bd0b4785680534e07c627a5eea0d2f065f0a4184a02ba2c1e643672c3f2ed
500
+ ```
501
+ </details>
502
+
503
+ ##### `value`
504
+
505
+ Returns the entry as a hash.
506
+
507
+ <details>
508
+ <summary>
509
+ Example use (click here to expand):
510
+ </summary>
511
+
512
+ ```
513
+
514
+ register_data = registers_client.get_register('country', 'beta')
515
+
516
+ entry = register_data.get_entries.select {|entry| entry.key == 'CZ'}.first
517
+ entry.value.to_json
518
+
519
+ ```
520
+ </details>
521
+ <details>
522
+ <summary>
523
+ Expected output (click here to expand):
524
+ </summary>
525
+
526
+ ```
527
+ "{"key":"CZ","timestamp":"2016-04-05T13:23:05Z","item_hash":"sha-256:c45bd0b4785680534e07c627a5eea0d2f065f0a4184a02ba2c1e643672c3f2ed"}"
528
+ ```
529
+ </details>
530
+
531
+ ### `ItemCollection`
532
+
533
+ A collection of `Item` objects.
534
+
535
+ #### `each`
536
+
537
+ Yields each `Item` object in the collection.
538
+
539
+ #### `page(int page=1)`
540
+
541
+ Returns all `Item` objects in the collection, according to the specified `page` number (defaults to `1`).
542
+
543
+ If there are fewer results than the current `page_size`, all results are returned.
544
+
545
+ ### `Item`
546
+
547
+ #### `hash`
548
+
549
+ Returns the SHA-256 hash of the item.
550
+
551
+ <details>
552
+ <summary>
553
+ Example use (click here to expand):
554
+ </summary>
555
+
556
+ ```
557
+
558
+ register_data = registers_client.get_register('country', 'beta')
559
+ item = register_data.get_records.select {|record| record.entry.key == 'SU'}.first.item
560
+ item.hash
561
+
562
+ ```
563
+ </details>
564
+ <details>
565
+ <summary>
566
+ Expected output (click here to expand):
567
+ </summary>
568
+
569
+ ```
570
+
571
+ "sha-256:e94c4a9ab00d951dadde848ee2c9fe51628b22ff2e0a88bff4cca6e4e6086d7a"
572
+
573
+ ```
574
+ </details>
575
+
576
+ #### `value`
577
+
578
+ Returns the key-value pairs represented by the item in a `JSON` object.
579
+
580
+ <details>
581
+ <summary>
582
+ Example use (click here to expand):
583
+ </summary>
584
+
585
+ ```
586
+
587
+ register_data = registers_client.get_register('country', 'beta')
588
+
589
+ item = register_data.get_records.select {|record| record.entry.key == 'SU'}.first.item
590
+ item.value
591
+
592
+ ```
593
+ </details>
594
+ <details>
595
+ <summary>
596
+ Expected output (click here to expand):
597
+ </summary>
598
+
599
+ ```
600
+
601
+ {"item_json":"{\"citizen-names\":\"Soviet citizen\",\"country\":\"SU\",\"end-date\":\"1991-12-25\",\"name\":\"USSR\",\"official-name\":\"Union of Soviet Socialist Republics\"}","item_hash":"sha-256:e94c4a9ab00d951dadde848ee2c9fe51628b22ff2e0a88bff4cca6e4e6086d7a","parsed_item":null}
602
+
603
+ ```
604
+ </details>
605
+
606
+ #### `has_end_date`
607
+
608
+ Returns a boolean to describe whether the item contains a key-value pair for the `end-date` field.
609
+
610
+ <details>
611
+ <summary>
612
+ Example use (click here to expand):
613
+ </summary>
614
+
615
+ ```
616
+
617
+ register_data = registers_client.get_register('country', 'beta')
618
+
619
+ item = register_data.get_records.select {|record| record.entry.key == 'SU'}.first.item
620
+ item.has_end_date
621
+
622
+ ```
623
+ </details>
624
+ <details>
625
+ <summary>
626
+ Expected output (click here to expand):
627
+ </summary>
628
+
629
+ ```
630
+ true
631
+ ```
632
+ </details>
633
+
634
+ ### `RecordCollection`
635
+
636
+ A collection of `Record` objects.
637
+
638
+ #### `each`
639
+
640
+ Yields each `Record` object in the collection.
641
+
642
+ #### `page(int page=1)`
643
+
644
+ Returns `Record` objects in the collection, according to the specified `page` number (defaults to `1`).
645
+
646
+ If there are fewer results than the current `page_size`, all results are returned.
647
+
648
+ ### `Record`
649
+
650
+ #### `entry`
651
+
652
+ Gets the `Entry` object associated with the record.
653
+
654
+ <details>
655
+ <summary>
656
+ Example use (click here to expand):
657
+ </summary>
658
+
659
+ ```
660
+
661
+ register_data = registers_client.get_register('country', 'beta')
662
+
663
+ record = register_data.get_records.select {|record| record.entry.key == 'CZ'}.first
664
+ record.entry.to_json
665
+
666
+ ```
667
+ </details>
668
+ <details>
669
+ <summary>
670
+ Expected output (click here to expand):
671
+ </summary>
672
+
673
+ ```
674
+ "{"entry_number":205,"parsed_entry":{"key":"CZ","timestamp":"2016-11-11T16:25:07Z","item_hash":"sha-256:c69c04fff98c59aabd739d43018e87a25fd51a00c37d100721cc68fa9003a720"}}"
675
+ ```
676
+ </details>
677
+
678
+ #### `item`
679
+
680
+ Gets the `Item` object associated with the record.
681
+
682
+ <details>
683
+ <summary>
684
+ Example use (click here to expand):
685
+ </summary>
686
+
687
+ ```
688
+
689
+ register_data = registers_client.get_register('country', 'beta')
690
+
691
+ record = register_data.get_records.select {|record| record.entry.key == 'CZ'}.first
692
+ record.item.to_json
693
+
694
+ ```
695
+ </details>
696
+ <details>
697
+ <summary>
698
+ Expected output (click here to expand):
699
+ </summary>
700
+
701
+ ```
702
+ "{"item_json":"{\"citizen-names\":\"Czech\",\"country\":\"CZ\",\"name\":\"Czechia\",\"official-name\":\"The Czech Republic\",\"start-date\":\"1993-01-01\"}","item_hash":"sha-256:c69c04fff98c59aabd739d43018e87a25fd51a00c37d100721cc68fa9003a720","parsed_item":null}"
703
+ ```
704
+ </details>
705
+
706
+
707
+ ### `RecordMapCollection`
708
+
709
+ A map of record key to list of both the current and historical `Record` objects for each key.
710
+
711
+ #### `each`
712
+
713
+ Yields each record key to list of current and historical `Record` objects in the collection, in the following format:
714
+
715
+ <details>
716
+ <summary>
717
+ Example use (click here to expand):
718
+ </summary>
719
+
720
+ ```
721
+
722
+ register_data = registers_client.get_register('country', 'beta')
723
+
724
+ register_data.get_records_with_history.each do |result|
725
+ puts result.to_json
726
+ end
727
+
728
+ ```
729
+ </details>
730
+ <details>
731
+ <summary>
732
+ Expected output for the first `result` (click here to expand):
733
+ </summary>
734
+
735
+ ```
736
+
737
+ "{"key":"SU","records":[{"entry":{"rsf_line":null,"entry_number":1,"parsed_entry":{"key":"SU","timestamp":"2016-04-05T13:23:05Z","item_hash":"sha-256:e94c4a9ab00d951dadde848ee2c9fe51628b22ff2e0a88bff4cca6e4e6086d7a"}},"item":{"item_json":"{\"citizen-names\":\"Soviet citizen\",\"country\":\"SU\",\"end-date\":\"1991-12-25\",\"name\":\"USSR\",\"official-name\":\"Union of Soviet Socialist Republics\"}","item_hash":"sha-256:e94c4a9ab00d951dadde848ee2c9fe51628b22ff2e0a88bff4cca6e4e6086d7a","parsed_item":null}}]}"
738
+
739
+ ```
740
+ </details>
741
+
742
+ #### `get_records_for_key(string key)`
743
+
744
+ Returns both the current and historical `Record` objects for a given key, or raises a `KeyError` if no records exist for the given key.
745
+
746
+ <details>
747
+ <summary>
748
+ Example use (click here to expand):
749
+ </summary>
750
+
751
+ ```
752
+
753
+ register_data = registers_client.get_register('country', 'beta')
754
+
755
+ register_data.get_records_with_history.get_records_for_key('SU')
756
+
757
+ ```
758
+ </details>
759
+ <details>
760
+ <summary>
761
+ Expected output (click here to expand):
762
+ </summary>
763
+
764
+ ```
765
+
766
+ "{[{"entry":{"rsf_line":null,"entry_number":1,"parsed_entry":{"key":"SU","timestamp":"2016-04-05T13:23:05Z","item_hash":"sha-256:e94c4a9ab00d951dadde848ee2c9fe51628b22ff2e0a88bff4cca6e4e6086d7a"}},"item":{"item_json":"{\"citizen-names\":\"Soviet citizen\",\"country\":\"SU\",\"end-date\":\"1991-12-25\",\"name\":\"USSR\",\"official-name\":\"Union of Soviet Socialist Republics\"}","item_hash":"sha-256:e94c4a9ab00d951dadde848ee2c9fe51628b22ff2e0a88bff4cca6e4e6086d7a","parsed_item":null}}]}"
767
+
768
+ ```
769
+ </details>
770
+
771
+ #### `paginator`
772
+
773
+ Returns an enumerator of a map of record key to list of current and historical `Record` objects in the collection, in slices specified by `page_size` (defined when creating the `RegisterClientManager`).
774
+
775
+ <details>
776
+ <summary>
777
+ Example use (click here to expand):
778
+ </summary>
779
+
780
+ ```
781
+
782
+ register_data = registers_client.get_register('country', 'beta')
783
+
784
+ enumerator = register_data.get_records_with_history.paginator
785
+ enumerator.next.to_json
786
+
787
+ ```
788
+ </details>
789
+ <details>
790
+ <summary>
791
+ Expected output (click here to expand):
792
+ </summary>
793
+
794
+ ```
795
+
796
+ [["SU",[{"entry":{"rsf_line":null,"entry_number":1,"parsed_entry":{"key":"SU","timestamp":"2016-04-05T13:23:05Z","item_hash":"sha-256:e94c4a9ab00d951dadde848ee2c9fe51628b22ff2e0a88bff4cca6e4e6086d7a"}},"item":{"item_json":"{\"citizen-names\":\"Soviet citizen\",\"country\":\"SU\",\"end-date\":\"1991-12-25\",\"name\":\"USSR\",\"official-name\":\"Union of Soviet Socialist Republics\"}","item_hash":"sha-256:e94c4a9ab00d951dadde848ee2c9fe51628b22ff2e0a88bff4cca6e4e6086d7a","parsed_item":null}}]],["DE",[{"entry":{"rsf_line":null,"entry_number":2,"parsed_entry":{"key":"DE","timestamp":"2016-04-05T13:23:05Z","item_hash":"sha-256:e03f97c2806206cdc2cc0f393d09b18a28c6f3e6218fc8c6f3aa2fdd7ef9d625"}},"item":{"item_json":"{\"citizen-names\":\"West German\",\"country\":\"DE\",\"end-date\":\"1990-10-02\",\"name\":\"West Germany\",\"official-name\":\"Federal Republic of Germany\"}","item_hash":"sha-256:e03f97c2806206cdc2cc0f393d09b18a28c6f3e6218fc8c6f3aa2fdd7ef9d625","parsed_item":null}},{"entry":{"rsf_line":null,"entry_number":71,"parsed_entry":{"key":"DE","timestamp":"2016-04-05T13:23:05Z","item_hash":"sha-256:747dbb718cb9f9799852e7bf698c499e6b83fb1a46ec06dbd6087f35c1e955cc"}},"item":{"item_json":"{\"citizen-names\":\"German\",\"country\":\"DE\",\"name\":\"Germany\",\"official-name\":\"The Federal Republic of Germany\",\"start-date\":\"1990-10-03\"}","item_hash":"sha-256:747dbb718cb9f9799852e7bf698c499e6b83fb1a46ec06dbd6087f35c1e955cc","parsed_item":n
797
+ ull}}]],
798
+
799
+ ...
800
+
801
+ ["AD",[{"entry":{"rsf_line":null,"entry_number":10,"parsed_entry":{"key":"AD","timestamp":"2016-04-05T13:23:05Z","item_hash":"sha-256:14fcb5099f0eff4c40d5a85b0e3c2f1a04337dc69dace1fc5c64ec9758a19b13"}},"item":{"item_json":"{\"citizen-names\":\"Andorran\",\"country\":\"AD\",\"name\":\"Andorra\",\"official-name\":\"The Principality of Andorra\"}","item_hash":"sha-256:14fcb5099f0eff4c40d5a85b0e3c2f1a04337dc69dace1fc5c64ec9758a19b13","parsed_item":null}}]]]"
802
+
803
+ ```
804
+ </details>
805
+
@@ -0,0 +1,61 @@
1
+ require 'register_client'
2
+ require 'in_memory_data_store'
3
+
4
+ module RegistersClient
5
+ VERSION = '1.0.0'
6
+ class RegisterClientManager
7
+ def initialize(config_options = {})
8
+ @config_options = defaults.merge(config_options)
9
+ @register_clients = {}
10
+ end
11
+
12
+ def get_register(register, phase, data_store = nil)
13
+ environment_url = get_environment_url_from_phase(phase)
14
+ get_register_from_environment(register, environment_url, data_store)
15
+ end
16
+
17
+ def get_register_from_environment(register, environment_url, data_store = nil)
18
+ key = register + ':' + environment_url.to_s
19
+
20
+ if !@register_clients.key?(key)
21
+ if (data_store.nil?)
22
+ data_store = RegistersClient::InMemoryDataStore.new(@config_options)
23
+ end
24
+
25
+ register_url = get_register_url(register, environment_url)
26
+ @register_clients[key] = create_register_client(register_url, data_store, @config_options.fetch(:page_size))
27
+ end
28
+
29
+ @register_clients[key]
30
+ end
31
+
32
+ private
33
+
34
+ def defaults
35
+ {
36
+ page_size: 100
37
+ }
38
+ end
39
+
40
+ def create_register_client(register_url, data_store, page_size)
41
+ RegistersClient::RegisterClient.new(register_url, data_store, page_size)
42
+ end
43
+
44
+ def get_register_url(register, environment_url)
45
+ URI.parse(environment_url.to_s.sub('register', register))
46
+ end
47
+
48
+ def get_environment_url_from_phase(phase)
49
+ case phase
50
+ when 'beta'
51
+ URI.parse('https://register.register.gov.uk')
52
+ when 'discovery'
53
+ URI.parse('https://register.cloudapps.digital')
54
+ when 'alpha', 'test'
55
+ URI.parse("https://register.#{phase}.openregister.org")
56
+ else
57
+ raise ArgumentError "Invalid phase '#{phase}'. Must be one of 'beta', 'alpha', 'discovery', 'test'."
58
+ end
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: govuk-registers-api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - GOV.UK Registers Team (Government Digital Service)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ description:
28
+ email: registers ~@nospam@~ digital.cabinet-office.gov.uk
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - README.md
33
+ files:
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - lib/register_client_manager.rb
38
+ homepage: https://github.com/openregister/govuk-registers-api-client
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options:
44
+ - "--main"
45
+ - README.md
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.7.4
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Client library for GOV.UK Registers
64
+ test_files: []