Skip to content

Commit a3dd0ac

Browse files
author
Adam Debono
committed
added readme
1 parent a9f311c commit a3dd0ac

File tree

3 files changed

+103
-5
lines changed

3 files changed

+103
-5
lines changed

NetworkMapper.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
Pod::Spec.new do |s|
22
s.name = "NetworkMapper"
33
s.version = "0.0.3"
4-
s.summary = "Map JSON responses to Swift Objects"
4+
s.summary = "A framework to map JSON responses to swift objects"
55
s.homepage = "https://github.com/adamdebono/NetworkMapper"
66
s.license = { :type => "MIT", :file => "LICENSE" }
77
s.author = { "Adam Debono" => "[email protected]" }
8-
8+
99
s.ios.deployment_target = "9.0"
1010
s.osx.deployment_target = "10.11"
1111
s.watchos.deployment_target = "2.0"
1212
s.tvos.deployment_target = "9.0"
1313

1414
s.source = { :git => "https://github.com/adamdebono/NetworkMapper.git", :tag => s.version }
1515
s.source_files = "Source/*.swift"
16-
16+
1717
s.dependency "Alamofire", "~> 4.2.0"
1818
s.dependency "Unbox", "~> 2.2.0"
1919
end

README.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,94 @@
1-
# NetworkMapper
1+
# NetworkMapper
2+
3+
A framework to map JSON responses to swift objects, based on
4+
[Alamofire](https://github.com/Alamofire/Alamofire) and
5+
[Unbox](https://github.com/JohnSundell/Unbox).
6+
7+
## Installation
8+
9+
### Cocoapods
10+
```ruby
11+
pod 'NetworkMapper', '~> 0.0.3'
12+
```
13+
14+
## Usage
15+
NetworkMapper makes use of protocols to implement functionality. The protocols
16+
only require you to define instance variables for the method, url and parameters
17+
of the request.
18+
19+
### Basic Request
20+
You can make basic requests that don't map the response by conforming to
21+
`NetworkRequest`. You can use `responseJSON` or `responseData` to retrieve the
22+
response from the server.
23+
24+
```swift
25+
struct ExampleRequest: NetworkRequest {
26+
let method: HTTPMethod = .get
27+
let url: URL = URL(string: "https://example.org/example/user")
28+
let parameters: [String:Any]? = nil
29+
}
30+
31+
ExampleRequest().responseJSON { response in
32+
switch response.result {
33+
case .failure(let error):
34+
// process error
35+
case .success(let json):
36+
// process response
37+
}
38+
}
39+
```
40+
41+
### Mapped Request
42+
A mapped request is slightly more complicated. Mapped requests require you to
43+
create an object that conforms to `NetworkObjectRequest` and
44+
`NetworkObjectResponse`.
45+
46+
The response protocol requires you to conform to the `Unboxable` protocol, see
47+
it's [documentation](https://github.com/JohnSundell/Unbox#unbox) for more
48+
information. The response object is treated as the root object of the JSON
49+
response. Currently arrays as a root object is not supported.
50+
51+
The request protocol adds on to `NetworkRequest` by adding an associatedtype,
52+
which specifies the type of response object. On completion of the request, the
53+
JSON object will be mapped to the response object. There is one other function,
54+
`responseDecoded` which can be optionally implemented, the default
55+
implementation of which does nothing.
56+
57+
```swift
58+
struct ExampleObjectRequest: NetworkObjectRequest {
59+
typealias ResponseType = ExampleObjectResponse
60+
61+
let method: HTTPMethod = .get
62+
let url: URL = URL(string: "https://example.org/example/users")
63+
let parameters: [String:Any]? = nil
64+
}
65+
66+
struct ExampleObjectResponse: NetworkObjectResponse {
67+
let users: [User]
68+
let pageNumber: Int
69+
70+
init(unboxer: Unboxable) throws {
71+
self.users = try unboxer.unbox(key: "users")
72+
self.pageNumber = try unboxer.unbox(keyPath: "pagination.page_number")
73+
}
74+
}
75+
76+
struct User: Unboxable {
77+
let name: String
78+
let age: Int
79+
80+
init(unboxer: Unboxable) throws {
81+
self.name = try unboxer.unbox(key: "name")
82+
self.name = try unboxer.unbox(key: "age")
83+
}
84+
}
85+
86+
ExampleObjectRequest().responseObject { response in
87+
switch response.result {
88+
case .failure(let error):
89+
// process error
90+
case .success(let object):
91+
// process response
92+
}
93+
}
94+
```

Source/NetworkObjectRequest.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import Foundation
44
import Unbox
55

66
public protocol NetworkObjectRequest: NetworkRequest {
7-
associatedtype ResponseType: Unboxable
7+
associatedtype ResponseType: NetworkObjectResponse
88

99
func responseDecoded(_ response: ResponseType)
1010
}
11+
public protocol NetworkObjectResponse: Unboxable {
12+
13+
}
1114

1215
public extension NetworkObjectRequest {
1316
@discardableResult
@@ -39,4 +42,6 @@ public extension NetworkObjectRequest {
3942
}
4043
}
4144
}
45+
46+
public func responseDecoded(_ response: ResponseType) {}
4247
}

0 commit comments

Comments
 (0)