Skip to content

Commit

Permalink
Add sorting support
Browse files Browse the repository at this point in the history
  • Loading branch information
naoufal committed Mar 11, 2016
1 parent 2024c91 commit c99f66e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 48 deletions.
62 changes: 34 additions & 28 deletions README.md
Expand Up @@ -235,39 +235,41 @@ unsplash.users.profile("naoufal")
```
---

### users.photos(username)
### users.photos(username, orderBy)
Get a list of photos uploaded by a user.

__Arguments__

| Argument | Type | Opt/Required |
|---|---|---|
|__`username`__|_string_|Required|
| Argument | Type | Opt/Required | Notes |
|---|---|---|---|
|__`username`__|_string_|Required||
|__`orderBy`__|_string_|Optional|`latest`, `popular` or `oldest`|

__Example__
```js
unsplash.users.photos("naoufal")
unsplash.users.photos("naoufal", "popular")
.then(toJson)
.then(json => {
// Your code
});
```
---

### users.likes(username, page, perPage)
### users.likes(username, page, perPage, orderBy)
Get a list of photos liked by a user.

__Arguments__

| Argument | Type | Opt/Required |
|---|---|---|
|__`username`__|_string_|Required|
|__`page`__|_number_|Optional|
|__`perPage`__|_number_|Optional|
| Argument | Type | Opt/Required | Notes |
|---|---|---|---|
|__`username`__|_string_|Required||
|__`page`__|_number_|Optional||
|__`perPage`__|_number_|Optional||
|__`orderBy`__|_string_|Optional|`latest`, `popular` or `oldest`|

__Example__
```js
unsplash.users.likes("naoufal", 2, 15)
unsplash.users.likes("naoufal", 2, 15, "popular")
.then(toJson)
.then(json => {
// Your code
Expand Down Expand Up @@ -487,19 +489,20 @@ unsplash.categories.categoryPhotos(4, 3, 15)

<div id="collections" />

### collections.listCollections(page, perPage)
### collections.listCollections(page, perPage, orderBy)
Get a single page from the list of all collections.

__Arguments__

| Argument | Type | Opt/Required |
|---|---|---|
|__`page`__|_number_|Optional|
|__`perPage`__|_number_|Optional|
| Argument | Type | Opt/Required |Notes|
|---|---|---|---|
|__`page`__|_number_|Optional||
|__`perPage`__|_number_|Optional||
|__`orderBy`__|_string_|Optional|`latest`, `popular` or `oldest`|

__Example__
```js
unsplash.collections.listCollections(1, 10)
unsplash.collections.listCollections(1, 10, "popular")
.then(toJson)
.then(json => {
// Your code
Expand Down Expand Up @@ -567,39 +570,42 @@ unsplash.collections.getCuratedCollection(88)
```
---

### collections.getCollectionPhotos(id)
### collections.getCollectionPhotos(id, orderBy)
Retrieve a collection鈥檚 photos.

__Arguments__

| Argument | Type | Opt/Required |
|---|---|---|
|__`id`__|_number_|Required|
| Argument | Type | Opt/Required | Notes |
|---|---|---|---|
|__`id`__|_number_|Required||
|__`orderBy`__|_string_|Optional|`latest`, `popular` or `oldest`|



__Example__
```js
unsplash.collections.getCollectionPhotos(123456)
unsplash.collections.getCollectionPhotos(123456, "popular")
.then(toJson)
.then(json => {
// Your code
});
```
---

### collections.getCuratedCollectionPhotos(id)
### collections.getCuratedCollectionPhotos(id, orderBy)
Or, for a curated collection:

__Arguments__

| Argument | Type | Opt/Required |
|---|---|---|
|__`id`__|_number_|Required|
| Argument | Type | Opt/Required | Notes |
|---|---|---|---|
|__`id`__|_number_|Required||
|__`orderBy`__|_string_|Optional|`latest`, `popular` or `oldest`|


__Example__
```js
unsplash.collections.getCuratedCollectionPhotos(88)
unsplash.collections.getCuratedCollectionPhotos(88, "popular")
.then(toJson)
.then(json => {
// Your code
Expand Down
30 changes: 22 additions & 8 deletions src/methods/collections.js
Expand Up @@ -2,7 +2,7 @@

export default function collections(): Object {
return {
listCollections: (page = 1, perPage = 10) => {
listCollections: (page: number = 1, perPage: number = 10) => {
const url = "/collections";

const query = {
Expand All @@ -17,7 +17,7 @@ export default function collections(): Object {
});
},

listCuratedCollections: (page = 1, perPage = 10) => {
listCuratedCollections: (page: number = 1, perPage: number = 10) => {
const url = "/collections/curated";
const query = {
page,
Expand All @@ -43,7 +43,7 @@ export default function collections(): Object {

updateCollection: createUpdateCollection.bind(this),

deleteCollection: (id) => {
deleteCollection: (id: string) => {
const url = `/collections/${id}`;

return this.request({
Expand All @@ -52,7 +52,7 @@ export default function collections(): Object {
});
},

addPhotoToCollection: (collectionId, photoId) => {
addPhotoToCollection: (collectionId: string, photoId: string) => {
const url = `/collections/${collectionId}/add`;

return this.request({
Expand All @@ -75,7 +75,7 @@ export default function collections(): Object {
};
}

function collection(isCurated, id) {
function collection(isCurated: bool, id: string) {
const url = isCurated
? `/collections/curated/${id}`
: `/collections/${id}`;
Expand All @@ -86,18 +86,32 @@ function collection(isCurated, id) {
});
}

function collectionPhotos(isCurated, id) {
function collectionPhotos(
isCurated: bool,
id: string,
orderBy: string = "latest"
) {
const url = isCurated
? `/collections/curated/${id}/photos`
: `/collections/${id}/photos`;

const query = {
order_by: orderBy
};

return this.request({
url: url,
method: "GET"
method: "GET",
query
});
}

function createUpdateCollection(id, title, description, isPrivate) {
function createUpdateCollection(
id: ?string,
title: string,
description: string,
isPrivate: bool
) {
const url = id
? `/collections/${id}`
: "/collections";
Expand Down
5 changes: 3 additions & 2 deletions src/methods/photos.js
Expand Up @@ -2,12 +2,13 @@

export default function photos(): Object {
return {
listPhotos: (page = 1, perPage = 10) => {
listPhotos: (page = 1, perPage = 10, orderBy = "latest") => {
const url = "/photos";

let query = {
page,
per_page: perPage
per_page: perPage,
order_by: orderBy
};

return this.request({
Expand Down
16 changes: 11 additions & 5 deletions src/methods/users.js
Expand Up @@ -11,21 +11,27 @@ export default function users(): Object {
});
},

photos: (username: string) => {
photos: (username: string, orderBy: string = "latest") => {
const url = `/users/${username}/photos`;

const query = {
order_by: orderBy
};

return this.request({
url,
method: "GET"
method: "GET",
query
});
},

likes: (username: string, page: number = 1, perPage: number = 10) => {
likes: (username: string, page: number = 1, perPage: number = 10, orderBy: string = "latest") => {
const url = `/users/${username}/likes`;

let query = {
const query = {
page,
per_page: perPage
per_page: perPage,
order_by: orderBy
};

return this.request({
Expand Down
21 changes: 16 additions & 5 deletions test/unsplash-test.js
Expand Up @@ -241,7 +241,10 @@ describe("Unsplash", () => {
expect(spy.calls.length).toEqual(1);
expect(spy.calls[0].arguments).toEqual([{
method: "GET",
url: "/users/naoufal/photos"
url: "/users/naoufal/photos",
query: {
order_by: "latest"
}
}]);
});
});
Expand All @@ -257,7 +260,8 @@ describe("Unsplash", () => {
url: "/users/naoufal/likes",
query: {
page: 1,
per_page: 10
per_page: 10,
order_by: "latest"
}
}]);
});
Expand Down Expand Up @@ -286,7 +290,8 @@ describe("Unsplash", () => {
url: "/photos",
query: {
page: 2,
per_page: 15
per_page: 15,
order_by: "latest"
}
}]);
});
Expand Down Expand Up @@ -578,7 +583,10 @@ describe("Unsplash", () => {
expect(spy.calls.length).toEqual(1);
expect(spy.calls[0].arguments).toEqual([{
method: "GET",
url: "/collections/88/photos"
url: "/collections/88/photos",
query: {
order_by: "latest"
}
}]);
});
});
Expand All @@ -591,7 +599,10 @@ describe("Unsplash", () => {
expect(spy.calls.length).toEqual(1);
expect(spy.calls[0].arguments).toEqual([{
method: "GET",
url: "/collections/curated/88/photos"
url: "/collections/curated/88/photos",
query: {
order_by: "latest"
}
}]);
});
});
Expand Down

0 comments on commit c99f66e

Please sign in to comment.