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.
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 |