cts-mpx 1.0.1

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.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +50 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +88 -0
  5. data/CONTRIBUTING +8 -0
  6. data/COPYRIGHT +10 -0
  7. data/EXAMPLES.md +81 -0
  8. data/Gemfile +4 -0
  9. data/Gemfile.lock +172 -0
  10. data/Guardfile +41 -0
  11. data/LICENSE +201 -0
  12. data/NOTICE +9 -0
  13. data/README.md +60 -0
  14. data/Rakefile +6 -0
  15. data/bin/console +14 -0
  16. data/bin/setup +8 -0
  17. data/config/data_services.json +423 -0
  18. data/config/ingest_services.json +14 -0
  19. data/config/root_registry_sea1.json +118 -0
  20. data/config/web_services.json +544 -0
  21. data/cts-mpx.gemspec +43 -0
  22. data/examples/basic_query.rb +23 -0
  23. data/examples/login.rb +7 -0
  24. data/examples/update_media.rb +16 -0
  25. data/examples/update_procedurally.rb +20 -0
  26. data/lib/cts/mpx.rb +42 -0
  27. data/lib/cts/mpx/driver.rb +47 -0
  28. data/lib/cts/mpx/driver/assemblers.rb +96 -0
  29. data/lib/cts/mpx/driver/connections.rb +41 -0
  30. data/lib/cts/mpx/driver/exceptions.rb +50 -0
  31. data/lib/cts/mpx/driver/helpers.rb +67 -0
  32. data/lib/cts/mpx/driver/page.rb +38 -0
  33. data/lib/cts/mpx/driver/request.rb +60 -0
  34. data/lib/cts/mpx/driver/response.rb +72 -0
  35. data/lib/cts/mpx/entries.rb +80 -0
  36. data/lib/cts/mpx/entry.rb +100 -0
  37. data/lib/cts/mpx/field.rb +38 -0
  38. data/lib/cts/mpx/fields.rb +120 -0
  39. data/lib/cts/mpx/query.rb +115 -0
  40. data/lib/cts/mpx/registry.rb +60 -0
  41. data/lib/cts/mpx/service.rb +70 -0
  42. data/lib/cts/mpx/services.rb +113 -0
  43. data/lib/cts/mpx/services/data.rb +124 -0
  44. data/lib/cts/mpx/services/ingest.rb +60 -0
  45. data/lib/cts/mpx/services/web.rb +90 -0
  46. data/lib/cts/mpx/user.rb +74 -0
  47. data/lib/cts/mpx/validators.rb +51 -0
  48. data/lib/cts/mpx/version.rb +6 -0
  49. data/sdk-ring-diagram.png +0 -0
  50. data/sdk-uml.png +0 -0
  51. data/uml.nomnoml +242 -0
  52. metadata +401 -0
