Skip to content

Commit

Permalink
Add all metadata fields in iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinAkram committed Jan 19, 2024
1 parent a858ea3 commit 7efc706
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 8 deletions.
98 changes: 96 additions & 2 deletions Sources/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,60 @@ public class ParselyMetadata {
*/
public init(canonical_url: String? = nil,
pub_date: Date? = nil,
save_date: Date? = nil,
title: String? = nil,
authors: Array<String>? = nil,
image_url: String? = nil,
section: String? = nil,
tags: Array<String>? = nil,
duration: TimeInterval? = nil,
page_type: String? = nil) {
page_type: String? = nil
urls: String? = nil,
post_id: String? = nil,
pub_date_tmsp: Date? = nil,
custom_metadata: String? = nil,
save_date_tmsp: Date? = nil,
thumb_url: String? = nil,
full_content_word_count: Int? = nil,
share_urls: Array<String>? = nil,
data_source: String? = nil,
canonical_hash: String? = nil,
canonical_hash64: String? = nil,
video_platform: String? = nil,
language: String? = nil,
full_content: String? = nil,
full_content_sha512: String? = nil,
network_id_str: String? = nil,
network_canonical: String? = nil,
content_enrichments: Dictionary<String, Any>? = nil) {
self.canonical_url = canonical_url
self.pub_date = pub_date
self.save_date = save_date
self.title = title
self.authors = authors
self.image_url = image_url
self.section = section
self.tags = tags
self.duration = duration
self.page_type = page_type
self.urls = urls
self.post_id = post_id
self.pub_date_tmsp = pub_date_tmsp
self.custom_metadata = custom_metadata
self.save_date_tmsp = save_date_tmsp
self.thumb_url = thumb_url
self.full_content_word_count = full_content_word_count
self.share_urls = share_urls
self.data_source = data_source
self.canonical_hash = canonical_hash
self.canonical_hash64 = canonical_hash64
self.video_platform = video_platform
self.language = language
self.full_content = full_content
self.full_content_sha512 = full_content_sha512
self.network_id_str = network_id_str
self.network_canonical = network_canonical
self.content_enrichments = content_enrichments
}

func toDict() -> Dictionary<String, Any> {
Expand All @@ -54,6 +92,9 @@ public class ParselyMetadata {
if let pub_date {
metas["pub_date"] = String(format:"%i", pub_date.millisecondsSince1970)
}
if let save_date {
metas["save_date"] = String(format:"%i", save_date.millisecondsSince1970)
}
if let title {
metas["title"] = title
}
Expand All @@ -75,7 +116,60 @@ public class ParselyMetadata {
if let page_type {
metas["page_type"] = page_type
}

if let urls {
metas["urls"] = urls
}
if let post_id {
metas["post_id"] = post_id
}
if let pub_date_tmsp {
metas["pub_date_tmsp"] = String(format:"%i", pub_date_tmsp.millisecondsSince1970)
}
if let custom_metadata {
metas["custom_metadata"] = custom_metadata
}
if let save_date_tmsp {
metas["save_date_tmsp"] = String(format:"%i", save_date_tmsp.millisecondsSince1970)
}
if let thumb_url {
metas["thumb_url"] = thumb_url
}
if let full_content_word_count {
metas["full_content_word_count"] = full_content_word_count
}
if let share_urls {
metas["share_urls"] = share_urls
}
if let data_source {
metas["data_source"] = data_source
}
if let canonical_hash {
metas["canonical_hash"] = canonical_hash
}
if let canonical_hash64 {
metas["canonical_hash64"] = canonical_hash64
}
if let video_platform {
metas["video_platform"] = video_platform
}
if let language {
metas["language"] = language
}
if let full_content {
metas["full_content"] = full_content
}
if let full_content_sha512 {
metas["full_content_sha512"] = full_content_sha512
}
if let network_id_str {
metas["network_id_str"] = network_id_str
}
if let network_canonical {
metas["network_canonical"] = network_canonical
}
if let content_enrichments {
metas["content_enrichments"] = content_enrichments
}
return metas
}
}
100 changes: 97 additions & 3 deletions Tests/MetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,32 @@ class MetadataTests: XCTestCase {
let expected: Dictionary<String, Any> = [
"canonical_url": "http://parsely-test.com",
"pub_date": Date(),
"save_date": Date(),
"title": "a title.",
"authors": ["Yogi Berra"],
"image_url": "http://parsely-test.com/image2",
"section": "Things my mother says",
"tags": ["tag1", "tag2"],
"duration": TimeInterval(100),
"page_type": "post"
"page_type": "post",
"urls": "http://parsely-test.com",
"post_id": "1",
"pub_date_tmsp": Date(),
"custom_metadata": "hedgehogs",
"save_date_tmsp": Date(),
"thumb_url": "http://parsely-test.com/image2",
"full_content_word_count": 100,
"share_urls": ["http://parsely-test.com"],
"data_source": "the moon",
"canonical_hash": "hash_browns",
"canonical_hash64": "hash_browns64",
"video_platform": "youtube",
"language": "en",
"full_content": "the full content of the article",
"full_content_sha512": "what is this?",
"network_id_str": "abc",
"network_canonical": "network canonical"

]

func testToDictEmpty() {
Expand All @@ -33,16 +52,37 @@ class MetadataTests: XCTestCase {
let metasUnderTest = ParselyMetadata(
canonical_url: expected["canonical_url"] as? String,
pub_date: expected["pub_date"] as? Date,
save_date: expected["save_date"] as? Date,
title: expected["title"] as? String,
authors: expected["authors"] as? Array<String>,
image_url: expected["image_url"] as? String,
section: expected["section"] as? String,
tags: expected["tags"] as? Array<String>,
duration: expected["duration"] as? TimeInterval,
page_type: expected["page_type"] as? String
page_type: expected["page_type"] as? String,
urls: expected["urls"] as? String,
post_id: expected["post_id"] as? String,
pub_date_tmsp: expected["pub_date_tmsp"] as? Date,
custom_metadata: expected["custom_metadata"] as? String,
save_date_tmsp: expected["save_date_tmsp"] as? Date,
thumb_url: expected["thumb_url"] as? String,
full_content_word_count: expected["full_content_word_count"] as? Int,
share_urls: expected["share_urls"] as? Array<String>,
data_source: expected["data_source"] as? String,
canonical_hash: expected["canonical_hash"] as? String,
canonical_hash64: expected["canonical_hash64"] as? String,
video_platform: expected["video_platform"] as? String,
language: expected["language"] as? String,
full_content: expected["full_content"] as? String,
full_content_sha512: expected["full_content_sha512"] as? String,
network_id_str: expected["network_id_str"] as? String,
network_canonical: expected["network_canonical"] as? String
)
let actual: Dictionary<String, Any> = metasUnderTest.toDict()
let pubDateUnix: String = String(format:"%i", (expected["pub_date"]! as! Date).millisecondsSince1970)
let saveDateUnix: String = String(format:"%i", (expected["save_date"]! as! Date).millisecondsSince1970)
let pubDateTmspUnix: String = String(format:"%i", (expected["pub_date_tmsp"]! as! Date).millisecondsSince1970)
let saveDateTmspUnix: String = String(format:"%i", (expected["save_date_tmsp"]! as! Date).millisecondsSince1970)
XCTAssertFalse(actual.isEmpty, "Creating a ParselyMetadataobject with many parameters results in a " +
"non-empty object")
XCTAssertEqual(actual["link"]! as! String, expected["canonical_url"]! as! String,
Expand All @@ -51,6 +91,9 @@ class MetadataTests: XCTestCase {
XCTAssertEqual(actual["pub_date"]! as! String, pubDateUnix,
"The pub_date field in the result of ParselyMetadata.toDict should match the pub_date argument " +
"used at initialization")
XCTAssertEqual(actual["save_date"]! as! String, pubDateUnix,
"The save_date field in the result of ParselyMetadata.toDict should match the save_date argument " +
"used at initialization")
XCTAssertEqual(actual["title"]! as! String, expected["title"]! as! String,
"The title field in the result of ParselyMetadata.toDict should match the title argument " +
"used at initialization")
Expand All @@ -71,7 +114,58 @@ class MetadataTests: XCTestCase {
"used at initialization")
XCTAssertEqual(actual["page_type"]! as! String, expected["page_type"]! as! String,
"The page_type field in the result of ParselyMetadata.toDict should match the page_type argument " +
"used at initialization")
"used at initialization")
XCTAssertEqual(actual["urls"]! as! String, expected["urls"]! as! String,
"The urls field in the result of ParselyMetadata.toDict should match the urls argument " +
"used at initialization")
XCTAssertEqual(actual["post_id"]! as! String, expected["post_id"]! as! String,
"The post_id field in the result of ParselyMetadata.toDict should match the post_id argument " +
"used at initialization")
XCTAssertEqual(actual["pub_date_tmsp"]! as! String, pubDateTmspUnix,
"The pub_date_tmsp field in the result of ParselyMetadata.toDict should match the pub_date_tmsp argument " +
"used at initialization")
XCTAssertEqual(actual["custom_metadata"]! as! String, expected["custom_metadata"]! as! String,
"The custom_metadata field in the result of ParselyMetadata.toDict should match the custom_metadata argument " +
"used at initialization")
XCTAssertEqual(actual["save_date_tmsp"]! as! String, saveDateTmspUnix,
"The save_date_tmsp field in the result of ParselyMetadata.toDict should match the save_date_tmsp argument " +
"used at initialization")
XCTAssertEqual(actual["thumb_url"]! as! String, expected["thumb_url"]! as! String,
"The thumb_url field in the result of ParselyMetadata.toDict should match the thumb_url argument " +
"used at initialization")
XCTAssertEqual(actual["full_content_word_count"]! as! Int, expected["full_content_word_count"]! as! Int,
"The full_content_word_count field in the result of ParselyMetadata.toDict should match the full_content_word_count argument " +
"used at initialization")
XCTAssertEqual(actual["share_urls"]! as! Array<String>, expected["share_urls"]! as! Array<String>,
"The share_urls field in the result of ParselyMetadata.toDict should match the share_urls argument " +
"used at initialization")
XCTAssertEqual(actual["data_source"]! as! String, expected["data_source"]! as! String,
"The data_source field in the result of ParselyMetadata.toDict should match the data_source argument " +
"used at initialization")
XCTAssertEqual(actual["canonical_hash"]! as! String, expected["canonical_hash"]! as! String,
"The canonical_hash field in the result of ParselyMetadata.toDict should match the canonical_hash argument " +
"used at initialization")
XCTAssertEqual(actual["canonical_hash64"]! as! String, expected["canonical_hash64"]! as! String,
"The canonical_hash64 field in the result of ParselyMetadata.toDict should match the canonical_hash64 argument " +
"used at initialization")
XCTAssertEqual(actual["video_platform"]! as! String, expected["video_platform"]! as! String,
"The video_platform field in the result of ParselyMetadata.toDict should match the video_platform argument " +
"used at initialization")
XCTAssertEqual(actual["language"]! as! String, expected["language"]! as! String,
"The language field in the result of ParselyMetadata.toDict should match the language argument " +
"used at initialization")
XCTAssertEqual(actual["full_content"]! as! String, expected["full_content"]! as! String,
"The full_content field in the result of ParselyMetadata.toDict should match the full_content argument " +
"used at initialization")
XCTAssertEqual(actual["full_content_sha512"]! as! String, expected["full_content_sha512"]! as! String,
"The full_content_sha512 field in the result of ParselyMetadata.toDict should match the full_content_sha512 argument " +
"used at initialization")
XCTAssertEqual(actual["network_id_str"]! as! String, expected["network_id_str"]! as! String,
"The network_id_str field in the result of ParselyMetadata.toDict should match the network_id_str argument " +
"used at initialization")
XCTAssertEqual(actual["network_canonical"]! as! String, expected["network_canonical"]! as! String,
"The network_canonical field in the result of ParselyMetadata.toDict should match the network_canonical argument " +
"used at initialization")
}

func testMetadata() {
Expand Down
21 changes: 20 additions & 1 deletion Tests/RequestBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,32 @@ class RequestBuilderTests: XCTestCase {
let exampleMetadata: ParselyMetadata = ParselyMetadata(
canonical_url:"http://parsely-test.com",
pub_date: Date(timeIntervalSince1970: 3),
save_date: Date(timeIntervalSince1970: 4),
title: "a title.",
authors: ["Yogi Berra"],
image_url: "http://parsely-test.com/image2",
section: "Things my mother says",
tags: ["tag1", "tag2"],
duration: TimeInterval(100),
page_type: "post"
page_type: "post",
urls: ["http://parsely-test.com/1", "http://parsely-test.com/2"],
post_id: "1",
pub_date_tmsp: Date(timeIntervalSince1970: 5),
custom_metadata: "custom",
save_date_tmsp: Date(timeIntervalSince1970: 6),
thumb_url: "http://parsely-test.com/thumb",
full_content_word_count: 1000,
share_urls: ["http://parsely-test.com/share1", "http://parsely-test.com/share2"],
data_source: "test",
canonical_hash: "hash",
canonical_hash64: "hash64",
video_platform: "youtube",
language: "en",
full_content: "full content",
full_content_sha512: "full content sha512",
network_id_str: "network id",
network_canonical: "http://parsely-test.com/network_canonical",
content_enrichments: {"enrichment1": "value1", "enrichment2": "value2"}
)
return [Event(
"pageview",
Expand Down
43 changes: 41 additions & 2 deletions Tests/VideoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,32 @@ class VideoTests: ParselyTestCase {
let firstTestMetadata = ParselyMetadata(
canonical_url: testUrl,
pub_date: Date(),
save_date: Date(),
title: "test",
authors: nil,
image_url: nil,
section: testSectionFirst,
tags: nil,
duration: nil,
page_type: nil
page_type: "post",
urls: nil,
post_id: nil,
pub_date_tmsp: nil,
custom_metadata: nil,
save_date_tmsp: nil,
thumb_url: nil,
full_content_word_count: nil,
share_urls: nil,
data_source: nil,
canonical_hash: nil,
canonical_hash64: nil,
video_platform: nil,
language: nil,
full_content: nil,
full_content_sha512: nil,
network_id_str: nil,
network_canonical: nil,
content_enrichments: nil
)

videoManager.trackPlay(
Expand All @@ -100,13 +119,33 @@ class VideoTests: ParselyTestCase {
let secondTestMetadata = ParselyMetadata(
canonical_url: testUrl,
pub_date: Date(),
save_date: Date(),
title: "test",
authors: nil,
image_url: nil,
section: testSectionSecond,
tags: nil,
duration: nil,
page_type: nil
page_type: nil,
page_type: "post",
urls: nil,
post_id: nil,
pub_date_tmsp: nil,
custom_metadata: nil,
save_date_tmsp: nil,
thumb_url: nil,
full_content_word_count: nil,
share_urls: nil,
data_source: nil,
canonical_hash: nil,
canonical_hash64: nil,
video_platform: nil,
language: nil,
full_content: nil,
full_content_sha512: nil,
network_id_str: nil,
network_canonical: nil,
content_enrichments: nil
)

videoManager.trackPlay(
Expand Down

0 comments on commit 7efc706

Please sign in to comment.