Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapping: array field should return empty array rather than error when…
… the field is missing
- Loading branch information
Showing
3 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require "./spec_helper" | ||
|
||
module Example::Facebook::AdCreatives | ||
# account.adcreatives(fields: %w( title object_story_id object_story_spec )) | ||
STR = <<-EOF | ||
[ | ||
{ | ||
"title": "abc", | ||
"object_story_spec": { | ||
"page_id": "012345...", | ||
"link_data": { | ||
"link": "http://example.com/", | ||
"message": "advertise message", | ||
"name": "title", | ||
"attachment_style": "link", | ||
"image_hash": "c9e4c9...", | ||
"call_to_action": { | ||
"type": "LEARN_MORE" | ||
}, | ||
"child_attachments": [ | ||
{ | ||
"link": "http://example.com/1", | ||
"image_hash": "405bf...", | ||
"name": "title of carousel1", | ||
"call_to_action": { | ||
"type": "LEARN_MORE" | ||
} | ||
}, | ||
{ | ||
"link": "http://example.com/2", | ||
"image_hash": "9cef4...", | ||
"name": "title of carousel2", | ||
"call_to_action": { | ||
"type": "LEARN_MORE" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] | ||
EOF | ||
|
||
class Attachment | ||
Jq.mapping({ | ||
link: String, | ||
name: String, | ||
}) | ||
end | ||
|
||
class LinkData | ||
Jq.mapping({ | ||
link: String, | ||
message: String, | ||
name: String, | ||
attachments: {Array(Attachment), ".child_attachments"}, | ||
}) | ||
end | ||
|
||
class AdCreative | ||
Jq.mapping({ | ||
title: String, | ||
link_data: {LinkData, ".object_story_spec.link_data"}, | ||
}) | ||
end | ||
|
||
describe "Example::Facebook::AdCreatives" do | ||
it "works" do | ||
creatives = Array(AdCreative).from_json(STR) | ||
link = creatives[0].link_data | ||
link.attachments.map(&.link).should eq(["http://example.com/1","http://example.com/2"]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "./spec_helper" | ||
|
||
module Example::Facebook::AdImages | ||
# account.adimages(fields: ["name", "status", "creatives"]) | ||
STR = <<-EOF | ||
[ | ||
{ | ||
"name": "abc", | ||
"status": "ACTIVE", | ||
"creatives": ["123456","789012"] | ||
}, | ||
{ | ||
"name": "xyz", | ||
"status": "ACTIVE" | ||
} | ||
] | ||
EOF | ||
|
||
class AdImage | ||
Jq.mapping({ | ||
name: String, | ||
status: String, | ||
creatives: {Array(String), ".creatives"} | ||
}) | ||
end | ||
|
||
describe "Example::Facebook::AdImages" do | ||
it "works with array field" do | ||
images = Array(AdImage).from_json(STR) | ||
images[0].creatives.should eq(["123456","789012"]) | ||
end | ||
|
||
it "return empty array when array field is missing" do | ||
images = Array(AdImage).from_json(STR) | ||
images[1].creatives.should eq(Array(String).new) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters