Skip to content

Commit 41ff49b

Browse files
committed
[master] - Release v2.0.0 🎉
1 parent 68ae97d commit 41ff49b

File tree

107 files changed

+8334
-11524
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+8334
-11524
lines changed

AlamoRecord.podspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = 'AlamoRecord'
4-
s.version = '1.4.0'
4+
s.version = '2.0.0'
55
s.summary = 'An elegant Alamofire wrapper inspired by ActiveRecord.'
66
s.description = <<-DESC
77
AlamoRecord is a powerful yet simple framework that eliminates the often complex networking layer that exists between your networking framework and your application. AlamoRecord uses the power of AlamoFire, AlamofireObjectMapper and the concepts behind the ActiveRecord pattern to create a networking layer that makes interacting with your API easier than ever.
@@ -19,5 +19,6 @@ AlamoRecord is a powerful yet simple framework that eliminates the often complex
1919
s.watchos.deployment_target = '3.0'
2020

2121
s.source_files = 'AlamoRecord/Classes/**/*'
22-
s.dependency 'AlamofireObjectMapper', '5.1.0'
22+
s.dependency 'Alamofire', '5.0.0-beta.6'
23+
2324
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// AlamoRecordDecoder.swift
3+
// AlamoRecord
4+
//
5+
// Created by Dalton Hinterscher on 6/6/19.
6+
//
7+
8+
import Alamofire
9+
10+
class AlamoRecordDecoder: DataDecoder {
11+
12+
private let keyPath: String?
13+
14+
init(keyPath: String?) {
15+
self.keyPath = keyPath
16+
}
17+
18+
func decode<D>(_ type: D.Type, from data: Data) throws -> D where D : Decodable {
19+
let decoder = JSONDecoder()
20+
guard let keyPath = keyPath,
21+
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any],
22+
let keyPathJson = json[keyPath] else {
23+
return try decoder.decode(D.self, from: data)
24+
}
25+
let keyPathJsonData = try JSONSerialization.data(withJSONObject: keyPathJson, options: .prettyPrinted)
26+
return try decoder.decode(D.self, from: keyPathJsonData)
27+
}
28+
29+
30+
}

AlamoRecord/Classes/AlamoRecordError.swift

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,33 @@
1515
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1616

1717
*/
18+
import Foundation
1819