@@ -0,0 +1,60 @@
1
+ module Cts
2
+ module Mpx
3
+ module Services
4
+ # Collection of methods to interact with the ingest services
5
+ module Ingest
6
+ module_function
7
+
8
+ # Addressable method, indexed by ingest service title
9
+ # @param [String] key ingest service title to look up the service object
10
+ # @raise [ArgumentError] if the key is not a service name
11
+ # @raise [ArgumentError] if the key is not a string
12
+ # @return [Service[]] if no key, return the entire array of services
13
+ # @return [Service] a service
14
+ def [](key = nil)
15
+ return services unless key
16
+ Driver::Exceptions.raise_unless_argument_error?(key, String)
17
+
18
+ service = services.find { |e| e.name == key }
19
+ Driver::Exceptions.raise_unless_argument_error?(service, Driver::Service)
20
+
21
+ service
22
+ end
23
+
24
+ # Ingest service list
25
+ # @return [Services[]] Array of ingest services
26
+ def services
27
+ Services[].select { |s| s.type == 'ingest' }
28
+ end
29
+
30
+ # Procedural method to interact with an ingest service via POST
31
+ # @param [User] user user to make calls with
32
+ # @param [Hash] headers additional headers to attach to the http call
33
+ # @param [String] account account context, can be id or name
34
+ # @param [String] endpoint endpoint to make the call against
35
+ # @param [String] extra_path additional part to add to the path
36
+ # @param [String] payload string to send to ingest
37
+ # @param [String] service title of a service
38
+ # @raise [ArgumentError] if headers is not a [Hash]
39
+ # @raise [ArgumentError] if the list of [User], service or endpoint is not included
40
+ # @raise [ArgumentError] if the [User] does not have a token
41
+ # @raise (see Registry#fetch_domain)
42
+ # @raise (see Registry#store_domain)
43
+ # @return [Response] Response of the call
44
+ def post(user: nil, account: nil, service: nil, endpoint: nil, headers: {}, payload: nil, extra_path: nil)
45
+ Driver::Helpers.required_arguments ['user', 'service', 'endpoint'], binding
46
+ Driver::Helpers.raise_if_not_a_hash [headers]
47
+ user.token!
48
+
49
+ Registry.fetch_and_store_domain(user, account) unless self[service].url?
50
+
51
+ host = Driver::Assemblers.host user: user, service: service
52
+ path = Driver::Assemblers.path service: service, endpoint: endpoint, extra_path: extra_path
53
+
54
+ request = Driver::Request.create(method: :post, url: [host, path].join, payload: payload, headers: headers)
55
+ request.call
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,90 @@
1
+ module Cts
2
+ module Mpx
3
+ module Services
4
+ # Collection of procedural methods to interact with the Web services
5
+ # All of these methods mimic the Business clients as close as possible
6
+ # If your operation does not work in the Business client, it will not work here
7
+ module Web
8
+ module_function
9
+
10
+ # Addressable method, indexed by web service title
11
+ # @param [String] key service title to look up the service object
12
+ # @raise [ArgumentError] if the key is not a service name
13
+ # @raise [ArgumentError] if the key is not a string
14
+ # @return [Service[]] if no key, return the entire array of services
15
+ # @return [Service] a service
16
+ def [](key = nil)
17
+ return services unless key
18
+ Driver::Exceptions.raise_unless_argument_error?(key, String)
19
+
20
+ service = services.find { |e| e.name == key }
21
+ Driver::Exceptions.raise_unless_argument_error?(service, Driver::Service)
22
+
23
+ service
24
+ end
25
+
26
+ # Web service list
27
+ # @return [Services[]] Array of web services
28
+ def services
29
+ Services[].select { |s| s.type == 'web' }
30
+ end
31
+
32
+ # assembles service, endpoint, method, and arguments into a payload
33
+ # @param [String] service title of a service
34
+ # @param [String] endpoint endpoint to make the call against
35
+ # @param [String] method method to make the call against
36
+ # @param [Hash] arguments arguments to send to the method
37
+ # @return [Hash] a hash ready for the web services
38
+ def assemble_payload(service: nil, endpoint: nil, method: nil, arguments: nil)
39
+ Driver::Helpers.required_arguments ['service', 'endpoint', 'method', 'arguments'], binding
40
+ service = Services[service]
41
+ method_list = service.endpoints[endpoint]['methods']
42
+ Driver::Exceptions.raise_unless_argument_error?(arguments, Hash)
43
+ Driver::Exceptions.raise_unless_argument_error?(method, 'method') { method_list.key?(method) }
44
+
45
+ arguments.each_key { |k| Driver::Exceptions.raise_unless_argument_error?(arguments, 'argument') { method_list[method].include? k.to_s } }
46
+
47
+ h = {}
48
+ h[method] = arguments
49
+ h
50
+ end
51
+
52
+ # Procedural method to interact with a web service via POST
53
+ # @param [User] user user to make calls with
54
+ # @param [Hash] headers additional headers to attach to the http call
55
+ # @param [Hash] query additional parameters to add to the http call
56
+ # @param [Hash] arguments data to be sent to the data service
57
+ # @param [String] account account context, can be id or name
58
+ # @param [String] method method to make the call against
59
+ # @param [String] endpoint endpoint to make the call against
60
+ # @param [String] extra_path additional part to add to the path
61
+ # @param [String] service title of a service
62
+ # @raise [ArgumentError] if headers is not a [Hash]
63
+ # @raise [ArgumentError] if the list of [User], service or endpoint is not included
64
+ # @raise [ArgumentError] if the [User] does not have a token
65
+ # @raise (see Registry#fetch_domain)
66
+ # @raise (see Registry#store_domain)
67
+ # @return [Response] Response of the call
68
+ def post(user: nil, account: nil, service: nil, endpoint: nil, method: nil, query: {}, headers: {}, arguments: {}, extra_path: nil)
69
+ ### check arguments
70
+ Driver::Helpers.required_arguments ['user', 'service', 'endpoint', 'method', 'arguments'], binding
71
+ Driver::Helpers.raise_if_not_a_hash [query, headers, arguments]
72
+ user.token!
73
+
74
+ ### Registry
75
+ Registry.fetch_and_store_domain(user, account) unless self[service].url?
76
+
77
+ ### Assemblers/prep
78
+ host = Driver::Assemblers.host user: user, service: service
79
+ path = Driver::Assemblers.path service: service, endpoint: endpoint, extra_path: extra_path
80
+ payload = assemble_payload service: service, endpoint: endpoint, method: method, arguments: arguments
81
+ query = Driver::Assemblers.query user: user, account: account, service: service, endpoint: endpoint, query: query
82
+
83
+ ### Request
84
+ request = Driver::Request.create(method: :post, url: [host, path].join, query: query, payload: Oj.dump(payload), headers: headers)
85
+ request.call
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,74 @@
1
+ module Cts
2
+ module Mpx
3
+ # Class to wrap the user and basic functions related to it.
4
+ # @attribute username
5
+ # @return [String] username of the user
6
+ # @attribute password
7
+ # @return [String] password of the user
8
+ # @attribute idle_timeout
9
+ # @return [Numeric] how long the token will stay alive without communicating with the services
10
+ # @attribute duration
11
+ # @return [Numeric] total time to live for the token
12
+ # @attribute token
13
+ # @return [String] the retrieved token
14
+ class User
15
+ extend Creatable
16
+
17
+ attribute name: 'username', type: 'accessor', kind_of: String
18
+ attribute name: 'password', type: 'accessor', kind_of: String
19
+ attribute name: 'idle_timeout', type: 'accessor', kind_of: Integer
20
+ attribute name: 'duration', type: 'accessor', kind_of: Integer
21
+ attribute name: 'token', type: 'accessor', kind_of: String
22
+
23
+ # Attempt to sign the user in with the provided credentials
24
+ # @param [Numeric] idle_timeout how long the token will stay alive without communicating with the services
25
+ # @param [Numeric] duration total time to live for the token
26
+ # @return [Self]
27
+ def sign_in(idle_timeout: nil, duration: nil)
28
+ raise 'token is already set, use sign_out first.' if token
29
+ arguments = {}
30
+
31
+ arguments['idleTimeout'] if idle_timeout
32
+ arguments['duration'] if duration
33
+ headers = { 'Authorization' => "Basic #{Base64.encode64("#{username}:#{password}").tr "\n", ''}" }
34
+
35
+ self.token = 'sign_in_token'
36
+ response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signIn', arguments: arguments, headers: headers
37
+
38
+ raise "sign_in exception, status: #{response.status}" unless response.status == 200
39
+ self.token = response.data['signInResponse']['token']
40
+ self
41
+ end
42
+
43
+ # Sign the token out
44
+ # @return [Void]
45
+ def sign_out
46
+ arguments = { "token" => token }
47
+ response = Services::Web.post user: self, service: 'User Data Service', endpoint: 'Authentication', method: 'signOut', arguments: arguments
48
+ self.token = nil if response.status == 200
49
+ nil
50
+ end
51
+
52
+ # Override to masq the password
53
+ # @return [String] updated output
54
+ def inspect
55
+ output = "#<#{self.class}:#{(object_id << 1).to_s(16)}"
56
+
57
+ %i[username token idle_timeout duration].each do |attribute|
58
+ value = instance_variable_get "@#{attribute}".to_sym
59
+ output += " @#{attribute}=#{value}" unless value.nil?
60
+ end
61
+
62
+ output += '>'
63
+ output
64
+ end
65
+
66
+ # raise an error if the token is not set, otherwise return the token
67
+ # @return [String] token
68
+ def token!
69
+ raise "#{username} is not signed in, (token is set to nil)." unless token
70
+ token
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,51 @@
1
+ module Cts
2
+ module Mpx
3
+ #
4
+ # Collection of methods that will validate input based on specific tests.
5
+ #
6
+ module Validators
7
+ module_function
8
+
9
+ # Validates if a string is a long form id
10
+ # @param [String] account_id long form id
11
+ # @return [Boolean] true if it is a long form id, false if it is not.
12
+ def account_id?(account_id)
13
+ return true if account_id == 'urn:theplatform:auth:root'
14
+ return false unless reference? account_id
15
+ return false unless account_id.downcase.match?(/\/account\/\d+$/)
16
+
17
+ true
18
+ end
19
+
20
+ # Test to check for validity of argument by type, can also accept a block.
21
+ # @note test
22
+ # @param [Object] data object to check
23
+ # @param [Class] type class type to accept
24
+ # @yield Description of block
25
+ # @yieldreturn [boolean] true if the outcome is valid, false otherwise.
26
+ # @return [boolean] true if the outcome is valid, false otherwise.
27
+ def argument_error?(data, type = nil, &block)
28
+ return block.yield if block
29
+ return true unless type && data.is_a?(type)
30
+ false
31
+ end
32
+
33
+ # Validates if a string is a reference
34
+ # @param [String] uri reference
35
+ # @return [Boolean] true if it is a reference, false if it is not.
36
+ def reference?(uri)
37
+ begin
38
+ ref = URI.parse uri
39
+ rescue URI::InvalidURIError
40
+ return false
41
+ end
42
+
43
+ return false if ref.host == 'web.theplatform.com'
44
+ return false unless ref.scheme == "http" || ref.scheme == "https"
45
+ return false unless ref.host.end_with? ".theplatform.com"
46
+ return false if ref.host.start_with? 'feed.media.theplatform'
47
+ true
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,6 @@
1
+ module Cts
2
+ module Mpx
3
+ # @return [String] frozen string of the version of Cts::Mpx
4
+ VERSION = "1.0.1".freeze
5
+ end
6
+ end
Binary file
Binary file
@@ -0,0 +1,242 @@
1
+ [<module> Assemblers|
2
+ |
3
+ host(user: nil, service: nil, account_id: 'urn:theplatform:auth:root')
4
+ path(service: nil, endpoint: nil, extra_path: nil, ids: nil, account: 'urn:theplatform:auth:root')
5
+ query(user: nil, account: nil, service: nil, endpoint: nil, query: {}, range: nil, count: nil, entries: nil, sort: nil)
6
+ query_data(range: nil, count: nil, entries: nil, sort: nil)
7
+ ]
8
+ [<module> Connections|
9
+ |
10
+ \[\](uri = nil)
11
+ ]
12
+ [<module> Helpers|
13
+ |
14
+ raise_if_not_a(objects, type)
15
+ raise_if_not_an_array(objects)
16
+ raise_if_not_a_hash(objects)
17
+ required_arguments(arguments, a_binding)
18
+ ]
19
+ [<class> Page|
20
+ +entries kind_of:Array
21
+ +xmlns kind_of:Hash
22
+ |
23
+ initialize
24
+ to_s(indent_depth = nil)
25
+ ]
26
+ [<class> Request|
27
+ +method kind_of:Symbol
28
+ +url kind_of:String
29
+ +query kind_of:Hash
30
+ +payload kind_of:String
31
+ +response kind_of:::Cts::Mpx::Driver::Response
32
+ +headers kind_of:Hash
33
+ |
34
+ call
35
+ call_exceptions(method, url)
36
+ ]
37
+ [<class> Response|
38
+ +original kind_of:Excon::Response
39
+ |
40
+ data
41
+ healthy?
42
+ service_exception?
43
+ page
44
+ status
45
+ ]
46
+ [<module> Driver|
47
+ |
48
+ gem_dir
49
+ config_dir
50
+ load_json_file(filename)
51
+ ]
52
+ [<class> Entries|
53
+ +collection kind_of:Array
54
+ |
55
+ self.create_from_page(page)
56
+ \[\](key = nil)
57
+ add(entry)
58
+ each
59
+ initialize
60
+ remove(argument)
61
+ reset
62
+ ]
63
+ [<class> Entry|
64
+ +data kind_of:Hash
65
+ +endpoint kind_of:String
66
+ +fields kind_of:Fields
67
+ +id kind_of:String
68
+ +service kind_of:String
69
+ +xmlns kind_of:Hash
70
+ |
71
+ self.load_by_id(user: nil, id: nil, fields: nil)
72
+ id
73
+ id=(value)
74
+ initialize
75
+ load(user: nil, fields: nil)
76
+ save(user: nil)
77
+ ]
78
+ [<module> Exceptions|
79
+ |
80
+ raise_unless_account_id(argument)
81
+ ]
82
+ [<module> Exceptions|
83
+ |
84
+ raise_unless_argument_error?(data, type = nil, &block)
85
+ ]
86
+ [<module> Exceptions|
87
+ |
88
+ raise_unless_reference?(argument)
89
+ ]
90
+ [<module> Exceptions|
91
+ |
92
+ raise_unless_required_argument?(argument: nil)
93
+ ]
94
+ [<class> Field|
95
+ +field_name kind_of:String
96
+ +xmlns kind_of:Hash
97
+ |
98
+ name
99
+ qualified_field_name
100
+ to_s
101
+ to_h
102
+ type
103
+ ]
104
+ [<class> Fields|
105
+ +collection kind_of:Array
106
+ |
107
+ self.create_from_data(data: nil, xmlns: nil)
108
+ \[\](key = nil)
109
+ \[\]=(key, value, xmlns: nil)
110
+ add(field)
111
+ each
112
+ initialize
113
+ parse(xmlns: nil, data: nil)
114
+ remove(field_name)
115
+ reset
116
+ to_h
117
+ to_s
118
+ xmlns
119
+ ]
120
+ [<class> Query|
121
+ +account kind_of:String
122
+ +endpoint kind_of:String
123
+ +extra_path kind_of:String
124
+ +fields kind_of:String
125
+ +ids kind_of:Array
126
+ +page kind_of:Driver::Page
127
+ +query kind_of:Hash
128
+ +range kind_of:String
129
+ +return_count kind_of:FalseClass
130
+ +return_entries kind_of:TrueClass
131
+ +service kind_of:String
132
+ +sort kind_of:String
133
+ |
134
+ attributes
135
+ initialize
136
+ entries
137
+ run(user: nil)
138
+ url(user: nil, token: nil); end
139
+ params
140
+ ]
141
+ [<module> Registry|
142
+ |
143
+ domains
144
+ fetch_and_store_domain(user, account_id = 'urn:theplatform:auth:root')
145
+ fetch_domain(user, account_id = 'urn:theplatform:auth:root')
146
+ store_domain(data, account_id = 'urn:theplatform:auth:root')
147
+ initialize
148
+ raise_argument_error(data, name)
149
+ ]
150
+ [<class> Service|
151
+ |
152
+ initialize
153
+ url(account_id = 'urn:theplatform:auth:root')
154
+ url?(account_id = 'urn:theplatform:auth:root')
155
+ ]
156
+ [<module> Data|
157
+ |
158
+ \[\](key = nil)
159
+ delete(user: nil, account: nil, service: nil, endpoint: nil, sort: nil, extra_path: nil, range: nil, ids: nil, query: {}, headers: {}, count: nil, entries: nil)
160
+ get(user: nil, account: nil, service: nil, fields: nil, endpoint: nil, sort: nil, extra_path: nil, range: nil, ids: nil, query: {}, headers: {}, count: nil, entries: nil, method: :get)
161
+ post(user: nil, account: nil, service: nil, endpoint: nil, extra_path: nil, query: {}, page: nil, headers: {}, method: :post)
162
+ put(user: nil, account: nil, service: nil, endpoint: nil, extra_path: nil, query: {}, page: nil, headers: {})
163
+ prep_call(args = {})
164
+ ]
165
+ [<module> Ingest|
166
+ |
167
+ \[\](key = nil)
168
+ services
169
+ post(user: nil, account: nil, service: nil, endpoint: nil, headers: {}, payload: nil, extra_path: nil)
170
+ ]
171
+ [<module> Web|
172
+ |
173
+ \[\](key = nil)
174
+ services
175
+ assemble_payload(service: nil, endpoint: nil, method: nil, arguments: nil)
176
+ post(user: nil, account: nil, service: nil, endpoint: nil, method: nil, query: {}, headers: {}, arguments: {}, extra_path: nil)
177
+ ]
178
+ [<module> Services|
179
+ |
180
+ \[\](key = nil)
181
+ from_url(url)
182
+ initialize
183
+ load_reference_file(file: nil, type: nil)
184
+ load_references
185
+ load_services
186
+ raw_reference
187
+ reference(key = nil)
188
+ types
189
+ ]
190
+ [<class> User|
191
+ |
192
+ sign_in(idle_timeout: nil, duration: nil)
193
+ sign_out
194
+ token!
195
+ ]
196
+ [<module> Validators|
197
+ |
198
+ account_id?(account_id)
199
+ ]
200
+ [<module> Validators|
201
+ |
202
+ argument_error?(data, type = nil, &block)
203
+ ]
204
+ [<module> Validators|
205
+ |
206
+ reference?(uri)
207
+ ]
208
+ [<module> Validators|
209
+ |
210
+ required_argument?(argument: nil)
211
+ ]
212
+ [<module> Mpx|
213
+ |
214
+ ]
215
+ [<module> Mpx|
216
+ |
217
+ ]
218
+
219
+
220
+ [Cts]->[Mpx]
221
+ [Mpx]->[User]
222
+ [Mpx]->[Entries]
223
+ [Mpx]->[Entry]
224
+ [Mpx]->[Field]
225
+ [Mpx]->[Fields]
226
+ [Mpx]->[Query]
227
+ [Mpx]->[Registry]
228
+ [Mpx]->[Service]
229
+ [Mpx]->[Services]
230
+ [Mpx]->[Exceptions]
231
+ [Mpx]->[Driver]
232
+ [Mpx]->[Validators]
233
+
234
+ [Driver]->[Assemblers]
235
+ [Driver]->[Connections]
236
+ [Driver]->[Helpers]
237
+ [Driver]->[Page]
238
+ [Driver]->[Request]
239
+ [Driver]->[Response]
240
+ [Services]->[Data]
241
+ [Services]->[Web]
242
+ [Services]->[Ingest]