Skip to content

Commit

Permalink
Refactor apis
Browse files Browse the repository at this point in the history
  • Loading branch information
muratayusuke committed Feb 21, 2016
1 parent a1f0287 commit 12cadee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
15 changes: 8 additions & 7 deletions SlackKit/Sources/ClientExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ extension Client {
api.imOpen(id, completion: completion)
}
}

public func getChannelHistory(channelID: String, completion: (messages: [Message]) -> Void) {
api.fetchChannelHistory(channelID) { (response) -> Void in

//MARK: - Pure APIs
public func channelsHistory(channelID: String, options: [String: AnyObject] = [String: AnyObject](), completion: (messages: [Message]) -> Void) {
api.channelsHistory(channelID, options: options) { (response) -> Void in
if let messages = response["messages"] as? [[String: AnyObject]] {
let messageObjects: [Message] = messages.map({ (m) -> Message in
return Message.init(message: m)!
Expand All @@ -49,8 +50,8 @@ extension Client {
}
}

public func getIMHistory(channelID: String, completion: (messages: [Message]) -> Void) {
api.fetchIMHistory(channelID) { (response) -> Void in
public func imHistory(channelID: String, options: [String: AnyObject] = [String: AnyObject](), completion: (messages: [Message]) -> Void) {
api.imHistory(channelID, options: options) { (response) -> Void in
if let messages = response["messages"] as? [[String: AnyObject]] {
let messageObjects: [Message] = messages.map({ (m) -> Message in
return Message.init(message: m)!
Expand All @@ -60,8 +61,8 @@ extension Client {
}
}

public func postMessage(channelID: String, message: String, completion: (response: [String: AnyObject]) -> Void) {
api.postMessage(channelID, message: message) { (response) -> Void in
public func chatPostMessage(channelID: String, message: String, options: [String: AnyObject] = [String: AnyObject](), completion: (response: [String: AnyObject]) -> Void) {
api.chatPostMessage(channelID, message: message, options: options) { (response) -> Void in
completion(response: response)
}
}
Expand Down
32 changes: 25 additions & 7 deletions SlackKit/Sources/NetworkInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public struct NetworkInterface {
for key in parameters.keys {
if let value = parameters[key] as? String {
requestString = requestString + "&\(key)=\(value)"
} else if let value = parameters[key] as? Int {
requestString = requestString + "&\(key)=\(value)"
}
}

Expand Down Expand Up @@ -143,30 +145,46 @@ public struct NetworkInterface {
}) { (SlackError) -> Void in }
}

public func fetchChannelHistory(channelID: String, completion: (response: [String: AnyObject]) -> Void) {
// https://api.slack.com/methods/channels.history
public func channelsHistory(channelID: String, options: [String: AnyObject], completion: (response: [String: AnyObject]) -> Void) {
let params = options + ["channel": channelID]
request(
SlackAPIEndpoint.ChannelsHistory,
parameters: ["channel": channelID],
parameters: params,
successClosure: { (response) -> Void in
completion(response: response)
}) { (SlackError) -> Void in }
}

public func fetchIMHistory(channelID: String, completion: (response: [String: AnyObject]) -> Void) {

// https://api.slack.com/methods/im.history
public func imHistory(channelID: String, options: [String: AnyObject], completion: (response: [String: AnyObject]) -> Void) {
let params = options + ["channel": channelID]
request(
SlackAPIEndpoint.IMHistory,
parameters: ["channel": channelID],
parameters: params,
successClosure: { (response) -> Void in
completion(response: response)
}) { (SlackError) -> Void in }
}

public func postMessage(channelID: String, message: String, completion: (response: [String: AnyObject]) -> Void) {
// https://api.slack.com/methods/chat.postMessage
public func chatPostMessage(channelID: String, message: String, options: [String: AnyObject], completion: (response: [String: AnyObject]) -> Void) {
let params = options + ["channel": channelID, "text": message]
request(
SlackAPIEndpoint.ChatPostMessage,
parameters: ["channel": channelID, "text": message, "as_user": "true"],
parameters: params,
successClosure: { (response) -> Void in
completion(response: response)
}) { (SlackError) -> Void in }
}
}

// merge dictionary
public func + <K,V> (left: Dictionary<K,V>, right: Dictionary<K,V>?) -> Dictionary<K,V> {
guard let right = right else { return left }
return left.reduce(right) {
var new = $0 as [K:V]
new.updateValue($1.1, forKey: $1.0)
return new
}
}

0 comments on commit 12cadee

Please sign in to comment.