Skip to content

Commit

Permalink
Add lastFetchTime and lastFetchStatus for Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tikidunpon committed Oct 21, 2021
1 parent 99cdf5f commit 8c8d390
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 15 deletions.
8 changes: 4 additions & 4 deletions Karte.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3131,7 +3131,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 3.4.0;
MARKETING_VERSION = 3.5.0;
PRODUCT_BUNDLE_IDENTIFIER = io.karte.KarteUtilities;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -3162,7 +3162,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 3.4.0;
MARKETING_VERSION = 3.5.0;
PRODUCT_BUNDLE_IDENTIFIER = io.karte.KarteUtilities;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -3513,7 +3513,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 2.2.0;
MARKETING_VERSION = 2.3.0;
PRODUCT_BUNDLE_IDENTIFIER = io.karte.KarteVariables;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -3544,7 +3544,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 2.2.0;
MARKETING_VERSION = 2.3.0;
PRODUCT_BUNDLE_IDENTIFIER = io.karte.KarteVariables;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,71 @@ class VariablesSpec: QuickSpec {
expect(variable.string).to(equal("変数4"))
}
}

describe("default lastFetch information") {
beforeEachWithMetadata { (metadata) in
UserDefaults.standard.removeObject(forKey: .lastFetchStatus)
UserDefaults.standard.removeObject(forKey: .lastFetchTime)
KarteApp.setup(appKey: APP_KEY, configuration: configuration)
}

it("lastFetchStatus is notyet") {
let lastFetchStatus = Variables.lastFetchStatus
expect(lastFetchStatus).to(equal(.nofetchYet))
}
it("lastFetchTime is nil") {
let lastFetchTime = Variables.lastFetchTime
expect(lastFetchTime).to(beNil())
}

it("hasSuccessfulLastFetch returns false") {
let hasSuccessfulLastFetch = Variables.hasSuccessfulLastFetch(inSeconds: 100)
expect(hasSuccessfulLastFetch).to(beFalse())
}
}

describe("update lastFetch information") {
beforeEachWithMetadata { (metadata) in
let module = StubActionModule(self, metadata: metadata, builder: fetchStubBuilder2, eventName: .fetchVariables) { (_, _, _) in
}

KarteApp.setup(appKey: APP_KEY, configuration: configuration)
Variables.fetch()

module.wait()

StubActionModule(self, metadata: metadata, builder: otherStubBuilder, eventName: .messageReady).wait()
}

it("lastFetchTime is not nil") {
let lastFetchTime = Variables.lastFetchTime
expect(lastFetchTime).toNot(beNil())
}

it("lastFetchStatus is success") {
let lastFetchStatus = Variables.lastFetchStatus
expect(lastFetchStatus).to(equal(.success))
}

it("hasSuccessfulLastFetch returns true") {
let hasSuccessfulLastFetch = Variables.hasSuccessfulLastFetch(inSeconds: 1)
expect(hasSuccessfulLastFetch).to(beTrue())

let hasSuccessfulLastFetch60 = Variables.hasSuccessfulLastFetch(inSeconds: 60)
expect(hasSuccessfulLastFetch60).to(beTrue())
}

it("hasSuccessfulLastFetch returns false") {
Thread.sleep(until: Date(timeIntervalSinceNow: 1))
let hasSuccessfulLastFetch = Variables.hasSuccessfulLastFetch(inSeconds: 1)
expect(hasSuccessfulLastFetch).to(beFalse())
}

it("hasSuccessfulLastFetch returns false when specify minus value") {
let hasSuccessfulLastFetch = Variables.hasSuccessfulLastFetch(inSeconds: -60)
expect(hasSuccessfulLastFetch).to(beFalse())
}
}
}

