You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.9 KiB

6 years ago
  1. // Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. import FBSDKShareKit
  19. @testable import FacebookCore
  20. /**
  21. An Open Graph Object for sharing.
  22. The property keys MUST have namespaces specified on them, such as `og:image`, and `og:type` is required.
  23. See https://developers.facebook.com/docs/sharing/opengraph/object-properties for other properties.
  24. You can specify nested namespaces inline to define complex properties. For example, the following code will generate a
  25. fitness.course object with a location:
  26. ```
  27. let course: OpenGraphObject = [
  28. "og:type": "fitness.course",
  29. "og:title": "Sample course",
  30. "fitness:metrics:location:latitude": "41.40338",
  31. "fitness:metrics:location:longitude": "2.17403",
  32. ]
  33. ```
  34. */
  35. public struct OpenGraphObject {
  36. fileprivate var properties: [OpenGraphPropertyName : OpenGraphPropertyValue]
  37. /**
  38. Create a new `OpenGraphObject`.
  39. */
  40. public init() {
  41. properties = [:]
  42. }
  43. }
  44. extension OpenGraphObject: OpenGraphPropertyContaining {
  45. /// Get the property names contained in this container.
  46. public var propertyNames: Set<OpenGraphPropertyName> {
  47. return Set(properties.keys)
  48. }
  49. public subscript(key: OpenGraphPropertyName) -> OpenGraphPropertyValue? {
  50. get {
  51. return properties[key]
  52. } set {
  53. properties[key] = newValue
  54. }
  55. }
  56. }
  57. extension OpenGraphObject: ExpressibleByDictionaryLiteral {
  58. /**
  59. Convenience method to build a new object from a dictinary literal.
  60. - parameter elements: The elements of the dictionary literal to initialize from.
  61. - example:
  62. ```
  63. let object: OpenGraphObject = [
  64. "og:type": "foo",
  65. "og:title": "bar",
  66. ....
  67. ]
  68. ```
  69. */
  70. public init(dictionaryLiteral elements: (OpenGraphPropertyName, OpenGraphPropertyValue)...) {
  71. properties = [:]
  72. for (key, value) in elements {
  73. properties[key] = value
  74. }
  75. }
  76. }
  77. extension OpenGraphObject {
  78. internal var sdkGraphObjectRepresentation: FBSDKShareOpenGraphObject {
  79. let sdkObject = FBSDKShareOpenGraphObject()
  80. sdkObject.parseProperties(properties.keyValueMap { key, value in
  81. (key.rawValue, value.openGraphPropertyValue)
  82. })
  83. return sdkObject
  84. }
  85. internal init(sdkGraphObject: FBSDKShareOpenGraphObject) {
  86. var properties = [OpenGraphPropertyName : OpenGraphPropertyValue]()
  87. sdkGraphObject.enumerateKeysAndObjects { (key: String?, value: Any?, stop) in
  88. guard let key = key.map(OpenGraphPropertyName.init(rawValue:)),
  89. let value = value.map(OpenGraphPropertyValueConverter.valueFrom) else {
  90. return
  91. }
  92. properties[key] = value
  93. }
  94. self.properties = properties
  95. }
  96. }
  97. extension OpenGraphObject: Equatable {
  98. /**
  99. Compare two `OpenGraphObject`s for equality.
  100. - parameter lhs: The first `OpenGraphObject` to compare.
  101. - parameter rhs: The second `OpenGraphObject` to compare.
  102. - returns: Whether or not the objects are equal.
  103. */
  104. public static func == (lhs: OpenGraphObject, rhs: OpenGraphObject) -> Bool {
  105. return false
  106. }
  107. }