Class: Unsplash::Collection

Inherits:
Client
  • Object
show all
Defined in:
lib/unsplash/collection.rb

Overview

Unsplash Collection operations.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

connection, #connection, connection=, #reload!, #to_h

Constructor Details

#initialize(options = {}) ⇒ Collection

Returns a new instance of Collection.



71
72
73
74
75
# File 'lib/unsplash/collection.rb', line 71

def initialize(options = {})
  options["user"] = Unsplash::User.new options["user"]
  options["cover_photo"] = Unsplash::Photo.new options["cover_photo"]
  super(options)
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.

Parameters:

  • page (Integer) (defaults to: 1)

    Which page of search results to return.

  • per_page (Integer) (defaults to: 10)

    The number of search results per page. (default: 10, maximum: 30)

Returns:

  • (Array)

    A single page of the Unsplash::Collection list.



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.

Parameters:

  • title (String) (defaults to: "")

    The title of the collection.

  • description (String) (defaults to: "")

    The collection’s description. (optional)

  • private (Boolean) (defaults to: false)

    Whether to make the collection private. (optional, default false)



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

Get a list of all featured collections.

Parameters:

  • page (Integer) (defaults to: 1)

    Which page of results to return.

  • per_page (Integer) (defaults to: 10)

    The number of results per page. (default: 10, maximum: 30)

Returns:

  • (Array)

    A single page of the Unsplash::Collection list.



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.

Parameters:

  • id (Integer)

    The ID of the collection.

Returns:



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.

Parameters:

  • query (String)

    Keywords to search for.

  • page (Integer) (defaults to: 1)

    Which page of search results to return.

  • per_page (Integer) (defaults to: 10)

    The number of collections search result per page. (default: 10, maximum: 30)

Returns:

  • (SearchResult)

    a list of Unsplash::Collection objects.



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.

Parameters:

Returns:

  • (Hash)

    Collected photo metadata.



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

#destroyBoolean

Delete the collection. This does not delete the photos it contains.

Returns:

  • (Boolean)

    true on success.



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.

Parameters:

  • page (Integer) (defaults to: 1)

    Which page of photos collection to return.

  • per_page (Integer) (defaults to: 10)

    The number of photos per page. (default: 10, maximum: 30)

Returns:

  • (Array)

    The list of Unsplash::Photos in the 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.

Parameters:

Returns:

  • (Boolean)

    true on success.



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.

Parameters:

  • title (String) (defaults to: nil)

    The title of the collection.

  • description (String) (defaults to: nil)

    The collection’s description. (optional)

  • private (Boolean) (defaults to: nil)

    Whether to make the collection private. (optional)



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