diff --git a/lib/facebook_ads/field_types.rb b/lib/facebook_ads/field_types.rb index 6288279d..24f063dc 100644 --- a/lib/facebook_ads/field_types.rb +++ b/lib/facebook_ads/field_types.rb @@ -21,14 +21,21 @@ module FacebookAds module FieldTypes def register(*type_names) - @@registry ||= {} type_names.each do |type_name| - @@registry[type_name] = self + registry[type_name] = self end end def lookup(type_name) - @@registry && @@registry[type_name] + if registry.has_key?(type_name) + registry[type_name] + elsif type_name.respond_to?(:include?) && type_name.include?('map<') + registry['map'] + end + end + + def registry + @@registry ||= {} end def for(type_spec) diff --git a/lib/facebook_ads/field_types/object.rb b/lib/facebook_ads/field_types/object.rb index b570bc46..326262bb 100644 --- a/lib/facebook_ads/field_types/object.rb +++ b/lib/facebook_ads/field_types/object.rb @@ -22,7 +22,7 @@ class Object < Base register 'hash', 'map', 'object' def deserialize(value, session = nil) - JSON.parse(value) + value.is_a?(String) ? JSON.parse(value) : value end def serialize(value) diff --git a/spec/field_types/object_spec.rb b/spec/field_types/object_spec.rb new file mode 100644 index 00000000..6756f967 --- /dev/null +++ b/spec/field_types/object_spec.rb @@ -0,0 +1,39 @@ +# Copyright (c) 2017-present, Facebook, Inc. All rights reserved. +# +# You are hereby granted a non-exclusive, worldwide, royalty-free license to use, +# copy, modify, and distribute this software in source code or binary form for use +# in connection with the web services and APIs provided by Facebook. +# +# As with any software that integrates with the Facebook platform, your use of +# this software is subject to the Facebook Platform Policy +# [http://developers.facebook.com/policy/]. This copyright notice shall be +# included in all copies or substantial portions of the software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +require 'spec_helper' + +RSpec.describe FacebookAds::FieldTypes::Object do + subject(:this) { described_class.new } + + describe '#deserialize' do + subject(:deserialize) { this.deserialize(value) } + + context 'when given value is string type' do + let(:value) { "{\"ACTIONS\":200}" } + + it { expect(deserialize).to eq({ "ACTIONS" => 200 }) } + end + + context 'when given value is hash type' do + let(:value) { { "ACTIONS" => 200 } } + + it { expect(deserialize).to eq({ "ACTIONS" => 200 }) } + end + end +end diff --git a/spec/file_types_spec.rb b/spec/file_types_spec.rb new file mode 100644 index 00000000..ade77995 --- /dev/null +++ b/spec/file_types_spec.rb @@ -0,0 +1,52 @@ +# Copyright (c) 2017-present, Facebook, Inc. All rights reserved. +# +# You are hereby granted a non-exclusive, worldwide, royalty-free license to use, +# copy, modify, and distribute this software in source code or binary form for use +# in connection with the web services and APIs provided by Facebook. +# +# As with any software that integrates with the Facebook platform, your use of +# this software is subject to the Facebook Platform Policy +# [http://developers.facebook.com/policy/]. This copyright notice shall be +# included in all copies or substantial portions of the software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +require 'spec_helper' + +RSpec.describe FacebookAds::FieldTypes do + subject(:this) { FacebookAds::FieldTypes } + + describe '.lookup' do + subject(:lookup) { this.lookup(type) } + + context 'when map field type is given' do + let(:type) { 'map' } + it { expect(lookup).to eq(FacebookAds::FieldTypes::Object) } + end + + context 'when map field type is given' do + let(:type) { 'map' } + it { expect(lookup).to eq(FacebookAds::FieldTypes::Object) } + end + + context 'when map field type is given' do + let(:type) { 'map' } + it { expect(lookup).to eq(FacebookAds::FieldTypes::Object) } + end + + context 'when object field type is given' do + let(:type) { 'object' } + it { expect(lookup).to eq(FacebookAds::FieldTypes::Object) } + end + + context 'when hash field type is given' do + let(:type) { 'hash' } + it { expect(lookup).to eq(FacebookAds::FieldTypes::Object) } + end + end +end