Class: ReactorSDK::ResponseParser

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • data (Hash)

    Raw JSON:API data hash

  • resource_class (Class)

    Resource class to instantiate

  • response (Hash, nil) (defaults to: nil)

    Full response envelope — used to extract included array for Revision resources

Returns:

  • (Object)

    An instance of resource_class populated with parsed data

Raises:

  • (ArgumentError)

    if data is nil

  • (KeyError)

    if required JSON:API fields are missing



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.

Parameters:

  • data (Hash)
  • response (Hash, nil) (defaults to: nil)

Returns:



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.

Parameters:

  • data_array (Array<Hash>)

    Array of raw JSON:API data hashes

  • resource_class (Class)

    Resource class to instantiate for each item

  • response (Hash, nil) (defaults to: nil)

    Full response envelope (passed through to parse)

Returns:

  • (Array<Object>)

    Array of resource_class instances



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.

Parameters:

  • data_array (Array<Hash>)
  • response (Hash, nil) (defaults to: nil)

Returns:



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