Skip to content

Instantly share code, notes, and snippets.

@lucasecf
Created November 13, 2015 11:33
Show Gist options
  • Save lucasecf/2213946048c46d1858e1 to your computer and use it in GitHub Desktop.
Save lucasecf/2213946048c46d1858e1 to your computer and use it in GitHub Desktop.
Example of a model that implements the ResponseSerializable protocol
//
// Event.swift
// EventCast
//
// Created by Lucas Eduardo on 27/10/15.
// Copyright © 2015 RNCDev. All rights reserved.
//
import Foundation
import CoreData
final class Event: CoreDataModel, CoreDataActiveRecord {
@NSManaged var hashCode: String
@NSManaged var name: String
@NSManaged var phoneUser: String
@NSManaged var status: Bool
@NSManaged var subscribersCount: Int
@NSManaged var subEvents: Set<SubEvent>
@NSManaged var subscription: Subscription
func toJson() -> [String: AnyObject] {
var json = [String: AnyObject]()
json["id"] = identifier
json["name"] = name
json["status"] = status
json["hashCode"] = hashCode
json["phoneUser"] = phoneUser
return json
}
}
//MARK: - Event Serializer
extension Event: ResponseSerializable {
class var keyPath: String? {
return "events"
}
class func build(response response: NSHTTPURLResponse, representation: AnyObject) throws -> Event {
//case when trying to create an event with a already existent hashtag
guard (representation.valueForKeyPath("available") == nil) else {
throw BuildError.ValidationError(message: NSLocalizedString("event_not_available", comment: "Validation error messaege to be shown when trying to create a new event"))
}
let event = Event()
//single properties
event.identifier = (representation.valueForKeyPath("id") as? NSNumber)?.stringValue ?? ""
event.name = (representation.valueForKeyPath("name") as? String) ?? ""
event.hashCode = (representation.valueForKeyPath("hashCode") as? String) ?? ""
event.phoneUser = (representation.valueForKeyPath("phoneUser") as? String) ?? ""
event.subscribersCount = (representation.valueForKeyPath("subscribersCount") as? NSNumber)?.integerValue ?? 0
event.status = (representation.valueForKeyPath("status") as? NSNumber)?.boolValue ?? true
//relationships
let subEventsJson = (representation.valueForKeyPath("subEventsList") as? [[String : AnyObject]]) ?? []
let subEvents = subEventsJson.map { (subEventJson) -> SubEvent in
return try! SubEvent.build(response: response, representation: subEventJson)
}
event.subEvents = Set(subEvents)
if let subscriptionJson = (representation.valueForKeyPath("subscription") as? [String : AnyObject]) {
event.subscription = try! Subscription.build(response: response, representation: subscriptionJson)
}
return event
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment