Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/facebook_ads/field_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/facebook_ads/field_types/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions spec/field_types/object_spec.rb
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions spec/file_types_spec.rb
Original file line number Diff line number Diff line change
@@ -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<string, int> field type is given' do
let(:type) { 'map<string, int>' }
it { expect(lookup).to eq(FacebookAds::FieldTypes::Object) }
end

context 'when map<string, string> field type is given' do
let(:type) { 'map<string, string>' }
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