19-
import ObjectMapper
20-
21-
open class AlamoRecordError: NSError, Mappable {
22-
23-
override open var description: String {
24-
guard let nsError = nsError else {
25-
return "[AlamoRecordError] No description could be found for this error."
26-
}
27-
return "[AlamoRecordError] \(nsError.localizedDescription)"
28-
}
20+
open class AlamoRecordError: Error {
2921

3022
/// The error of the failed request
31-
public var nsError: NSError?
23+
public let error: Error?
3224

33-
public required init() {
34-
super.init(domain: "", code: -1, userInfo: [:])
25+
required public init(error: Error) {
26+
self.error = error
3527
}
3628

37-
required public init(nsError: NSError) {
38-
super.init(domain: nsError.domain, code: nsError.code, userInfo: nsError.userInfo)
39-
self.nsError = nsError
40-
}
41-
42-
required public init?(map: Map) {
43-
super.init(domain: "", code: -1, userInfo: [:])
44-
mapping(map: map)
29+
public required init(from decoder: Decoder) throws {
30+
error = nil
4531
}
32+
33+
}
34+
35+
extension AlamoRecordError: Codable {
36+
open func encode(to encoder: Encoder) throws {}
37+
}
38+
39+
extension AlamoRecordError: CustomStringConvertible {
4640

47-
required public init?(coder aDecoder: NSCoder) {
48-
super.init(coder: aDecoder)
41+
open var description: String {
42+
guard let error = error else {
43+
return "❗️ [AlamoRecordError] No description could be found for this error."
44+
}
45+
return "❗️ [AlamoRecordError] \(error.localizedDescription)"
4946
}
50-
51-
open func mapping(map: Map) {}
5247
}

AlamoRecord/Classes/AlamoRecordObject.swift

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
*/
1818

1919
import Alamofire
20-
import AlamofireObjectMapper
21-
import ObjectMapper
2220

23-
open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NSObject, Mappable {
21+
open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType: Codable>: Codable {
2422

25-
/// Key to encode/decode the id variable
26-
private let idKey: String = "id"
23+
/// The id of this instance. This should be a String or an Int.
24+
public let id: IDType
2725

2826
/// The RequestManager that is tied to all instances of this class
2927
open class var requestManager: RequestManager<U, E, IDType> {
@@ -35,9 +33,6 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
3533
return type(of: self).requestManager
3634
}
3735

38-
/// The id of this instance. This should be a String or an Int.
39-
open var id: IDType!
40-
4136
/// The root of all instances of this class. This is used when making URL's that relate to a component of this class.
4237
// Example: '/comment/id' --> '/\(Comment.root)/id'
4338
open class var root: String {
@@ -95,37 +90,24 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
9590
open var pluralKeyPath: String? {
9691
return type(of: self).pluralKeyPath
9792
}
98-
99-
public override init() {
100-
super.init()
101-
}
102-
103-
public required init?(map: Map) {
104-
super.init()
105-
mapping(map: map)
106-
}
107-
108-
open func mapping(map: Map) {
109-
id <- map["id"]
110-
}
11193

11294
/**
11395
Returns an array of all objects of this instance if the server supports it
11496
- parameter parameters: The parameters. `nil` by default
115-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
97+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
11698
- parameter headers: The HTTP headers. `nil` by default
11799
- parameter success: The block to execute if the request succeeds
118100
- parameter failure: The block to execute if the request fails
119101
*/
120102
@discardableResult
121-
open class func all<T: AlamoRecordObject>(parameters: Parameters? = nil,
122-
encoding: ParameterEncoding = URLEncoding.default,
123-
headers: HTTPHeaders? = nil,
124-
success: (([T]) -> Void)?,
125-
failure: ((E) -> Void)?) -> DataRequest {
126-
return requestManager.findArray(T.urlForAll(),
103+
open class func all<O: AlamoRecordObject>(parameters: Parameters? = nil,
104+
encoding: ParameterEncoding = JSONEncoding.default,
105+
headers: HTTPHeaders? = nil,
106+
success: (([O]) -> Void)?,
107+
failure: ((E) -> Void)?) -> DataRequest {
108+
return requestManager.findArray(O.urlForAll(),
127109
parameters: parameters,
128-
keyPath: T.pluralKeyPath,
110+
keyPath: O.pluralKeyPath,
129111
encoding: encoding,
130112
headers: headers,
131113
success: success,
@@ -135,17 +117,17 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
135117
/**
136118
Creates an object of this instance
137119
- parameter parameters: The parameters. `nil` by default
138-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
120+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
139121
- parameter headers: The HTTP headers. `nil` by default
140122
- parameter success: The block to execute if the request succeeds
141123
- parameter failure: The block to execute if the request fails
142124
*/
143125
@discardableResult
144-
open class func create<T: AlamoRecordObject>(parameters: Parameters? = nil,
145-
encoding: ParameterEncoding = URLEncoding.default,
146-
headers: HTTPHeaders? = nil,
147-
success: ((T) -> Void)?,
148-
failure: ((E) -> Void)?) -> DataRequest {
126+
open class func create<O: AlamoRecordObject>(parameters: Parameters? = nil,
127+
encoding: ParameterEncoding = JSONEncoding.default,
128+
headers: HTTPHeaders? = nil,
129+
success: ((O) -> Void)?,
130+
failure: ((E) -> Void)?) -> DataRequest {
149131

150132
return requestManager.createObject(parameters: parameters,
151133
keyPath: keyPath,
@@ -158,14 +140,14 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
158140
/**
159141
Creates an object of this instance
160142
- parameter parameters: The parameters. `nil` by default
161-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
143+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
162144
- parameter headers: The HTTP headers. `nil` by default
163145
- parameter success: The block to execute if the request succeeds
164146
- parameter failure: The block to execute if the request fails
165147
*/
166148
@discardableResult
167149
open class func create(parameters: Parameters? = nil,
168-
encoding: ParameterEncoding = URLEncoding.default,
150+
encoding: ParameterEncoding = JSONEncoding.default,
169151
headers: HTTPHeaders? = nil,
170152
success: (() -> Void)?,
171153
failure: ((E) -> Void)?) -> DataRequest {
@@ -182,18 +164,18 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
182164
Finds an object of this instance based on the given id
183165
- parameter id: The id of the object to find
184166
- parameter parameters: The parameters. `nil` by default
185-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
167+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
186168
- parameter headers: The HTTP headers. `nil` by default
187169
- parameter success: The block to execute if the request succeeds
188170
- parameter failure: The block to execute if the request fails
189171
*/
190172
@discardableResult
191-
open class func find<T: AlamoRecordObject>(id: IDType,
192-
parameters: Parameters? = nil,
193-
encoding: ParameterEncoding = URLEncoding.default,
194-
headers: HTTPHeaders? = nil,
195-
success: ((T) -> Void)?,
196-
failure: ((E) -> Void)?) -> DataRequest {
173+
open class func find<O: AlamoRecordObject>(id: IDType,
174+
parameters: Parameters? = nil,
175+
encoding: ParameterEncoding = JSONEncoding.default,
176+
headers: HTTPHeaders? = nil,
177+
success: ((O) -> Void)?,
178+
failure: ((E) -> Void)?) -> DataRequest {
197179

198180
return requestManager.findObject(id: id,
199181
parameters: parameters,
@@ -207,19 +189,19 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
207189
/**
208190
Updates the object
209191
- parameter parameters: The parameters. `nil` by default
210-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
192+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
211193
- parameter headers: The HTTP headers. `nil` by default
212194
- parameter success: The block to execute if the request succeeds
213195
- parameter failure: The block to execute if the request fails
214196
*/
215197
@discardableResult
216-
open func update<T: AlamoRecordObject>(parameters: Parameters? = nil,
217-
encoding: ParameterEncoding = URLEncoding.default,
218-
headers: HTTPHeaders? = nil,
219-
success: ((T) -> Void)?,
220-
failure: ((E) -> Void)?) -> DataRequest {
198+
open func update<O: AlamoRecordObject>(parameters: Parameters? = nil,
199+
encoding: ParameterEncoding = JSONEncoding.default,
200+
headers: HTTPHeaders? = nil,
201+
success: ((O) -> Void)?,
202+
failure: ((E) -> Void)?) -> DataRequest {
221203

222-
return T.update(id: id,
204+
return O.update(id: id,
223205
parameters: parameters,
224206
encoding: encoding,
225207
headers: headers,
@@ -231,18 +213,18 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
231213
Updates an object of this instance based with the given id
232214
- parameter id: The id of the object to update
233215
- parameter parameters: The parameters. `nil` by default
234-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
216+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
235217
- parameter headers: The HTTP headers. `nil` by default
236218
- parameter success: The block to execute if the request succeeds
237219
- parameter failure: The block to execute if the request fails
238220
*/
239221
@discardableResult
240-
open class func update<T: AlamoRecordObject>(id: IDType,
241-
parameters: Parameters? = nil,
242-
encoding: ParameterEncoding = URLEncoding.default,
243-
headers: HTTPHeaders? = nil,
244-
success: ((T) -> Void)?,
245-
failure: ((E) -> Void)?) -> DataRequest {
222+
open class func update<O: AlamoRecordObject>(id: IDType,
223+
parameters: Parameters? = nil,
224+
encoding: ParameterEncoding = JSONEncoding.default,
225+
headers: HTTPHeaders? = nil,
226+
success: ((O) -> Void)?,
227+
failure: ((E) -> Void)?) -> DataRequest {
246228

247229
return requestManager.updateObject(id: id,
248230
parameters: parameters,
@@ -257,15 +239,15 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
257239
Updates an object of this instance based with the given id
258240
- parameter id: The id of the object to update
259241
- parameter parameters: The parameters. `nil` by default
260-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
242+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
261243
- parameter headers: The HTTP headers. `nil` by default
262244
- parameter success: The block to execute if the request succeeds
263245
- parameter failure: The block to execute if the request fails
264246
*/
265247
@discardableResult
266248
open class func update(id: IDType,
267249
parameters: Parameters? = nil,
268-
encoding: ParameterEncoding = URLEncoding.default,
250+
encoding: ParameterEncoding = JSONEncoding.default,
269251
headers: HTTPHeaders? = nil,
270252
success: (() -> Void)?,
271253
failure: ((E) -> Void)?) -> DataRequest {
@@ -282,14 +264,14 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
282264
Updates an object of this instance based with the given id
283265
- parameter id: The id of the object to update
284266
- parameter parameters: The parameters. `nil` by default
285-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
267+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
286268
- parameter headers: The HTTP headers. `nil` by default
287269
- parameter success: The block to execute if the request succeeds
288270
- parameter failure: The block to execute if the request fails
289271
*/
290272
@discardableResult
291273
open func update(parameters: Parameters? = nil,
292-
encoding: ParameterEncoding = URLEncoding.default,
274+
encoding: ParameterEncoding = JSONEncoding.default,
293275
headers: HTTPHeaders? = nil,
294276
success: (() -> Void)?,
295277
failure: ((E) -> Void)?) -> DataRequest {
@@ -305,14 +287,14 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
305287
/**
306288
Destroys the object
307289
- parameter parameters: The parameters. `nil` by default
308-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
290+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
309291
- parameter headers: The HTTP headers. `nil` by default
310292
- parameter success: The block to execute if the request succeeds
311293
- parameter failure: The block to execute if the request fails
312294
*/
313295
@discardableResult
314296
open func destroy(parameters: Parameters? = nil,
315-
encoding: ParameterEncoding = URLEncoding.default,
297+
encoding: ParameterEncoding = JSONEncoding.default,
316298
headers: HTTPHeaders? = nil,
317299
success: (() -> Void)?,
318300
failure: ((E) -> Void)?) -> DataRequest {
@@ -329,18 +311,18 @@ open class AlamoRecordObject<U: AlamoRecordURL, E: AlamoRecordError, IDType>: NS
329311
Finds an object of this instance based on the given id
330312
- parameter id: The id of the object to destroy
331313
- parameter parameters: The parameters. `nil` by default
332-
- parameter encoding: The parameter encoding. `URLEncoding.default` by default
314+
- parameter encoding: The parameter encoding. `JSONEncoding.default` by default
333315
- parameter headers: The HTTP headers. `nil` by default
334316
- parameter success: The block to execute if the request succeeds
335317
- parameter failure: The block to execute if the request fails
336318
*/
337319
@discardableResult
338320
open class func destroy(id: IDType,
339-
parameters: Parameters? = nil,
340-
encoding: ParameterEncoding = URLEncoding.default,
341-
headers: HTTPHeaders? = nil,
342-
success: (() -> Void)?,
343-
failure: ((E) -> Void)?) -> DataRequest {
321+
parameters: Parameters? = nil,
322+
encoding: ParameterEncoding = JSONEncoding.default,
323+
headers: HTTPHeaders? = nil,
324+
success: (() -> Void)?,
325+
failure: ((E) -> Void)?) -> DataRequest {
344326

345327
return requestManager.destroyObject(url: urlForDestroy(id),
346328
parameters: parameters,

0 commit comments

Comments
 (0)