Class: Unsplash::User

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

Overview

Unsplash User operations.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

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

Constructor Details

This class inherits a constructor from Unsplash::Client

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Unsplash::Client

Class Method Details

.currentUnsplash::User

Get the currently-logged in user.

Returns:



17
18
19
# File 'lib/unsplash/user.rb', line 17

def current
  new JSON.parse(connection.get("/me").body)
end

.find(username) ⇒ Unsplash::User

Get a user.

Parameters:

  • username (String)

    the username of the user to retrieve.

Returns:



11
12
13
# File 'lib/unsplash/user.rb', line 11

def find(username)
  new JSON.parse(connection.get("/users/#{username}").body)
end

.search(query, page = 1, per_page = 10) ⇒ Array

Get a single page of user 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 users search result per page. (default: 10, maximum: 30)

Returns:

  • (Array)

    a list of Unsplash::User objects.



33
34
35
36
37
38
39
40
# File 'lib/unsplash/user.rb', line 33

def search(query, page = 1, per_page = 10)
  params = {
    query:    query,
    page:     page,
    per_page: per_page
  }
  Unsplash::Search.search("/search/users", self, params)
end

.update_current(params) ⇒ Unsplash::User

Update the current user.

Parameters:

  • params (Hash)

    the hash of attributes to update.

Returns:



24
25
26
# File 'lib/unsplash/user.rb', line 24

def update_current(params)
  Unsplash::User.new JSON.parse(connection.put("/me", params).body)
end

Instance Method Details

#collections(page = 1, per_page = 10) ⇒ Array

Get a list of collections created by the user.

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 list of Unsplash::Collection objects.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/unsplash/user.rb', line 80

def collections(page = 1, per_page = 10)
  params = {
    page:     page,
    per_page: per_page
  }

  list = JSON.parse(connection.get("/users/#{username}/collections", params).body)
  list.map do |collection|
    Unsplash::Collection.new collection.to_hash
  end
end

#likes(page = 1, per_page = 10) ⇒ Array

Get a list of photos liked by the user.

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 list of Unsplash::Photo objects.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/unsplash/user.rb', line 64

def likes(page = 1, per_page = 10)
  params = {
    page:     page,
    per_page: per_page
  }

  list = JSON.parse(connection.get("/users/#{username}/likes", params).body)
  list.map do |photo|
    Unsplash::Photo.new photo.to_hash
  end
end

#photos(page = 1, per_page = 10) ⇒ Array

Get a list of photos uploaded by the user.

Parameters:

  • page (Integer) (defaults to: 1)

    Which page of results to return.

  • per_page (Integer) (defaults to: 10)

    The number of results per page.

Returns:

  • (Array)

    a list of Unsplash::Photo objects.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/unsplash/user.rb', line 48

def photos(page = 1, per_page = 10)
  params = {
    page:     page,
    per_page: per_page
  }

  list = JSON.parse(connection.get("/users/#{username}/photos", params).body)
  list.map do |photo|
    Unsplash::Photo.new photo.to_hash
  end
end