Class: Unsplash::Collection
Overview
Unsplash Collection operations.
Class Method Summary collapse
-
.all(page = 1, per_page = 10) ⇒ Array
Get a list of all collections.
-
.create(title: "", description: "", private: false) ⇒ Object
Create a new collection on behalf of current user.
-
.featured(page = 1, per_page = 10) ⇒ Array
Get a list of all featured collections.
-
.find(id) ⇒ Unsplash::Collection
Get a specific collection.
-
.search(query, page = 1, per_page = 10) ⇒ SearchResult
Get a single page of collection results for a query.
Instance Method Summary collapse
-
#add(photo) ⇒ Hash
Add a photo to the collection.
-
#destroy ⇒ Boolean
Delete the collection.
-
#initialize(options = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#photos(page = 1, per_page = 10) ⇒ Array
Get a list of the photos contained in this collection.
-
#remove(photo) ⇒ Boolean
Remove a photo from the collection.
-
#update(title: nil, description: nil, private: nil) ⇒ Object
Update the collection’s attributes.
Methods inherited from Client
connection, #connection, connection=, #reload!, #to_h
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Unsplash::Client
Class Method Details
.all(page = 1, per_page = 10) ⇒ Array
Get a list of all collections.
19 20 21 22 23 24 25 26 |
# File 'lib/unsplash/collection.rb', line 19 def all(page = 1, per_page = 10) params = { page: page, per_page: per_page } list = JSON.parse(connection.get("/collections/", params).body) list.map { |data| Unsplash::Collection.new(data) } end |
.create(title: "", description: "", private: false) ⇒ Object
Create a new collection on behalf of current user.
46 47 48 49 50 51 52 53 |
# File 'lib/unsplash/collection.rb', line 46 def create(title: "", description: "", private: false) params = { title: title, description: description, private: private } Unsplash::Collection.new JSON.parse(connection.post("/collections", params).body) end |
.featured(page = 1, per_page = 10) ⇒ Array
Get a list of all featured collections.
33 34 35 36 37 38 39 40 |
# File 'lib/unsplash/collection.rb', line 33 def featured(page = 1, per_page = 10) params = { page: page, per_page: per_page } list = JSON.parse(connection.get("/collections/featured", params).body) list.map { |data| Unsplash::Collection.new(data) } end |
.find(id) ⇒ Unsplash::Collection
Get a specific collection.
11 12 13 |
# File 'lib/unsplash/collection.rb', line 11 def find(id) Unsplash::Collection.new JSON.parse(connection.get("/collections/#{id}").body) end |
.search(query, page = 1, per_page = 10) ⇒ SearchResult
Get a single page of collection results for a query.
60 61 62 63 64 65 66 67 |
# File 'lib/unsplash/collection.rb', line 60 def search(query, page = 1, per_page = 10) params = { query: query, page: page, per_page: per_page } Unsplash::Search.search("/search/collections", self, params) end |
Instance Method Details
#add(photo) ⇒ Hash
Add a photo to the collection. If the photo is already in the collection, this action has no effect.
118 119 120 121 122 123 124 125 126 |
# File 'lib/unsplash/collection.rb', line 118 def add(photo) response = JSON.parse(connection.post("/collections/#{id}/add", { photo_id: photo.id }).body) { photo_id: response["photo"]["id"], collection_id: response["collection"]["id"], user_id: response["user"]["id"], created_at: response["created_at"] } end |
#destroy ⇒ Boolean
Delete the collection. This does not delete the photos it contains.
95 96 97 98 |
# File 'lib/unsplash/collection.rb', line 95 def destroy response = connection.delete("/collections/#{id}") (200..299).include?(response.status) end |
#photos(page = 1, per_page = 10) ⇒ Array
Get a list of the photos contained in this collection.
104 105 106 107 108 109 110 111 112 |
# File 'lib/unsplash/collection.rb', line 104 def photos(page = 1, per_page = 10) params = { page: page, per_page: per_page } list = JSON.parse(connection.get("/collections/#{id}/photos", params).body) list.map { |photo| Unsplash::Photo.new photo } end |
#remove(photo) ⇒ Boolean
Remove a photo from the collection. If the photo is not in the collection, this action has no effect.
132 133 134 135 |
# File 'lib/unsplash/collection.rb', line 132 def remove(photo) response = connection.delete("/collections/#{id}/remove", photo_id: photo.id) (200..299).include?(response.status) end |
#update(title: nil, description: nil, private: nil) ⇒ Object
Update the collection’s attributes.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/unsplash/collection.rb', line 81 def update(title: nil, description: nil, private: nil) params = { title: title, description: description, private: private }.select { |k,v| v } updated = JSON.parse(connection.put("/collections/#{id}", params).body) self.title = updated["title"] self.description = updated["description"] self end |