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

Allow to send action and object #5

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 ACActionCable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

Pod::Spec.new do |spec|
spec.name = 'ACActionCable'
spec.version = '2.1.2'
spec.version = '2.2.0'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.homepage = 'https://github.com/High5Apps/ACActionCable'
spec.authors = { 'Julian Tigler' => 'high5apps@gmail.com', 'Fabian Jäger' => 'fabian@mailbutler.io' }
spec.summary = 'A well-tested, dependency-free Action Cable client for Rails 6'
spec.source = { :git => 'https://github.com/High5Apps/ACActionCable.git', :tag => 'v2.1.2' }
spec.source = { :git => 'https://github.com/High5Apps/ACActionCable.git', :tag => 'v2.2.0' }
spec.swift_version = '5.1'
spec.ios.deployment_target = '11.0'
spec.osx.deployment_target = '10.13'
Expand Down
4 changes: 2 additions & 2 deletions ACActionCable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 2.1.2;
MARKETING_VERSION = 2.2.0;
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand Down Expand Up @@ -352,7 +352,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 2.1.2;
MARKETING_VERSION = 2.2.0;
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand Down
25 changes: 16 additions & 9 deletions Sources/ACActionCable/ACCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ public struct ACCommand {

// MARK: Initialization

init?<T: Encodable>(type: ACCommandType, identifier: ACChannelIdentifier, action: String? = nil, object: T? = nil) {
self.type = type
self.identifier = identifier

if let object, let data = try? Self.encoder.encode(object) {
self.data = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
}

if type == .message, let action {
self.data = self.data ?? [:]
self.data?["action"] = action
}

if type == .message && (self.data ?? [:]).isEmpty { return nil }
}

init?(type: ACCommandType, identifier: ACChannelIdentifier, action: String? = nil) {
High5Apps marked this conversation as resolved.
Show resolved Hide resolved
self.type = type
self.identifier = identifier
Expand All @@ -52,16 +68,7 @@ public struct ACCommand {
self.data = ["action": action]
}
}

init?<T: Encodable>(type: ACCommandType, identifier: ACChannelIdentifier, object: T) {
High5Apps marked this conversation as resolved.
Show resolved Hide resolved
self.type = type
self.identifier = identifier

// special handling of data
guard let data = try? Self.encoder.encode(object), let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) else { return nil }
self.data = jsonObject as? [String: Any]
}

// MARK: Helpers

private func json(from dictionary: [String: Any]) -> String? {
Expand Down
6 changes: 3 additions & 3 deletions Sources/ACActionCable/ACSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class ACSubscription {

// MARK: Sending

public func send<T: Encodable>(object: T, completion: ACEventHandler? = nil) {
guard let command = ACCommand(type: .message, identifier: channelIdentifier, object: object) else { return }
public func send<T: Encodable>(action: String? = nil, object: T, completion: ACEventHandler? = nil) {
High5Apps marked this conversation as resolved.
Show resolved Hide resolved
guard let command = ACCommand(type: .message, identifier: channelIdentifier, action: action, object: object) else { return }
client.send(command, completion: completion)
}

public func send(action: String, completion: ACEventHandler? = nil) {
guard let command = ACCommand(type: .message, identifier: channelIdentifier, action: action) else { return }
client.send(command)
client.send(command, completion: completion)
High5Apps marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
15 changes: 15 additions & 0 deletions Tests/ACActionCableTests/ACCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ class ACCommandTests: XCTestCase {
let command = ACCommand(type: .message, identifier: identifier, action: "my_action")
XCTAssertEqual(expected, command?.string)
}

func testShouldEncodeActionMessageWithObject() throws {
struct MyObject: Encodable {
let myInt: Int
let myString: String
let myDate: Date
}

let date = Date(timeIntervalSince1970: 1600545466)
let format = #"{"command":"message","data":"{\"action\":\"my_action\",\"my_date\":%d,\"my_int\":1,\"my_string\":\"test\"}","identifier":"{\"channel\":\"TestChannel\",\"test_id\":42}"}"#
let expected = String(format: format, Int(date.timeIntervalSince1970))
let identifier = ACChannelIdentifier(channelName: "TestChannel", identifier: ["test_id": 42])!
let command = ACCommand(type: .message, identifier: identifier, action: "my_action", object: MyObject(myInt: 1, myString: "test", myDate: date))
XCTAssertEqual(expected, command?.string)
}
}
12 changes: 12 additions & 0 deletions Tests/ACActionCableTests/ACSubscriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class ACSubscriptionTests: XCTestCase {
wait(for: [expectation], timeout: 1)
}

func testShouldSendWithActionAndEncodable() throws {
struct MyObject: Encodable {
let myProperty: Int
}

let expectedSend = #"{"command":"message","data":"{\"action\":\"my_action\",\"my_property\":1}","identifier":"{\"channel\":\"TestChannel\",\"test_id\":32}"}"#
let object = MyObject(myProperty: 1)
let (subscription, expectation) = expectMessage(expectedSend)
subscription.send(action:"my_action", object: object)
wait(for: [expectation], timeout: 1)
}

private func expectMessage(_ expectedSend: String) -> (ACSubscription, XCTestExpectation) {
let expectedSubscribe = #"{"command":"subscribe","identifier":"{\"channel\":\"TestChannel\",\"test_id\":32}"}"#
let subscribe = expectation(description: "Subscribe")
Expand Down