Class: ReactorSDK::ResponseParser
- Inherits:
-
Object
- Object
- ReactorSDK::ResponseParser
- Defined in:
- lib/reactor_sdk/response_parser.rb
Constant Summary collapse
- TYPE_REGISTRY =
{ 'app_configurations' => Resources::AppConfiguration, 'audit_events' => Resources::AuditEvent, 'builds' => Resources::Build, 'callbacks' => Resources::Callback, 'companies' => Resources::Company, 'data_elements' => Resources::DataElement, 'environments' => Resources::Environment, 'extension_package_usage_authorizations' => Resources::ExtensionPackageUsageAuthorization, 'extension_packages' => Resources::ExtensionPackage, 'extensions' => Resources::Extension, 'hosts' => Resources::Host, 'libraries' => Resources::Library, 'notes' => Resources::Note, 'profiles' => Resources::Profile, 'properties' => Resources::Property, 'revisions' => Resources::Revision, 'rule_components' => Resources::RuleComponent, 'rules' => Resources::Rule, 'secrets' => Resources::Secret }.freeze
Instance Method Summary collapse
-
#parse(data, resource_class, response: nil) ⇒ Object
Parses a single JSON:API resource hash into a typed resource object.
-
#parse_auto(data, response: nil) ⇒ ReactorSDK::Resources::BaseResource
Parses a single resource into the best matching SDK class using its JSON:API type.
-
#parse_many(data_array, resource_class, response: nil) ⇒ Array<Object>
Parses an array of JSON:API resource hashes into typed resource objects.
-
#parse_many_auto(data_array, response: nil) ⇒ Array<ReactorSDK::Resources::BaseResource>
Parses a heterogeneous collection into typed SDK resources.
Instance Method Details
#parse(data, resource_class, response: nil) ⇒ Object
Parses a single JSON:API resource hash into a typed resource object.
For Revision resources, also extracts the included entity snapshot and relationships from the full response envelope if provided.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/reactor_sdk/response_parser.rb', line 61 def parse(data, resource_class, response: nil) raise ArgumentError, 'data cannot be nil' if data.nil? base_args = { id: data.fetch('id'), type: data.fetch('type'), attributes: data.fetch('attributes', {}), meta: data.fetch('meta', {}), relationships: data.fetch('relationships', {}) } base_args.merge!(extract_revision_extras(data, response)) if resource_class == Resources::Revision resource_class.new(**base_args) end |
#parse_auto(data, response: nil) ⇒ ReactorSDK::Resources::BaseResource
Parses a single resource into the best matching SDK class using its JSON:API type.
98 99 100 101 |
# File 'lib/reactor_sdk/response_parser.rb', line 98 def parse_auto(data, response: nil) resource_class = TYPE_REGISTRY.fetch(data.fetch('type'), Resources::BaseResource) parse(data, resource_class, response: response) end |
#parse_many(data_array, resource_class, response: nil) ⇒ Array<Object>
Parses an array of JSON:API resource hashes into typed resource objects. Returns an empty array if data_array is nil or empty.
86 87 88 |
# File 'lib/reactor_sdk/response_parser.rb', line 86 def parse_many(data_array, resource_class, response: nil) Array(data_array).map { |data| parse(data, resource_class, response: response) } end |
#parse_many_auto(data_array, response: nil) ⇒ Array<ReactorSDK::Resources::BaseResource>
Parses a heterogeneous collection into typed SDK resources.
110 111 112 |
# File 'lib/reactor_sdk/response_parser.rb', line 110 def parse_many_auto(data_array, response: nil) Array(data_array).map { |data| parse_auto(data, response: response) } end |