Skip to content

Commit bb85daf

Browse files
author
Rafael da Silva Ferreira
committed
Add Core implementation
Add all core implementation for the library.
1 parent 6917194 commit bb85daf

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

JSONStub/Classes/FileStub.swift

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// FileStub.swift
3+
// Pods
4+
//
5+
// Created by Rafael Ferreira on 10/25/16.
6+
//
7+
//
8+
9+
public protocol FileStub {
10+
var fileName: String { get }
11+
}

JSONStub/Classes/Mappable.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Mappable.swift
3+
// NuChargebackTests
4+
//
5+
// Created by Rafael Ferreira on 10/5/16.
6+
// Copyright © 2016 Swift Yah. All rights reserved.
7+
//
8+
9+
import ObjectMapper
10+
11+
public extension Mappable {
12+
init?(fromFileName file: String) {
13+
guard let json = file.fileDictionary() else { return nil }
14+
15+
self.init(JSON: json)
16+
}
17+
18+
init?(fromStub stub: FileStub) {
19+
self.init(fromFileName: stub.fileName)
20+
}
21+
22+
var asData: Data {
23+
guard let json = toJSONString(), let data = json.data(using: .utf8), json != "{}" else { return Data() }
24+
25+
return data
26+
}
27+
}

JSONStub/Classes/ReplaceMe.swift

Whitespace-only changes.

JSONStub/Classes/String.swift

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// String.swift
3+
// NuChargeback
4+
//
5+
// Created by Rafael Ferreira on 10/5/16.
6+
// Copyright © 2016 Swift Yah. All rights reserved.
7+
//
8+
9+
public extension String {
10+
func fileData() -> Data? {
11+
guard let path = filePath() else { return nil }
12+
13+
let url = URL(fileURLWithPath: path)
14+
15+
return try? Data(contentsOf: url)
16+
}
17+
18+
func fileDictionary() -> [String: AnyObject]? {
19+
guard let data = fileData(),
20+
let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) else { return nil }
21+
22+
return json as? [String: AnyObject]
23+
}
24+
25+
func filePath() -> String? {
26+
guard splitAtDot.count == 2 else { return nil }
27+
28+
return Bundle.main.path(forResource: splitAtDot.first, ofType: splitAtDot.last)
29+
}
30+
}
31+
32+
// MARK: Utility functions
33+
34+
extension String {
35+
var splitAtDot: [String] {
36+
return splitBy(separator: ".")
37+
}
38+
39+
func splitBy(separator: String) -> [String] {
40+
guard let character = separator.characters.first else { return [self] }
41+
42+
return characters.split(separator: character).map({ String($0) })
43+
}
44+
}
45+
46+
// MARK: Case functions
47+
48+
public extension String {
49+
var pascalCase: String {
50+
return first.uppercased() + last
51+
}
52+
}
53+
54+
// MARK: Private functions
55+
56+
private extension String {
57+
var first: String {
58+
let item = characters.prefix(1)
59+
60+
return String(item)
61+
}
62+
63+
var last: String {
64+
let array = characters.dropFirst()
65+
66+
return String(array)
67+
}
68+
}

0 commit comments

Comments
 (0)