usps-imis-api 0.6.12 → 0.6.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03dbc937f5336d48ede502436a3ddcee7d77f8f357d18d807ac4119b52acf7e8
4
- data.tar.gz: 9052ead6d9fb967e5faadb730d7000f32d0e97e83545e60fdef7e9a0b3deda18
3
+ metadata.gz: 05b1b663fab7b4f81c95f105c0c7f667bc2f5b9855b5fd48e5f384ce0adba8dc
4
+ data.tar.gz: '02529c44a577d5e239b3bd306cec0f9f91e53413ef3d921c5564352e8dca3d5f'
5
5
  SHA512:
6
- metadata.gz: 33d7ced678ecc240e405b73fe5ba6c006fedece86612f76018296166c53e8226996b27617fb1a7212f885c4a5107c9c03186ad922106ccc79714e6a522127fd9
7
- data.tar.gz: 78b04d51fde868adf81b86231083bdafdb6f08a7f6e7664ba3406b65557be09bb875cc9f1c37846a353872f784cdc872506b27415150ad1b5f79363b5baf29aa
6
+ metadata.gz: e81f01c677aa5a22b1c7c5cfeb6e3119cc1f4387dc2076fec0001032879378f5be3cd562c3ccdd689a5e26e7becb4381a891249c175b025014d78638a36dd03e
7
+ data.tar.gz: 3e3f53303d8316f814cefffb3473ccb8e91c5fbd5146bc94b1701532c006fbf8b0ab905a156bd880e3fe6a2f1c2c4f79528c591df8b6ad6e26cb8cee7ab88346
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- usps-imis-api (0.6.12)
4
+ usps-imis-api (0.6.14)
5
5
  activesupport (~> 8.0)
6
6
 
7
7
  GEM
data/Readme.md CHANGED
@@ -216,9 +216,9 @@ previous value.
216
216
  api.with(31092) do
217
217
  # These requests are identical:
218
218
 
219
- on('ABC_ASC_Individual_Demog') { put('TotMMS' => 15) }
219
+ on('ABC_ASC_Individual_Demog') { put_fields('TotMMS' => 15) }
220
220
 
221
- on('ABC_ASC_Individual_Demog').put('TotMMS' => 15)
221
+ on('ABC_ASC_Individual_Demog').put_fields('TotMMS' => 15)
222
222
 
223
223
  mapper.update(mm: 15)
224
224
 
data/lib/usps/imis/api.rb CHANGED
@@ -135,10 +135,10 @@ module Usps
135
135
  # An instance of +BusinessObject+, using this instance as its parent +Api+
136
136
  #
137
137
  # @param business_object_name [String] Name of the business object
138
- # @param url_id [String] Override the ID param of the URL (e.g. used for Panels)
138
+ # @param ordinal [Integer] Ordinal to build override ID param of the URL (e.g. used for Panels)
139
139
  #
140
- def business_object(business_object_name, url_id: nil)
141
- BusinessObject.new(self, business_object_name, url_id:)
140
+ def business_object(business_object_name, ordinal: nil)
141
+ BusinessObject.new(self, business_object_name, ordinal:)
142
142
  end
143
143
 
144
144
  # Run requests as DSL, with specific +BusinessObject+ only maintained for this scope
@@ -146,15 +146,13 @@ module Usps
146
146
  # If no block is given, this returns the specified +BusinessObject+.
147
147
  #
148
148
  # @param business_object_name [String] Name of the business object
149
- # @param url_id [String] Override the ID param of the URL (e.g. used for Panels)
149
+ # @param ordinal [Integer] Ordinal to build override ID param of the URL (e.g. used for Panels)
150
150
  #
151
- def on(business_object_name, url_id: nil, &)
152
- object = business_object(business_object_name, url_id:)
151
+ def on(business_object_name, ordinal: nil, &)
152
+ object = business_object(business_object_name, ordinal:)
153
153
  return object unless block_given?
154
154
 
155
- result = nil
156
- object.tap { |obj| result = obj.instance_eval(&) }
157
- result
155
+ object.instance_eval(&)
158
156
  end
159
157
 
160
158
  # An instance of +Mapper+, using this instance as its parent +Api+
@@ -18,16 +18,16 @@ module Usps
18
18
  #
19
19
  attr_reader :business_object_name
20
20
 
21
- # Override ID param of the URL (e.g. used for Panels)
21
+ # Ordinal to build override ID param of the URL (e.g. used for Panels)
22
22
  #
