Skip to content

Commit

Permalink
Created BeaconInfo, modified tests, bumped up podspec number
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalonunez committed Nov 8, 2017
1 parent f8960b7 commit 22ef991
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Parrot.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Parrot"
s.version = "0.0.2"
s.version = "0.0.3"
s.summary = "Parrot is a (very) small Swift framework that helps with advertising an iOS device as an iBeacon as well as monitoring/ranging for iBeacons."
s.description = <<-DESC
Parrot is (very) small Swift framework that helps with iBeacon related functionality on iOS. BeaconAdvertiser advertises an iOS device as an iBeacon, and BeaconRanger monitors/ranges for iBeacons.
Expand Down
4 changes: 4 additions & 0 deletions Parrot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
AB751A071FB259CE00285CF0 /* BeaconAdvertiser.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB751A051FB259CD00285CF0 /* BeaconAdvertiser.swift */; };
AB751A081FB259CE00285CF0 /* BeaconMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB751A061FB259CE00285CF0 /* BeaconMonitor.swift */; };
AB751A0A1FB2654700285CF0 /* BeaconParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB751A091FB2654700285CF0 /* BeaconParams.swift */; };
AB962CCB1FB3C93300F82AB5 /* BeaconInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB962CCA1FB3C93300F82AB5 /* BeaconInfo.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -35,6 +36,7 @@
AB751A051FB259CD00285CF0 /* BeaconAdvertiser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BeaconAdvertiser.swift; sourceTree = "<group>"; };
AB751A061FB259CE00285CF0 /* BeaconMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BeaconMonitor.swift; sourceTree = "<group>"; };
AB751A091FB2654700285CF0 /* BeaconParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BeaconParams.swift; sourceTree = "<group>"; };
AB962CCA1FB3C93300F82AB5 /* BeaconInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BeaconInfo.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -79,6 +81,7 @@
children = (
AB751A051FB259CD00285CF0 /* BeaconAdvertiser.swift */,
AB751A061FB259CE00285CF0 /* BeaconMonitor.swift */,
AB962CCA1FB3C93300F82AB5 /* BeaconInfo.swift */,
AB751A091FB2654700285CF0 /* BeaconParams.swift */,
AB7519EE1FB24DFC00285CF0 /* Parrot.h */,
AB7519EF1FB24DFC00285CF0 /* Info.plist */,
Expand Down Expand Up @@ -209,6 +212,7 @@
AB751A081FB259CE00285CF0 /* BeaconMonitor.swift in Sources */,
AB751A0A1FB2654700285CF0 /* BeaconParams.swift in Sources */,
AB751A071FB259CE00285CF0 /* BeaconAdvertiser.swift in Sources */,
AB962CCB1FB3C93300F82AB5 /* BeaconInfo.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion Parrot/BeaconAdvertiser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public final class BeaconAdvertiser: NSObject {
Initializer that creates the underlying [CLBeaconRegion](https://developer.apple.com/documentation/corelocation/clbeaconregion) that is advertised.

- parameter uuid: The underlying `CLBeaconRegion`'s `proximityUUID`.
- parameter params: The `BeaconParams` that will be used to populate the underlying `CLBeaconRegion`'s major/minor values.
- parameter identifier: The underlying `CLBeaconRegion`'s `identifier`.
- parameter params: The `BeaconParams` that will be used to populate the underlying `CLBeaconRegion`'s major/minor values.
*/
public init(
uuid: UUID,
Expand Down
80 changes: 80 additions & 0 deletions Parrot/BeaconInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// BeaconInfo.swift
// Parrot
//
// Created by Gonzalo Nunez on 11/8/17.
// Copyright © 2017 carrot. All rights reserved.
//

import Foundation
import CoreLocation

/// A struct to help package and serialize information regarding a `CLBeaconRegion`
public struct BeaconInfo {

/// The underlying `CLBeaconRegion`'s `proximityUUID`.
public var uuid: UUID

/// The underlying `CLBeaconRegion`'s `identifier`.
public var identifier: String

/// The `BeaconParams` that will be used to populate the underlying `CLBeaconRegion`'s major/minor values.
public var params: BeaconRegionParams

/**
- parameter uuid: The underlying `CLBeaconRegion`'s `proximityUUID`.
- parameter params: The `BeaconParams` that will be used to populate the underlying `CLBeaconRegion`'s major/minor values.
- parameter identifier: The underlying `CLBeaconRegion`'s `identifier`.
*/
public init(
uuid: UUID,
identifier: String,
params: BeaconRegionParams)
{
self.uuid = uuid
self.identifier = identifier
self.params = params
}

/**
- parameter region: The underlying `CLBeaconRegion`.
*/
public init(region: CLBeaconRegion) {
self.uuid = region.proximityUUID
self.identifier = region.identifier
self.params = region.params
}
}

/// :nodoc:
extension BeaconInfo: Codable {

enum CodingError: Error {
case decoding(String)
}

enum CodingKeys: String, CodingKey {
case uuid
case identifier
case params
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
guard let uuidString = try? values.decode(String.self, forKey: .uuid),
let proximityUUID = UUID(uuidString: uuidString)
else {
throw CodingError.decoding("Decoding Failed. \(dump(values))")
}
uuid = proximityUUID
identifier = try values.decode(String.self, forKey: .identifier)
params = try values.decode(BeaconRegionParams.self, forKey: .params)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(uuid.uuidString, forKey: .uuid)
try container.encode(identifier, forKey: .identifier)
try container.encode(params, forKey: .params)
}
}
4 changes: 3 additions & 1 deletion Parrot/BeaconMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public final class BeaconMonitor: NSObject {
/**
Initializer that accepts the [CLBeaconRegion](https://developer.apple.com/documentation/corelocation/clbeaconregion) to be monitored.

- parameter region: The `CLBeaconRegion` to monitor.
- parameter uuid: The underlying `CLBeaconRegion`'s `proximityUUID`.
- parameter identifier: The underlying `CLBeaconRegion`'s `identifier`.
- parameter params: The `BeaconParams` that will be used to populate the underlying `CLBeaconRegion`'s major/minor values.
*/
public init(
uuid: UUID,
Expand Down
50 changes: 50 additions & 0 deletions Parrot/BeaconParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,46 @@ public enum BeaconRegionParams {
case both(major: UInt16, minor: UInt16)
}

/// :nodoc:
extension BeaconRegionParams: Codable {

public enum CodingKeys: String, CodingKey {
case major
case minor
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let decodedMajor = try? values.decode(UInt16.self, forKey: .major)
let decodedMinor = try? values.decode(UInt16.self, forKey: .minor)
switch (decodedMajor, decodedMinor) {
case (.none, _):
self = .none
case let (major?, .none):
self = .major(major)
case let (major?, minor?):
self = .both(major: major, minor: minor)
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .none:
break
case let .major(major):
try container.encode(major, forKey: .major)
case let .both(major: major, minor: minor):
try container.encode(major, forKey: .major)
try container.encode(minor, forKey: .minor)
}
}
}

/// An extension that allows `CLBeaconRegion`s to be used with `BeaconRegionParams`
extension CLBeaconRegion {

/// A convenience initializer to create a `CLBeaconRegion` with `BeaconRegionParams`.
convenience init(
uuid: UUID,
identifier: String,
Expand All @@ -44,4 +82,16 @@ extension CLBeaconRegion {
identifier: identifier)
}
}

/// Returns the corresponding `BeaconRegionParams` of the receiver.
public var params: BeaconRegionParams {
switch (major, minor) {
case (.none, .none), (.none, _):
return .none
case let (major?, .none):
return .major(major.uint16Value)
case let (major?, minor?):
return .both(major: major.uint16Value, minor: minor.uint16Value)
}
}
}
18 changes: 18 additions & 0 deletions ParrotTests/ParrotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,37 @@ class ParrotTests: XCTestCase {
assert(beacon.identifier == identifier)
assert(beacon.major == nil)
assert(beacon.minor == nil)
switch beacon.params {
case .none:
assert(true)
case .major, .both:
assert(false, "Unexpected BeaconParams")
}
}

func testOnlyMajorParams() {
let beacon = CLBeaconRegion(uuid: uuid, identifier: identifier, params: .major(100))
assert(beacon.identifier == identifier)
assert(beacon.major == 100)
assert(beacon.minor == nil)
switch beacon.params {
case .major:
assert(true)
case .none, .both:
assert(false, "Unexpected BeaconParams")
}
}

func testBothMajorAndMinorParams() {
let beacon = CLBeaconRegion(uuid: uuid, identifier: identifier, params: .both(major: 100, minor: 50))
assert(beacon.identifier == identifier)
assert(beacon.major == 100)
assert(beacon.minor == 50)
switch beacon.params {
case .both:
assert(true)
case .none, .major:
assert(false, "Unexpected BeaconParams")
}
}
}

0 comments on commit 22ef991

Please sign in to comment.