class Unsplash::Collection

Unsplash Collection operations.

Public Class Methods

all(page = 1, per_page = 10) click to toggle source

Get a list of all collections. @param page [Integer] Which page of search results to return. @param per_page [Integer] The number of search results per page. (default: 10, maximum: 30) @return [Array] A single page of the Unsplash::Collection list.

# 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) click to toggle source

Create a new collection on behalf of current user. @param title [String] The title of the collection. @param description [String] The collection's description. (optional) @param private [Boolean] Whether to make the collection private. (optional, default false)

# 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) click to toggle source

Get a list of all curated collections. @param page [Integer] Which page of search results to return. @param per_page [Integer] The number of search results per page. (default: 10, maximum: 30) @return [Array] A single page of the Unsplash::Collection list.

# 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
find(id, curated = false) click to toggle source

Get a specific collection. @param id [Integer] The ID of the collection. @return [Unsplash::Collection] The requested collection.

# 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
new(options = {}) click to toggle source
Calls superclass method Unsplash::Client::new
# File lib/unsplash/collection.rb, line 85
def initialize(options = {})
  options["user"] = Unsplash::User.new options["user"]
  options["cover_photo"] = Unsplash::Photo.new options["cover_photo"]
  super(options)
end

Public Instance Methods

add(photo) click to toggle source

Add a photo to the collection. If the photo is already in the collection, this action has no effect. @param [Unsplash::Photo] The photo to add. @return [Hash] Collected photo metadata.

# 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() click to toggle source

Delete the collection. This does not delete the photos it contains. @return [Boolean] true on success.

# 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) click to toggle source

Get a list of the photos contained in this collection. @param page [Integer] Which page of photos collection to return. @param per_page [Integer] The number of photos per page. (default: 10, maximum: 30) @return [Array] The list of +Unsplash::Photo+s in the collection.

# 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) click to toggle source

Remove a photo from the collection. If the photo is not in the collection, this action has no effect. @param [Unsplash::Photo] The photo to remove. @return [Boolean] true on success.

# 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) click to toggle source

Update the collection's attributes. @param title [String] The title of the collection. @param description [String] The collection's description. (optional) @param private [Boolean] Whether to make the collection private. (optional)

# 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