23
- attr_reader :url_id
23
+ attr_reader :ordinal
24
24
 
25
25
  # A new instance of +BusinessObject+
26
26
  #
27
- def initialize(api, business_object_name, url_id: nil)
27
+ def initialize(api, business_object_name, ordinal: nil)
28
28
  @api = api
29
29
  @business_object_name = business_object_name
30
- @url_id = url_id
30
+ @ordinal = ordinal
31
31
  end
32
32
 
33
33
  # Get a business object for the current member
@@ -39,6 +39,7 @@ module Usps
39
39
  result = submit(uri, authorize(request))
40
40
  JSON.parse(result.body)
41
41
  end
42
+ alias read get
42
43
 
43
44
  # Get a single named field from a business object for the current member
44
45
  #
@@ -52,6 +53,7 @@ module Usps
52
53
 
53
54
  value.is_a?(String) ? value : value['$value']
54
55
  end
56
+ alias fetch get_field
55
57
 
56
58
  # Update only specific fields on a business object for the current member
57
59
  #
@@ -63,6 +65,7 @@ module Usps
63
65
  updated = filter_fields(fields)
64
66
  put(updated)
65
67
  end
68
+ alias patch put_fields
66
69
 
67
70
  # Update a business object for the current member
68
71
  #
@@ -76,6 +79,7 @@ module Usps
76
79
  result = submit(uri, authorize(request))
77
80
  JSON.parse(result.body)
78
81
  end
82
+ alias update put
79
83
 
80
84
  # Create a business object for the current member
81
85
  #
@@ -84,11 +88,12 @@ module Usps
84
88
  # @return [Hash] Response data from the API
85
89
  #
86
90
  def post(body)
87
- request = Net::HTTP::Post.new(uri)
91
+ request = Net::HTTP::Post.new(uri(id: ''))
88
92
  request.body = JSON.dump(body)
89
93
  result = submit(uri, authorize(request))
90
94
  JSON.parse(result.body)
91
95
  end
96
+ alias create post
92
97
 
93
98
  # Remove a business object for the current member
94
99
  #
@@ -99,6 +104,7 @@ module Usps
99
104
  result = submit(uri, authorize(request))
100
105
  result.body
101
106
  end
107
+ alias destroy delete
102
108
 
103
109
  private
104
110
 
@@ -107,31 +113,42 @@ module Usps
107
113
 
108
114
  # Construct a business object API endpoint address
109
115
  #
110
- def uri
111
- id_for_url = url_id ? CGI.escape(url_id) : api.imis_id
112
- full_path = "#{API_PATH}/#{business_object_name}/#{id_for_url}"
116
+ def uri(id: nil)
117
+ full_path = "#{API_PATH}/#{business_object_name}/#{id_for_uri(id)}"
113
118
  URI(File.join(Imis.configuration.hostname, full_path))
114
119
  end
115
120
 
121
+ # Select the correct ID to use in the URI
122
+ #
123
+ def id_for_uri(id = nil)
124
+ return CGI.escape(id) unless id.nil?
125
+ return CGI.escape("~#{api.imis_id}|#{ordinal}") if ordinal
126
+
127
+ api.imis_id
128
+ end
129
+
116
130
  # Manually assemble the matching data structure, with fields in the correct order
117
131
  #
118
132
  def filter_fields(fields)
119
133
  existing = get
120
134
 
121
135
  JSON.parse(JSON.dump(existing)).tap do |updated|
136
+ # Wipe the array on the duped object
137
+ #
122
138
  # The first property is always the iMIS ID again
123
- updated['Properties']['$values'] = [existing['Properties']['$values'][0]]
139
+ #
140
+ updated['Properties']['$values'] = []
124
141
 
125
142
  # Iterate through all existing fields
126
143
  existing['Properties']['$values'].each do |value|
127
- next unless fields.keys.include?(value['Name'])
128
-
129
- # Strings are not wrapped in the type definition structure
130
- new_value = fields[value['Name']]
131
- if new_value.is_a?(String)
132
- value['Value'] = new_value
133
- else
134
- value['Value']['$value'] = new_value
144
+ if fields.keys.include?(value['Name'])
145
+ # Strings are not wrapped in the type definition structure
146
+ new_value = fields[value['Name']]
147
+ if new_value.is_a?(String)
148
+ value['Value'] = new_value
149
+ else
150
+ value['Value']['$value'] = new_value
151
+ end
135
152
  end
