Skip to content

Commit

Permalink
Add title and url to attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
Koray Koska committed Jul 16, 2017
1 parent 40484d4 commit 0d91633
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Sources/VaporFacebookBot/Webhooks/FacebookAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@ public final class FacebookAttachment: JSONConvertible {
public var type: FacebookAttachmentType
public var payload: FacebookAttachmentPayload

public var title: String?
public var url: String?

public init(json: JSON) throws {
guard let type = FacebookAttachmentType(rawValue: try json.get("type")) else {
throw Abort(.badRequest, metadata: "type is not a valid FacebookAttachmentType in FacebookAttachement")
}
self.type = type
self.payload = try FacebookAttachmentPayload(json: json.get("payload"))

self.title = json["title"]?.string
self.url = json["url"]?.string
}

public func makeJSON() throws -> JSON {
var json = JSON()
try json.set("type", type.rawValue)
try json.set("payload", try payload.makeJSON())

if let title = title {
try json.set("title", title)
}
if let url = url {
try json.set("url", url)
}

return json
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ public final class FacebookAttachmentPayload: JSONConvertible {
self.url = url
}

public init(coordinatesLat: Double, coordinatesLong: Double, url: String? = nil) {
public init(coordinatesLat: Double, coordinatesLong: Double) {
self.coordinatesLat = coordinatesLat
self.coordinatesLong = coordinatesLong
self.url = url
}

public convenience init(json: JSON) throws {
Expand All @@ -31,7 +30,7 @@ public final class FacebookAttachmentPayload: JSONConvertible {
let coordinatesLong = json["coordinates"]?["long"]?.double

if let coordinatesLat = coordinatesLat, let coordinatesLong = coordinatesLong {
self.init(coordinatesLat: coordinatesLat, coordinatesLong: coordinatesLong, url: url)
self.init(coordinatesLat: coordinatesLat, coordinatesLong: coordinatesLong)
} else if let url = url {
self.init(url: url)
} else {
Expand Down

0 comments on commit 0d91633

Please sign in to comment.