Class: Unsplash::Photo

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

Overview

Unsplash Photo operations.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

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

Constructor Details

#initialize(attrs) ⇒ Photo

Returns a new instance of Photo.


6
7
8
9
# File 'lib/unsplash/photo.rb', line 6

def initialize(attrs)
  super
  add_utm_to_urls
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, order_by = "latest") ⇒ Array

Get a list of all photos.

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)

  • order_by (String) (defaults to: "latest")

    How to sort the photos. (Valid values: latest, oldest, popular; default: latest)

Returns:

  • (Array)

    A single page of Unsplash::Photo search results.


102
103
104
105
106
107
108
109
# File 'lib/unsplash/photo.rb', line 102

def all(page = 1, per_page = 10, order_by = "latest")
  params = {
    page:     page,
    per_page: per_page,
    order_by: order_by
  }
  parse_list connection.get("/photos/", params).body
end

.find(id) ⇒ Unsplash::Photo

Get a photo. Can be cropped or resized using the optional parameters.

Parameters:

  • id (String)

    The ID of the photo to retrieve.

Returns:


42
43
44
45
46
# File 'lib/unsplash/photo.rb', line 42

def find(id)
  photo = Unsplash::Photo.new JSON.parse(connection.get("/photos/#{id}").body)
  photo.user = Unsplash::User.new photo.user
  photo
end

.random(count: nil, collections: nil, featured: nil, user: nil, query: nil, orientation: nil) ⇒ Unsplash::Photo, Array

Get a random photo or set of photos. The photo selection pool can be narrowed using a combination of optional parameters.

Parameters:

  • count (Integer) (defaults to: nil)

    Number of photos required. Default=1, Max=30

  • featured (Boolean) (defaults to: nil)

    Limit selection to featured photos.

  • user (String) (defaults to: nil)

    Limit selection to given User's ID.

  • query (String) (defaults to: nil)

    Limit selection to given search query.

  • orientation (String) (defaults to: nil)

    Filter by orientation of the photo. Valid values are landscape, portrait, and squarish.

Returns:

  • (Unsplash::Photo)

    An Unsplash Photo if count parameter is omitted

  • (Array)

    An array of Unsplash Photos if the count parameter is specified. An array is returned even if count is 1


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/unsplash/photo.rb', line 57

def random(count: nil, collections: nil, featured: nil, user: nil, query: nil, orientation: nil)
  Unsplash.configuration.logger.warn "You cannot combine 'collections' and 'query' parameters. 'query' will be ignored." if collections && query

  params = {
    collections: (collections && collections.join(",")),
    featured: featured,
    username: user,
    query:    query,
    orientation: orientation
  }.select { |k,v| v }
  if count
    params[:count] = count
    photos = parse_list connection.get("/photos/random/", params).body
    photos.map { |photo|
      photo.user = Unsplash::User.new photo[:user]
      photo
    }
  else
    photo = Unsplash::Photo.new JSON.parse(connection.get("/photos/random", params).body)
    photo.user = Unsplash::User.new photo.user
    photo
  end
end

.search(query, page = 1, per_page = 10, orientation = nil) ⇒ SearchResult

Search for photos by keyword.

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)

  • orientation (String) (defaults to: nil)

    Filter by orientation of the photo. Valid values are landscape, portrait, and squarish.

Returns:


87
88
89
90
91
92
93
94
95
# File 'lib/unsplash/photo.rb', line 87

def search(query, page = 1, per_page = 10, orientation = nil)
  params = {
    query:    query,
    page:     page,
    per_page: per_page,
    orientation: orientation
  }.select { |_k, v| v }
  Unsplash::Search.search("/search/photos", self, params)
end

Instance Method Details

#add_utm_to_urlsObject


31
32
33
34
35
# File 'lib/unsplash/photo.rb', line 31

def add_utm_to_urls
  (@attributes["urls"] || {}).each do |key, url|
    @attributes["urls"][key] = add_utm_params(url)
  end
end

#like!Boolean

Like a photo for the current user.

Returns:

  • (Boolean)

    True if successful. Will raise on error.


13
14
15
16
# File 'lib/unsplash/photo.rb', line 13

def like!
  connection.post("/photos/#{id}/like")
  true
end

#track_downloadString

Track the download of a photo.

Returns:

  • (String)

    URL of image file for download.


27
28
29
# File 'lib/unsplash/photo.rb', line 27

def track_download
  connection.get(links.download_location)["url"]
end

#unlike!Boolean

Unlike a photo for the current user.

Returns:

  • (Boolean)

    True if successful. Will raise on error.


20
21
22
23
# File 'lib/unsplash/photo.rb', line 20

def unlike!
  connection.delete("/photos/#{id}/like")
  true
end