diff --git a/lib/app_store_connect/object/data.rb b/lib/app_store_connect/object/data.rb index fefc5dc..6530c16 100644 --- a/lib/app_store_connect/object/data.rb +++ b/lib/app_store_connect/object/data.rb @@ -33,12 +33,6 @@ def to_h end const_set('Data', klass) - - def to_h - { - data: data.to_h - } - end end class_methods do diff --git a/lib/app_store_connect/object/included.rb b/lib/app_store_connect/object/included.rb new file mode 100644 index 0000000..1bc0720 --- /dev/null +++ b/lib/app_store_connect/object/included.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'active_support/concern' + +module AppStoreConnect + module Object + module Included + extend ActiveSupport::Concern + + included do + attr_reader :included + + klass = Class.new do |i| + i.send(:define_method, :initialize) do |objects| + instance_variable_set('@objects', [*objects]) + end + + def to_a + @objects + end + end + + const_set('Included', klass) + end + end + end +end diff --git a/lib/app_store_connect/request/body.rb b/lib/app_store_connect/request/body.rb index b014ee5..7832abc 100644 --- a/lib/app_store_connect/request/body.rb +++ b/lib/app_store_connect/request/body.rb @@ -5,12 +5,22 @@ class Request class Body def self.inherited(klass) super + + klass.include(Object::Included) klass.include(Object::Data) end def initialize(**kwargs) + @included = self.class::Included.new([*kwargs.delete(:included)]) @data = self.class::Data.new(**kwargs) end + + def to_h + { + data: data.to_h, + included: included.to_a + } + end end end end diff --git a/spec/app_store_connect/request/body_spec.rb b/spec/app_store_connect/request/body_spec.rb index 1659c90..b2dc22e 100644 --- a/spec/app_store_connect/request/body_spec.rb +++ b/spec/app_store_connect/request/body_spec.rb @@ -4,6 +4,7 @@ let(:klass) do Class.new(described_class) do include AppStoreConnect::Object::Data + include AppStoreConnect::Object::Included data do attributes do @@ -18,6 +19,6 @@ subject { instance.to_h } describe '#to_h' do - it { is_expected.to eq({ data: { attributes: { bar: :baz } } }) } + it { is_expected.to eq({ included: [{ type: :foo }], data: { attributes: { bar: :baz } } }) } end end diff --git a/spec/bundle_id_create_request_spec.rb b/spec/bundle_id_create_request_spec.rb index d09802f..7ab58a7 100644 --- a/spec/bundle_id_create_request_spec.rb +++ b/spec/bundle_id_create_request_spec.rb @@ -17,7 +17,8 @@ name: attributes.name, platform: attributes.platform } - } + }, + included: [] ) end end @@ -36,7 +37,8 @@ seed_id: attributes.seed_id, platform: attributes.platform } - } + }, + included: [] ) end end