Skip to content

Commit

Permalink
Merge 7d421f0 into 4a8ebc2
Browse files Browse the repository at this point in the history
  • Loading branch information
adimasuhid committed Oct 22, 2016
2 parents 4a8ebc2 + 7d421f0 commit 66ae4cd
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/unsplash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require "unsplash/curated_batch"
require "unsplash/collection"
require "unsplash/stats"
require "unsplash/search"

module Unsplash
class << self
Expand Down
14 changes: 13 additions & 1 deletion lib/unsplash/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ def create(title: "", description: "", private: false)
Unsplash::Collection.new JSON.parse(connection.post("/collections", params).body)
end

# Get a single page of collection results for a query.
# @param query [String] Keywords to search for.
# @param page [Integer] Which page of search results to return.
# @return [Array] a list of +Unsplash::Collection+ objects.
def search(query, page = 1)
params = {
query: query,
page: page
}
Unsplash::Search.search("/search/collections", self, params)
end

end

def initialize(options = {})
Expand Down Expand Up @@ -114,4 +126,4 @@ def remove(photo)
end

end
end
end
8 changes: 3 additions & 5 deletions lib/unsplash/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@ def random(categories: nil, collections: nil, featured: nil, user: nil, query: n
# Search for photos by keyword.
# @param query [String] Keywords to search for.
# @param page [Integer] Which page of search results to return.
# @param per_page [Integer] The number of search results per page.
def search(query, page = 1, per_page = 10)
def search(query, page = 1)
params = {
query: query,
page: page,
per_page: per_page
page: page
}
parse_list connection.get("/photos/search/", params).body
Unsplash::Search.search("/search/photos", self, params)
end

# Get a list of all photos.
Expand Down
19 changes: 19 additions & 0 deletions lib/unsplash/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Unsplash # :nodoc:

# Unsplash Search operations
class Search < Client
class << self
# Helper class to facilitate search on multiple classes
# @param url [String] Url to be searched into
# @param klass [Class] Class to instantiate the contents with
# @param params [Hash] Params for the search
def search(url, klass, params)
list = JSON.parse(connection.get(url, params).body)

list["results"].map do |content|
klass.new content.to_hash
end
end
end
end
end
16 changes: 14 additions & 2 deletions lib/unsplash/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def update_current(params)
Unsplash::User.new JSON.parse(connection.put("/me", params).body)
end

# Get a single page of user results for a query.
# @param query [String] Keywords to search for.
# @param page [Integer] Which page of search results to return.
# @return [Array] a list of +Unsplash::User+ objects.
def search(query, page = 1)
params = {
query: query,
page: page
}
Unsplash::Search.search("/search/users", self, params)
end

end

# Get a list of photos uploaded by the user.
Expand All @@ -45,7 +57,7 @@ def likes
end
end

# Get a list of photos liked by the user.
# Get a list of collections created by the user.
# @return [Array] a list of +Unsplash::Collection+ objects.
def collections
list = JSON.parse(connection.get("/users/#{username}/collections").body)
Expand All @@ -56,4 +68,4 @@ def collections

end

end
end
100 changes: 100 additions & 0 deletions spec/cassettes/collections.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions spec/cassettes/photos.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions spec/cassettes/users.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 66ae4cd

Please sign in to comment.