Skip to content

Commit

Permalink
Update documentation for master branch.
Browse files Browse the repository at this point in the history
- Documentation/Basics.md
- Added SwishExamples.playground playground file,
  with the contents of Basics.md
  • Loading branch information
Sid Raval committed Mar 9, 2018
1 parent 22abc4c commit 8c4757f
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 194 deletions.
102 changes: 0 additions & 102 deletions Documentation/Advanced.md

This file was deleted.

43 changes: 17 additions & 26 deletions Documentation/Basics.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,51 @@
# Basic Usage #

Let's say you have an endpoint, `GET https://www.example.com/comments/1`, that returns the following JSON:
Let's say we have an endpoint, `GET https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json`
that returns the following JSON:

```json
{
"id": 1,
"id": 4,
"commentText": "Pretty good. Pret-ty pre-ty pre-ty good.",
"username": "LarryDavid"
}
```

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

```swift
import Argo
import Curry

struct Comment {
struct Comment: Codable {
let id: Int
let text: String
let body: String
let username: String
}

extension Comment: Decodable {
static func decode(_ json: JSON) -> Decoded<Comment> {
return curry(Comment.init)
<^> j <| "id"
<*> j <| "commentText"
<*> j <| "username"
enum CodingKeys: String, CodingKey {
case id
case body = "commentText"
case username
}
}
```

We can model the request by defining a struct that implement's Swish's `Request` protocol:
We can model the request by defining a struct that
implements Swish's `Request` protocol

```swift
import Swish

struct CommentRequest: Request {
typealias ResponseObject = Comment

let id: Int

func build() -> URLRequest {
let url = URL(string: "https://www.example.com/comments/\(id)")!
return URLRequest(URL: url)
let url = URL(string: "https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json")!
return URLRequest(url: url)
}
}
```

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

```swift
let getComment = CommentRequest(id: 1)

APIClient().perform(getComment) { (response: Result<Comment, SwishError>) in
APIClient().perform(CommentRequest()) { (response: Result<Comment, SwishError>) in
switch response {
case let .success(comment):
print("Here's the comment: \(comment)")
Expand Down
54 changes: 0 additions & 54 deletions Documentation/Protocols.md

This file was deleted.

5 changes: 0 additions & 5 deletions Documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@ Swish is a protocol oriented networking library. It aims to be flexible, non-inv

## Basic Usage ##
- [Working with JSON Responses](Basics.md)

## Advanced Usage ##
- [Customizing Swish by implementing protocols](Protocols.md)
- [An example for using Swish with non-JSON responses](Advanced.md)

5 changes: 5 additions & 0 deletions Documentation/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 4,
"commentText": "Pretty good. Pret-ty pre-ty pre-ty good.",
"username": "LarryDavid"
}
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
Nothing but net(working).

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

## Version Compatibility

Here is the current Swift compatibility breakdown:

| Swift Version | Swish Version |
| ------------- | ------------ |
| 3.X | 2.X |
| 2.X | 1.X |
| ------------- | ------------ |
| 4.X | >= 3.0.0 |
| 3.X | > 2.0, < 3.0 |
| 2.X | 1.X |

## Installation

Expand Down Expand Up @@ -66,8 +67,17 @@ target.

## Usage

- tl;dr: [Basic Usage](Documentation/Basics.md)
- Otherwise, see the [Documentation](Documentation)
### Basic Playground

You can see an example of Swish in action via the included `SwishExamples.playground`.

To use that, clone this repository and run `carthage bootstrap --platform iOS`.
When that finishes, open the `Swish.xcworkspace` and click the `SwishExamples`
playground on the left.

### Documentation

- [Basic Usage](Documentation/Basics.md)

## License

Expand Down
3 changes: 3 additions & 0 deletions Swish.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions SwishExamples.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import PlaygroundSupport
import Swish
import Result

PlaygroundPage.current.needsIndefiniteExecution = true

/*: overview
Let's say we have an endpoint,
`GET https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json`
that returns the following JSON:
{
"id": 4,
"commentText": "Pretty good. Pret-ty pre-ty pre-ty good.",
"username": "LarryDavid"
}
We will model the data with the following struct,
and implement the `Codable` protocol so we can
tell it how to turn from JSON into a Swift object.
*/

struct Comment: Codable {
let id: Int
let body: String
let username: String

enum CodingKeys: String, CodingKey {
case id
case body = "commentText"
case username
}
}

/*:
We can model the request by defining a struct that
implements Swish's `Request` protocol
*/

struct CommentRequest: Request {
typealias ResponseObject = Comment

func build() -> URLRequest {
let url = URL(string: "https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json")!
return URLRequest(url: url)
}
}

/*:
We can then use the Swish's default APIClient to make the request:
*/

APIClient().perform(CommentRequest()) { (response: Result<Comment, SwishError>) in
switch response {
case let .success(comment):
print("Here's the comment: \(comment)")
case let .failure(error):
print("Oh no, an error: \(error)")
}
}

/*:
And that's it!
*/
4 changes: 4 additions & 0 deletions SwishExamples.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' display-mode='rendered'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8c4757f

Please sign in to comment.