Skip to content

Commit 87ea748

Browse files
Sid Ravalsidraval
authored andcommitted
Update documentation for master branch.
- Documentation/Basics.md - Added SwishExamples.playground playground file, with the contents of Basics.md
1 parent 58917b1 commit 87ea748

10 files changed

Lines changed: 118 additions & 194 deletions

File tree

Documentation/Advanced.md

Lines changed: 0 additions & 102 deletions
This file was deleted.

Documentation/Basics.md

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,51 @@
1-
# Basic Usage #
2-
3-
Let's say you have an endpoint, `GET https://www.example.com/comments/1`, that returns the following JSON:
1+
Let's say we have an endpoint, `GET https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json`
2+
that returns the following JSON:
43

54
```json
65
{
7-
"id": 1,
6+
"id": 4,
87
"commentText": "Pretty good. Pret-ty pre-ty pre-ty good.",
98
"username": "LarryDavid"
109
}
1110
```
1211

13-
We'll model this with the following struct, and implement Argo's `Decodable` protocol to tell it how to deal with JSON:
12+
We will model the data with the following struct
13+
and conform to the `Codable` protocol so we can
14+
turn from JSON into a Swift object.
1415

1516
```swift
16-
import Argo
17-
import Curry
18-
19-
struct Comment {
17+
struct Comment: Codable {
2018
let id: Int
21-
let text: String
19+
let body: String
2220
let username: String
23-
}
2421

25-
extension Comment: Decodable {
26-
static func decode(_ json: JSON) -> Decoded<Comment> {
27-
return curry(Comment.init)
28-
<^> j <| "id"
29-
<*> j <| "commentText"
30-
<*> j <| "username"
22+
enum CodingKeys: String, CodingKey {
23+
case id
24+
case body = "commentText"
25+
case username
3126
}
3227
}
3328
```
3429

35-
We can model the request by defining a struct that implement's Swish's `Request` protocol:
30+
We can model the request by defining a struct that
31+
implements Swish's `Request` protocol
3632

3733
```swift
38-
import Swish
39-
4034
struct CommentRequest: Request {
4135
typealias ResponseObject = Comment
4236

43-
let id: Int
4437

4538
func build() -> URLRequest {
46-
let url = URL(string: "https://www.example.com/comments/\(id)")!
47-
return URLRequest(URL: url)
39+
let url = URL(string: "https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json")!
40+
return URLRequest(url: url)
4841
}
4942
}
5043
```
5144

5245
We can then use the Swish's default `APIClient` to make the request:
5346

5447
```swift
55-
let getComment = CommentRequest(id: 1)
56-
57-
APIClient().perform(getComment) { (response: Result<Comment, SwishError>) in
48+
APIClient().perform(CommentRequest()) { (response: Result<Comment, SwishError>) in
5849
switch response {
5950
case let .success(comment):
6051
print("Here's the comment: \(comment)")

Documentation/Protocols.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

Documentation/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,3 @@ Swish is a protocol oriented networking library. It aims to be flexible, non-inv
44

55
## Basic Usage ##
66
- [Working with JSON Responses](Basics.md)
7-
8-
## Advanced Usage ##
9-
- [Customizing Swish by implementing protocols](Protocols.md)
10-
- [An example for using Swish with non-JSON responses](Advanced.md)
11-

Documentation/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": 4,
3+
"commentText": "Pretty good. Pret-ty pre-ty pre-ty good.",
4+
"username": "LarryDavid"
5+
}

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
Nothing but net(working).
77

88
Swish is a networking library that is particularly meant for requesting and
9-
decoding JSON via [Argo](http://github.com/thoughtbot/Argo). It is protocol
10-
based, and so aims to be easy to test and customize.
9+
decoding JSON via `Decodable`. It is protocol based, and so aims to be easy
10+
to test and customize.
1111

1212
## Version Compatibility
1313

1414
Here is the current Swift compatibility breakdown:
1515

1616
| Swift Version | Swish Version |
17-
| ------------- | ------------ |
18-
| 3.X | 2.X |
19-
| 2.X | 1.X |
17+
| ------------- | ------------ |
18+
| 4.X | >= 3.0.0 |
19+
| 3.X | > 2.0, < 3.0 |
20+
| 2.X | 1.X |
2021

2122
## Installation
2223

@@ -66,8 +67,17 @@ target.
6667

6768
## Usage
6869

69-
- tl;dr: [Basic Usage](Documentation/Basics.md)
70-
- Otherwise, see the [Documentation](Documentation)
70+
### Basic Playground
71+
72+
You can see an example of Swish in action via the included `SwishExamples.playground`.
73+
74+
To use that, clone this repository and run `carthage bootstrap --platform iOS`.
75+
When that finishes, open the `Swish.xcworkspace` and click the `SwishExamples`
76+
playground on the left.
77+
78+
### Documentation
79+
80+
- [Basic Usage](Documentation/Basics.md)
7181

7282
## License
7383

Swish.xcworkspace/contents.xcworkspacedata

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import PlaygroundSupport
2+
import Swish
3+
import Result
4+
5+
PlaygroundPage.current.needsIndefiniteExecution = true
6+
7+
/*: overview
8+
9+
Let's say we have an endpoint,
10+
`GET https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json`
11+
that returns the following JSON:
12+
13+
{
14+
"id": 4,
15+
"commentText": "Pretty good. Pret-ty pre-ty pre-ty good.",
16+
"username": "LarryDavid"
17+
}
18+
19+
We will model the data with the following struct,
20+
and implement the `Codable` protocol so we can
21+
tell it how to turn from JSON into a Swift object.
22+
*/
23+
24+
struct Comment: Codable {
25+
let id: Int
26+
let body: String
27+
let username: String
28+
29+
enum CodingKeys: String, CodingKey {
30+
case id
31+
case body = "commentText"
32+
case username
33+
}
34+
}
35+
36+
/*:
37+
We can model the request by defining a struct that
38+
implements Swish's `Request` protocol
39+
*/
40+
41+
struct CommentRequest: Request {
42+
typealias ResponseObject = Comment
43+
44+
func build() -> URLRequest {
45+
let url = URL(string: "https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json")!
46+
return URLRequest(url: url)
47+
}
48+
}
49+
50+
/*:
51+
We can then use the Swish's default APIClient to make the request:
52+
*/
53+
54+
APIClient().perform(CommentRequest()) { (response: Result<Comment, SwishError>) in
55+
switch response {
56+
case let .success(comment):
57+
print("Here's the comment: \(comment)")
58+
case let .failure(error):
59+
print("Oh no, an error: \(error)")
60+
}
61+
}
62+
63+
/*:
64+
And that's it!
65+
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' display-mode='rendered'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

SwishExamples.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)