Skip to content

Commit

Permalink
Merge pull request #3 from mtslzr/release/v1.1.0
Browse files Browse the repository at this point in the history
Release v1.1.0
  • Loading branch information
mtslzr authored Jun 22, 2019
2 parents c33da6c + 9bea3b0 commit c202fd4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.1.0] - 2019-06-22
### Added
- Pagination for resource lists
- Optional parameters for Resource calls:
- Offset (defaults to zero)
- Limit (defaults to twenty, per API)

## [1.0.0] - 2019-06-22
### Added
Expand All @@ -24,5 +26,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Resource endpoint for resource lists
- Unit tests for all endpoints

[Unreleased]: https://github.com/mtslzr/pokeapi-go/compare/v1.0.0...HEAD
[Unreleased]: https://github.com/mtslzr/pokeapi-go/compare/v1.1.0...HEAD
[1.1.0]: https://github.com/mtslzr/pokeapi-go/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/mtslzr/pokeapi-go/releases/tag/v1.0.0
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Wrapper for [Poke API](https://pokeapi.co), written in Go.
- [Moves](#Moves)
- [Pokemon](#Pokemon)
- [Utility](#Utility)
- [Resource List Parameters](#Resource-List-Parameters)

## Documentation

Expand Down Expand Up @@ -920,3 +921,37 @@ import pokeapi "github.com/mtslzr/pokeapi-go"
u := pokeapi.Language("en")
```
</details>

## Resource List Parameters

When calling `pokeapi.Resource()` for any resource list, you can optionally pass up to two integers. The first will be an offset (defaults to zero), and the second will be the limit (defaults two twenty).

<details>
<summary>Default</summary>

```go
r := pokeapi.Resource("pokemon")
fmt.Println(len(r.Results)) // 20
fmt.Println(r.Results[0].Name) // "bulbasaur"
```
</details>

<details>
<summary>Offset</summary>

```go
r := pokeapi.Resource("pokemon", 3)
fmt.Println(len(r.Results)) // 20
fmt.Println(r.Results[0].Name) // "charmander"
```
</details>

<details>
<summary>Offset and Limit</summary>

```go
r := pokeapi.Resource("pokemon", 6, 10)
fmt.Println(len(r.Results)) // 10
fmt.Println(r.Results[0].Name) // "squirtle"
```
</details>
22 changes: 18 additions & 4 deletions resource.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package pokeapi

import (
"fmt"

"github.com/mtslzr/pokeapi-go/structs"
)

// Resource returns resource list for an endpoint.
func Resource(endpoint string) (*structs.Resource, error) {
var result structs.Resource
func Resource(endpoint string, params ...int) (result structs.Resource,
err error) {
offset, limit := parseParams(params)
err = do(fmt.Sprintf("%s?offset=%d&limit=%d", endpoint, offset, limit),
&result)
return result, err
}

err := do(endpoint, &result)
return &result, err
func parseParams(params []int) (offset int, limit int) {
switch x := len(params); {
case x == 2:
limit = params[1]
fallthrough
case x >= 1:
offset = params[0]
}
return
}
14 changes: 14 additions & 0 deletions tests/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,17 @@ func TestResource(t *testing.T) {
"Expect to receive results with URLs.")
}
}

func TestResourceOffset(t *testing.T) {
result, _ := pokeapi.Resource("pokemon", 3)
assert.Equal(t, "charmander", result.Results[0].Name,
"Expect to receive Charmander.")
}

func TestResourceOffsetLimit(t *testing.T) {
result, _ := pokeapi.Resource("pokemon", 3, 3)
assert.Equal(t, 3, len(result.Results),
"Expect to receive exactly three results.")
assert.Equal(t, "charizard", result.Results[2].Name,
"Expect to receive Charizard last.")
}

0 comments on commit c202fd4

Please sign in to comment.