136
153
 
137
154
  # Add the completed field with the updated value
@@ -20,35 +20,61 @@ module Usps
20
20
  # @param ordinal [Integer] The ordinal identifier for the desired object
21
21
  #
22
22
  def get(ordinal)
23
- api.business_object(business_object, url_id: "~#{api.imis_id}|#{ordinal}").get
23
+ api.on(business_object, ordinal:).get
24
24
  end
25
+ alias read get
25
26
 
26
- # Create a new object in the Panel
27
+ # Get a single named field from a Panel for the current member
27
28
  #
28
- # @param data [Hash] The record data for the desired object
29
+ # @param ordinal [Integer] The ordinal identifier for the desired object
30
+ # @param name [String] Field name to return
31
+ #
32
+ # @return [Hash] Response data from the API
33
+ #
34
+ def get_field(ordinal, name)
35
+ api.on(business_object, ordinal:).get_field(name)
36
+ end
37
+ alias fetch get_field
38
+
39
+ # Update only specific fields on a Panel for the current member
40
+ #
41
+ # @param ordinal [Integer] The ordinal identifier for the desired object
42
+ # @param fields [Hash] Conforms to pattern +{ field_key => value }+
43
+ #
44
+ # @return [Hash] Response data from the API
29
45
  #
30
- def create(data)
31
- api.business_object(business_object, url_id: '').post(payload(data))
46
+ def put_fields(ordinal, fields)
47
+ api.on(business_object, ordinal:).put_fields(fields)
32
48
  end
49
+ alias patch put_fields
33
50
 
34
51
  # Update an existing object in the Panel
35
52
  #
36
53
  # @param data [Hash] The record data for the desired object -- including the required
37
54
  # +ordinal+ identifier
38
55
  #
39
- def update(data)
40
- api
41
- .business_object(business_object, url_id: "~#{api.imis_id}|#{data[:ordinal]}")
42
- .put(payload(data))
56
+ def put(data)
57
+ api.on(business_object, ordinal: data[:ordinal]).put(payload(data))
58
+ end
59
+ alias update put
60
+
61
+ # Create a new object in the Panel
62
+ #
63
+ # @param data [Hash] The record data for the desired object
64
+ #
65
+ def post(data)
66
+ api.on(business_object).post(payload(data))
43
67
  end
68
+ alias create post
44
69
 
45
70
  # Remove a specific object from the Panel
46
71
  #
47
72
  # @param ordinal [Integer] The ordinal identifier for the desired object
48
73
  #
49
- def destroy(ordinal)
50
- api.business_object(business_object, url_id: "~#{api.imis_id}|#{ordinal}").delete
74
+ def delete(ordinal)
75
+ api.on(business_object, ordinal:).delete
51
76
  end
77
+ alias destroy delete
52
78
 
53
79
  private
54
80
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- VERSION = '0.6.12'
5
+ VERSION = '0.6.14'
6
6
  end
7
7
  end
@@ -26,8 +26,14 @@ describe Usps::Imis::Panel::Education do
26
26
  end
27
27
  end
28
28
 
29
+ describe '#get_field' do
30
+ it 'returns a specific field' do
31
+ expect(education.get_field(90737, 'ABC_Product_Code')).to eq('CRS')
32
+ end
33
+ end
34
+
29
35
  # rubocop:disable RSpec/ExampleLength
30
- it 'handles new records correctly', :aggregate_failures do
36
+ it 'interacts with records correctly', :aggregate_failures do
31
37
  new_record = education.create(details)
32
38
  expect(new_record).to be_a(Hash)
33
39
 
@@ -40,6 +46,12 @@ describe Usps::Imis::Panel::Education do
40
46
  end
41
47
  expect(updated['Value']).to eq('Online Exams System - Modified')
42
48
 
49
+ put_fields_result = education.put_fields(ordinal, 'ABC_Educ_Source_System' => 'Online Exams System - Mod2')
50
+ patched = put_fields_result['Properties']['$values'].find do |v|
51
+ v['Name'] == 'ABC_Educ_Source_System'
52
+ end
53
+ expect(patched['Value']).to eq('Online Exams System - Mod2')
54
+
43
55
  expect(education.destroy(ordinal)).to eq('')
44
56
  end
45
57
  # rubocop:enable RSpec/ExampleLength
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usps-imis-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.12
4
+ version: 0.6.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander