Class: Unsplash::Photo
Overview
Unsplash Photo operations.
Class Method Summary collapse
-
.all(page = 1, per_page = 10, order_by = "latest") ⇒ Array
Get a list of all photos.
-
.find(id) ⇒ Unsplash::Photo
Get a photo.
-
.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.
-
.search(query, page = 1, per_page = 10, orientation = nil, content_filter = "low") ⇒ SearchResult
Search for photos by keyword.
Instance Method Summary collapse
- #add_utm_to_urls ⇒ Object
-
#initialize(attrs) ⇒ Photo
constructor
A new instance of Photo.
-
#like! ⇒ Boolean
Like a photo for the current user.
-
#track_download ⇒ String
Track the download of a photo.
-
#unlike! ⇒ Boolean
Unlike a photo for the current user.
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.
106 107 108 109 110 111 112 113 |
# File 'lib/unsplash/photo.rb', line 106 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.
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, 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 |
.search(query, page = 1, per_page = 10, orientation = nil, content_filter = "low") ⇒ SearchResult
Search for photos by keyword.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/unsplash/photo.rb', line 90 def search(query, page = 1, per_page = 10, orientation = nil, content_filter = "low") params = { query: query, page: page, per_page: per_page, orientation: orientation, content_filter: content_filter }.select { |_k, v| v } Unsplash::Search.search("/search/photos", self, params) end |
Instance Method Details
#add_utm_to_urls ⇒ Object
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.
13 14 15 16 |
# File 'lib/unsplash/photo.rb', line 13 def like! connection.post("/photos/#{id}/like") true end |
#track_download ⇒ String
Track the download of a photo.
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.
20 21 22 23 |
# File 'lib/unsplash/photo.rb', line 20 def unlike! connection.delete("/photos/#{id}/like") true end |