Method: Unsplash::Photo.random

Defined in:
lib/unsplash/photo.rb

.random(count: nil, collections: nil, featured: nil, user: nil, query: nil, orientation: nil, content_filter: "low") ⇒ 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.

  • content_filter (String) (defaults to: "low")

    Limit results by content_filter to avoid content that may be unsuitable for younger audiences. Valid values are low, high. Defaults to low.

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



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

def random(count: nil, collections: nil, featured: nil, user: nil, query: nil, orientation: nil, content_filter: "low")
  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,
    content_filter: content_filter
  }.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