Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update call a method with given params on AppDelegate. #3

Merged
merged 1 commit into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Insider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
CD3305C41C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305A71C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.m */; };
CD3305C51C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305A81C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h */; settings = {ATTRIBUTES = (Public, ); }; };
CD3305C61C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305A91C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.m */; };
CD3305C71C737BB000494BB9 /* GCDWebServerDataResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305AB1C737BB000494BB9 /* GCDWebServerDataResponse.h */; };
CD3305C71C737BB000494BB9 /* GCDWebServerDataResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305AB1C737BB000494BB9 /* GCDWebServerDataResponse.h */; settings = {ATTRIBUTES = (Public, ); }; };
CD3305C81C737BB000494BB9 /* GCDWebServerDataResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305AC1C737BB000494BB9 /* GCDWebServerDataResponse.m */; };
CD3305C91C737BB000494BB9 /* GCDWebServerErrorResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305AD1C737BB000494BB9 /* GCDWebServerErrorResponse.h */; };
CD3305CA1C737BB000494BB9 /* GCDWebServerErrorResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305AE1C737BB000494BB9 /* GCDWebServerErrorResponse.m */; };
Expand Down Expand Up @@ -294,8 +294,8 @@
CD3305C51C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h in Headers */,
CD3305BF1C737BB000494BB9 /* GCDWebServerDataRequest.h in Headers */,
CD3305BB1C737BB000494BB9 /* GCDWebServerRequest.h in Headers */,
CD3305C11C737BB000494BB9 /* GCDWebServerFileRequest.h in Headers */,
CD3305C71C737BB000494BB9 /* GCDWebServerDataResponse.h in Headers */,
CD3305C11C737BB000494BB9 /* GCDWebServerFileRequest.h in Headers */,
CD3305B71C737BB000494BB9 /* GCDWebServerFunctions.h in Headers */,
CD3305B51C737BB000494BB9 /* GCDWebServerConnection.h in Headers */,
CD3305BA1C737BB000494BB9 /* GCDWebServerPrivate.h in Headers */,
Expand Down
3 changes: 2 additions & 1 deletion Insider/Insider.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ FOUNDATION_EXPORT double InsiderVersionNumber;
FOUNDATION_EXPORT const unsigned char InsiderVersionString[];

#import "GCDWebServer.h"
#import "GCDWebServerURLEncodedFormRequest.h"
#import "GCDWebServerURLEncodedFormRequest.h"
#import "GCDWebServerDataResponse.h"
37 changes: 27 additions & 10 deletions Insider/Insider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ import UIKit
@objc
final public class Insider: NSObject {

enum StatusCodes: Int {
case Success = 200
case NotFound = 404
}

struct Constants {
static let defaultPort: UInt = 8080
static let defaultInsiderMethodSelector = Selector("insider:")
}

public static let sharedInstance = Insider()

public var appDelegateInsiderSelector: Selector?
public lazy var appDelegateInsiderSelector: Selector = {
return Constants.defaultInsiderMethodSelector
}()

private let localWebServer = GCDWebServer()

Expand All @@ -30,33 +37,43 @@ final public class Insider: NSObject {
// Add POST handler for x-www-form-urlencoded requests
server.addDefaultHandlerForMethod("POST", requestClass: GCDWebServerURLEncodedFormRequest.self, processBlock: { request in

var didProcessParams = false
if let request = request as? GCDWebServerURLEncodedFormRequest {
if let json = request.jsonObject {
self.processRequestParams(json, selector: selector)
} else if let params = request.arguments {
self.processRequestParams(params, selector: selector)
didProcessParams = self.processRequestParams(json, selector: selector)
} else if let encodedParams = request.arguments {
didProcessParams = self.processRequestParams(encodedParams, selector: selector)
}
}

return nil
return GCDWebServerDataResponse(statusCode: (didProcessParams) ? StatusCodes.Success.rawValue : StatusCodes.NotFound.rawValue);
})
}

private func processRequestParams(params: AnyObject, selector: Selector) {
private func canProcessRequestParams() -> Bool {
return UIApplication.sharedApplication().delegate?.respondsToSelector(appDelegateInsiderSelector) ?? false
}

private func processRequestParams(params: AnyObject, selector: Selector) -> Bool {
guard canProcessRequestParams() else {
return false
}

dispatch_sync(dispatch_get_main_queue()) { () -> Void in
UIApplication.sharedApplication().delegate?.performSelector(selector, withObject: params)
}

return true
}

// MARK - Public methods

public func start() {
startWithPort(Constants.defaultPort)
}

public func startWithPort(port: UInt) {
addHandlersForServer(localWebServer,
withAppDelegateInsiderSelector: appDelegateInsiderSelector ?? Constants.defaultInsiderMethodSelector
)

addHandlersForServer(localWebServer, withAppDelegateInsiderSelector: appDelegateInsiderSelector)
localWebServer.startWithPort(port, bonjourName: nil)
}

Expand Down