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.
-
.curated(page = 1, per_page = 10) ⇒ Array
Get a list of all curated collections.
-
.featured(page = 1, per_page = 10) ⇒ Array
Get a list of all featured collections.
-
.find(id, curated = false) ⇒ 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
#initialize(options = {}) ⇒ Collection
Returns a new instance of Collection.
85 86 87 88 89 |
# File 'lib/unsplash/collection.rb', line 85 def initialize( = {}) ["user"] = Unsplash::User.new ["user"] ["cover_photo"] = Unsplash::Photo.new ["cover_photo"] super() end |
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.
20 21 22 23 24 25 26 27 |
# File 'lib/unsplash/collection.rb', line 20 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.
60 61 62 63 64 65 66 67 |
# File 'lib/unsplash/collection.rb', line 60 def create(title: "", description: "", private: false) params = { title: title, description: description, private: private } Unsplash::Collection.new JSON.parse(connection.post("/collections", params).body) end |
.curated(page = 1, per_page = 10) ⇒ Array
Get a list of all curated collections.
47 48 49 50 51 52 53 54 |
# File 'lib/unsplash/collection.rb', line 47 def curated(page = 1, per_page = 10) params = { page: page, per_page: per_page } list = JSON.parse(connection.get("/collections/curated", params).body) list.map { |data| Unsplash::Collection.new(data) } end |
.featured(page = 1, per_page = 10) ⇒ Array
Get a list of all featured collections.
34 35 36 37 38 39 40 41 |
# File 'lib/unsplash/collection.rb', line 34 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, curated = false) ⇒ Unsplash::Collection
Get a specific collection.
11 12 13 14 |
# File 'lib/unsplash/collection.rb', line 11 def find(id, curated = false) url = ["/collections", (curated ? "curated" : nil), id].compact.join("/") Unsplash::Collection.new JSON.parse(connection.get(url).body) end |
.search(query, page = 1, per_page = 10) ⇒ SearchResult
Get a single page of collection results for a query.
74 75 76 77 78 79 80 81 |
# File 'lib/unsplash/collection.rb', line 74 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.
132 133 134 135 136 137 138 139 140 |
# File 'lib/unsplash/collection.rb', line 132 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.
109 110 111 112 |
# File 'lib/unsplash/collection.rb', line 109 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.
118 119 120 121 122 123 124 125 126 |
# File 'lib/unsplash/collection.rb', line 118 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.
146 147 148 149 |
# File 'lib/unsplash/collection.rb', line 146 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.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/unsplash/collection.rb', line 95 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 |