Skip to content

Commit

Permalink
Merge pull request #11 from IanKeen/event_refactor
Browse files Browse the repository at this point in the history
Event refactor
  • Loading branch information
IanKeen committed Jul 20, 2016
2 parents bc2251c + 2e254f0 commit a21d28e
Show file tree
Hide file tree
Showing 51 changed files with 459 additions and 902 deletions.
26 changes: 0 additions & 26 deletions Sources/RTMAPI/Events/ChannelArchiveBuilder.swift

This file was deleted.

59 changes: 59 additions & 0 deletions Sources/RTMAPI/Events/ChannelBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// ChannelBuilder.swift
// Chameleon
//
// Created by Ian Keen on 20/07/2016.
//
//

import Models
import Vapor

private enum ChannelEvent: String, RTMAPIEventBuilderEventType {
case
channel_marked, channel_created, channel_joined, channel_left,
channel_deleted, channel_archive, channel_unarchive

static var all: [ChannelEvent] {
return [.channel_marked, .channel_created, .channel_joined, .channel_left,
.channel_deleted, .channel_archive, .channel_unarchive]
}
}

/// Handler for the channel events
struct ChannelBuilder: RTMAPIEventBuilder {
static var eventTypes: [String] { return ChannelEvent.all.map({ $0.rawValue }) }

static func make(withJson json: JSON, builderFactory: (json: JSON) -> SlackModelBuilder) throws -> RTMAPIEvent {
guard let event = ChannelEvent.eventType(fromJson: json)
else { throw RTMAPIEventBuilderError.invalidBuilder(builder: self) }

let builder = builderFactory(json: json)

switch event {
case .channel_marked:
return .channel_marked(
channel: try builder.slackModel("channel"),
timestamp: try builder.property("ts")
)
case .channel_created:
return .channel_created(channel: try builder.property("channel"))
case .channel_joined:
return .channel_joined(channel: try builder.property("channel"))
case .channel_left:
return .channel_left(channel: try builder.slackModel("channel"))
case .channel_deleted:
return .channel_deleted(channel: try builder.slackModel("channel"))
case .channel_archive:
return .channel_archive(
channel: try builder.slackModel("channel"),
user: try builder.slackModel("user")
)
case .channel_unarchive:
return .channel_archive(
channel: try builder.slackModel("channel"),
user: try builder.slackModel("user")
)
}
}
}
23 changes: 0 additions & 23 deletions Sources/RTMAPI/Events/ChannelCreatedBuilder.swift

This file was deleted.

23 changes: 0 additions & 23 deletions Sources/RTMAPI/Events/ChannelDeletedBuilder.swift

This file was deleted.

23 changes: 0 additions & 23 deletions Sources/RTMAPI/Events/ChannelJoinedBuilder.swift

This file was deleted.

23 changes: 0 additions & 23 deletions Sources/RTMAPI/Events/ChannelLeftBuilder.swift

This file was deleted.

26 changes: 0 additions & 26 deletions Sources/RTMAPI/Events/ChannelMarkedBuilder.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/RTMAPI/Events/ChannelRenameBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Vapor

/// Handler for the `channel_rename` event
struct ChannelRenameBuilder: RTMAPIEventBuilder {
static var eventType: String { return "channel_rename" }
static var eventTypes: [String] { return ["channel_rename"] }

static func make(withJson json: JSON, builderFactory: (json: JSON) -> SlackModelBuilder) throws -> RTMAPIEvent {
guard self.canMake(fromJson: json) else { throw RTMAPIEventBuilderError.invalidBuilder(builder: self) }
Expand Down
26 changes: 0 additions & 26 deletions Sources/RTMAPI/Events/ChannelUnarchiveBuilder.swift

This file was deleted.

36 changes: 36 additions & 0 deletions Sources/RTMAPI/Events/DNDBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// DNDBuilder.swift
// Chameleon
//
// Created by Ian Keen on 20/07/2016.
//
//

import Models
import Vapor

private enum DNDEvent: String, RTMAPIEventBuilderEventType {
case dnd_updated, dnd_updated_user

static var all: [DNDEvent] { return [.dnd_updated, .dnd_updated_user] }
}

/// Handler for the dnd events
struct DNDBuilder: RTMAPIEventBuilder {
static var eventTypes: [String] { return DNDEvent.all.map({ $0.rawValue }) }

static func make(withJson json: JSON, builderFactory: (json: JSON) -> SlackModelBuilder) throws -> RTMAPIEvent {
guard let event = DNDEvent.eventType(fromJson: json)
else { throw RTMAPIEventBuilderError.invalidBuilder(builder: self) }

let builder = builderFactory(json: json)

let user: User = try builder.slackModel("user")
let status: DNDStatus = try builder.property("dnd_status")

switch event {
case .dnd_updated: return .dnd_updated(user: user, status: status)
case .dnd_updated_user: return .dnd_updated_user(user: user, status: status)
}
}
}
26 changes: 0 additions & 26 deletions Sources/RTMAPI/Events/DNDUpdatedBuilder.swift

This file was deleted.

26 changes: 0 additions & 26 deletions Sources/RTMAPI/Events/DNDUpdatedUserBuilder.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/RTMAPI/Events/ErrorBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Vapor

/// Handler for the `error` event
struct ErrorBuilder: RTMAPIEventBuilder {
static var eventType: String { return "error" }
static var eventTypes: [String] { return ["error"] }

static func make(withJson json: JSON, builderFactory: (json: JSON) -> SlackModelBuilder) throws -> RTMAPIEvent {
guard self.canMake(fromJson: json) else { throw RTMAPIEventBuilderError.invalidBuilder(builder: self) }
Expand Down

0 comments on commit a21d28e

Please sign in to comment.