describe("its fetchCompletion") {
Expand Down Expand Up @@ -286,6 +351,21 @@ class VariablesSpec: QuickSpec {
it("result is false") {
expect(result).to(beFalse())
}

it("lastFetchTime is not nil") {
let lastFetchTime = Variables.lastFetchTime
expect(lastFetchTime).toNot(beNil())
}

it("lastFetchStatus is failure") {
let lastFetchStatus = Variables.lastFetchStatus
expect(lastFetchStatus).to(equal(.failure))
}

it("hasSuccessfulLastFetch returns false") {
let hasSuccessfulLastFetch = Variables.hasSuccessfulLastFetch(inSeconds: 10)
expect(hasSuccessfulLastFetch).to(beFalse())
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion KarteUtilities.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'KarteUtilities'
s.version = '3.4.0'
s.version = '3.5.0'
s.summary = 'KARTE Utilities SDK'
s.homepage = 'https://karte.io'
s.author = { 'PLAID' => '[email protected]' }
Expand Down
4 changes: 4 additions & 0 deletions KarteUtilities/DataSource/UserDefaults/UserDefaultsKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public struct UserDefaultsKey {
public extension UserDefaultsKey {
/// オプトアウト情報
static let optout = UserDefaultsKey("optout", forNamespace: .config)
/// 最終フェッチ時間
static let lastFetchTime = UserDefaultsKey("lastFetchTime", forNamespace: .variables)
/// 最終フェッチステータス
static let lastFetchStatus = UserDefaultsKey("lastFetchStatus", forNamespace: .variables)
}
4 changes: 2 additions & 2 deletions KarteVariables.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'KarteVariables'
s.version = '2.2.0'
s.version = '2.3.0'
s.summary = 'KARTE Variables SDK'
s.homepage = 'https://karte.io'
s.author = { 'PLAID' => '[email protected]' }
Expand All @@ -31,5 +31,5 @@ Pod::Spec.new do |s|
}

s.dependency 'KarteCore', '~> 2.0'
s.dependency 'KarteUtilities', '~> 3.0'
s.dependency 'KarteUtilities', '~> 3.5'
end
64 changes: 64 additions & 0 deletions KarteVariables/Variables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,72 @@

import Foundation
import KarteCore
import KarteUtilities

/// 設定値の取得完了をハンドルするためのブロックの宣言です。
public typealias FetchCompletion = (_ isSuccessful: Bool) -> Void

/// 最終フェッチステータスを表す列挙型です。
@objc(KRTLastFetchStatus)
public enum LastFetchStatus: Int {
/// nofetchYet
case nofetchYet
/// success
case success
/// failure
case failure
}

/// 設定値の取得・管理を司るクラスです。
@objc(KRTVariables)
public class Variables: NSObject {
private static let lastFetchTimeKey = "lastFetchTime"
private static let lastFetchStatusKey = "lastFetchStatus"

/// 最終フェッチ完了時間を返します、未フェッチな場合は `nil` を返します
@objc public class var lastFetchTime: Date? {
if let date = UserDefaults.standard.object(forKey: .lastFetchTime) as? Date {
return date
}
return nil
}

/// 最終フェッチ完了ステータスを返します
@objc public class var lastFetchStatus: LastFetchStatus {
if let optionalStatus = UserDefaults.standard.object(forKey: .lastFetchStatus) as? Int,
let status = LastFetchStatus(rawValue: optionalStatus) {
return status
}
return .nofetchYet
}

/// ローダークラスが Objective-Cランライムに追加されたタイミングで呼び出されるメソッドです。
/// 本メソッドが呼び出されたタイミングで、`KarteApp` クラスに本クラスをライブラリとして登録します。
@objc
public class func _krt_load() {
KarteApp.register(library: self)
}

/// 直近指定秒以内に成功したフェッチ結果があるかどうかを返します
/// - Parameter inSeconds: 秒数(1以上)
@objc
public class func hasSuccessfulLastFetch(inSeconds: TimeInterval) -> Bool {
guard let lastFetchTime = Variables.lastFetchTime,
Variables.lastFetchStatus == .success,
inSeconds > 0 else {
return false
}
let nowMinusInSeconds = Date(timeIntervalSinceNow: -inSeconds)
return nowMinusInSeconds < lastFetchTime
}

/// 設定値を取得し、端末上にキャッシュします。
/// - Parameter completion: 取得完了ハンドラ
@objc
public class func fetch(completion: FetchCompletion? = nil) {
let task = Tracker.track(event: Event(.fetchVariables))
task.completion = { isSuccessful in
saveLastFetchInfo(isSuccessful)
completion?(isSuccessful)
}
}
Expand Down Expand Up @@ -134,6 +180,24 @@ extension Variables: ActionModule, UserModule {
public func renew(visitorId current: String, previous: String) {
UserDefaults.standard.removeObject(forNamespace: .variables)
}
}

extension Variables {
private class func saveLastFetchInfo(_ isSuccessful: Bool) {
if isSuccessful {
let keyValue: [String: Any] = [
lastFetchTimeKey: Date(),
lastFetchStatusKey: LastFetchStatus.success.rawValue
]
UserDefaults.standard.bulkSet(keyValue, forNameSpace: .variables)
} else {
let keyValue: [String: Any] = [
lastFetchTimeKey: Date(),
lastFetchStatusKey: LastFetchStatus.failure.rawValue
]
UserDefaults.standard.bulkSet(keyValue, forNameSpace: .variables)
}
}

private func bulkSave(variables: [Variable]) {
let keyValue = variables.filter { $0.campaignId != nil && $0.shortenId != nil }.reduce(into: [String: Any?]()) { dict, variable in
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ platform :ios, '9.0'
target 'KarteTests' do
use_frameworks!

pod 'Mockingjay', '3.0.0-alpha.1'
pod 'Mockingjay', :git => 'https://github.com/kylef/Mockingjay.git', :branch => 'master'
pod 'Quick'
pod 'Nimble'
end
Expand Down
23 changes: 16 additions & 7 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,35 @@ PODS:
- Mockingjay/Core
- Nimble (9.2.0)
- Quick (4.0.0)
- URITemplate (3.0.0)
- URITemplate (3.0.1)

DEPENDENCIES:
- Mockingjay (= 3.0.0-alpha.1)
- Mockingjay (from `https://github.com/kylef/Mockingjay.git`, branch `master`)
- Nimble
- Quick

SPEC REPOS:
trunk:
- Mockingjay
- Nimble
- Quick
- URITemplate

EXTERNAL SOURCES:
Mockingjay:
:branch: master
:git: https://github.com/kylef/Mockingjay.git

CHECKOUT OPTIONS:
Mockingjay:
:commit: b88c9dce2b7561cccbf35e2882b3c71a2efa387a
:git: https://github.com/kylef/Mockingjay.git

SPEC CHECKSUMS:
Mockingjay: 0f7c5aa49c7f1b95621cee3c79b557141f5a225c
Mockingjay: 97656c6f59879923976a0a52ef09da45756cca82
Nimble: 4f4a345c80b503b3ea13606a4f98405974ee4d0b
Quick: 6473349e43b9271a8d43839d9ba1c442ed1b7ac4
URITemplate: 58e0d47f967006c5d59888af5356c4a8ed3b197d
URITemplate: 5f5a79f3e384884102c4e6b3325d159c20a8035a

PODFILE CHECKSUM: 5a0d817e9e6b5c6a1eb018597eb370d7f57d1c03
PODFILE CHECKSUM: e6a525f3a112251366005c5cd58304729b99fc8f

COCOAPODS: 1.10.1
COCOAPODS: 1.11.2

0 comments on commit 8c8d390

Please sign in to comment.