unsplash 1.0.0.pre.rc.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/.yardopts +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +91 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/unsplash.rb +26 -0
- data/lib/unsplash/category.rb +37 -0
- data/lib/unsplash/client.rb +50 -0
- data/lib/unsplash/configuration.rb +17 -0
- data/lib/unsplash/connection.rb +116 -0
- data/lib/unsplash/curated_batch.rb +43 -0
- data/lib/unsplash/errors.rb +4 -0
- data/lib/unsplash/photo.rb +68 -0
- data/lib/unsplash/user.rb +41 -0
- data/lib/unsplash/version.rb +4 -0
- data/unsplash.gemspec +30 -0
- metadata +165 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d24a2ecbb2f9cfe1a6de6ae707118cc13d35abd1
|
4
|
+
data.tar.gz: 172c51085f995118ffc15a92c2ef17284593c675
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45f1db807acc4fa2a60de1f2ea68336ccf46007287efd891ad021be0bda1e78bf1ff7824d38532ed10e8b67635766d23f0f80482f2e4c1e51ed7d971c06f03b8
|
7
|
+
data.tar.gz: 73bb7c914802ab64a1530ab03385e6c718469677fe4e196f75177875d2eda8d273069cd8564f5173a2fd1a1b1a4a1e9497b781671c54f4b1f0d8a48129908525
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.5
|
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Aaron Klaassen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# unsplash_rb
|
2
|
+
|
3
|
+
[ ![Codeship Status for CrewLabs/unsplash_rb](https://codeship.com/projects/0d395ea0-fe45-0132-5e19-022bf5e0402e/status?branch=master)](https://codeship.com/projects/88039)
|
4
|
+
|
5
|
+
A ruby client for [the Unsplash API](https://unsplash.com/documentation).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'unsplash'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install unsplash
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Configuration
|
26
|
+
|
27
|
+
Before making requests, you must configure the gem with your application ID
|
28
|
+
and secret. If you are using Rails, you can do this in an initializer.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
Unsplash.configure do |config|
|
32
|
+
config.application_id = "YOUR APPLICATION ID"
|
33
|
+
config.application_secret = "YOUR APPLICATION SECRET"
|
34
|
+
config.application_redirect_uri = "https://your-application.com/oauth/callback"
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
### Public-scope actions
|
39
|
+
|
40
|
+
If you are *only* making public requests (i.e. nothing requiring a specific logged-in user,
|
41
|
+
for example photo uploads or private user details), then you're ready to go!
|
42
|
+
|
43
|
+
Looking for details of a specific photo? Find it by ID:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
photo = Unsplash::Photo.find("tAKXap853rY")
|
47
|
+
```
|
48
|
+
|
49
|
+
Want a bunch of pictures of cats? You're on the internet; of course you do.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
search_results = Unsplash::Photo.search("cats")
|
53
|
+
```
|
54
|
+
|
55
|
+
For a complete list of available actions, see our [documentation details]().
|
56
|
+
|
57
|
+
### User Authorization
|
58
|
+
|
59
|
+
For non-public actions, you'll have to get your user's permission to access their data.
|
60
|
+
Direct them to the Unsplash authorization URL:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
requested_scopes = ["public", "read_user", "something_else_you_are_asking_for"]
|
64
|
+
auth_url = Unsplash::Client.connection.authorization_url(requested_scopes)
|
65
|
+
```
|
66
|
+
|
67
|
+
Upon authorization, Unsplash will return to you an authentication code via your OAuth
|
68
|
+
callback handler. With that you can generate an access token:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
Unsplash::Client.connection.authorize!("the authentication code")
|
72
|
+
```
|
73
|
+
|
74
|
+
And that's it. The API actions will be available to you according to whichever
|
75
|
+
permission scopes you requested and the user authorized.
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/crewlabs/unsplash_rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
86
|
+
|
87
|
+
|
88
|
+
## License
|
89
|
+
|
90
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
91
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "unsplash"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/unsplash.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "oauth2"
|
2
|
+
require "httparty"
|
3
|
+
|
4
|
+
require "unsplash/version"
|
5
|
+
require "unsplash/configuration"
|
6
|
+
require "unsplash/errors"
|
7
|
+
require "unsplash/client"
|
8
|
+
require "unsplash/connection"
|
9
|
+
require "unsplash/user"
|
10
|
+
require "unsplash/photo"
|
11
|
+
require "unsplash/category"
|
12
|
+
require "unsplash/curated_batch"
|
13
|
+
|
14
|
+
module Unsplash
|
15
|
+
class << self
|
16
|
+
attr_accessor :configuration
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configuration
|
20
|
+
@configuration ||= Configuration.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configure
|
24
|
+
yield(configuration)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Unsplash # :nodoc:
|
2
|
+
|
3
|
+
# Unsplash Category operations.
|
4
|
+
class Category < Client
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# Get a list of all of the Unsplash photo categories.
|
9
|
+
# @return [Array] The list categories.
|
10
|
+
def all
|
11
|
+
JSON.parse(connection.get("/categories/").body).map do |category|
|
12
|
+
new category
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Get an Unsplash Category.
|
17
|
+
# @param id [Integer] The ID of the category to retrieve.
|
18
|
+
# @return [Unsplash::Category] The specified category.
|
19
|
+
def find(id)
|
20
|
+
new JSON.parse(connection.get("/categories/#{id}").body)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Get a list of all photos in this category.
|
25
|
+
# @param page [Integer] Which page of search results to return.
|
26
|
+
# @param per_page [Integer] The number of search results per page.
|
27
|
+
# @return [Array] A single page of +Unsplash::Photo+s.
|
28
|
+
def photos(page = 1, per_page = 10)
|
29
|
+
params = {
|
30
|
+
page: page,
|
31
|
+
per_page: per_page
|
32
|
+
}
|
33
|
+
list = JSON.parse(connection.get("/categories/#{id}/photos", params).body)
|
34
|
+
list.map { |photo| Unsplash::Photo.new photo }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Unsplash # :nodoc:
|
2
|
+
|
3
|
+
# Common functionality across Unsplash API objects.
|
4
|
+
class Client
|
5
|
+
|
6
|
+
# Build an Unsplash object with the given attributes.
|
7
|
+
# @param attrs [Hash]
|
8
|
+
def initialize(attrs = {})
|
9
|
+
@attributes = OpenStruct.new(attrs)
|
10
|
+
end
|
11
|
+
|
12
|
+
# (Re)load full object details from Unsplash.
|
13
|
+
# @return [Unspash::Client] Itself, with full details reloaded.
|
14
|
+
def reload!
|
15
|
+
if links && links["self"]
|
16
|
+
attrs = JSON.parse(connection.get(links["self"]).body)
|
17
|
+
@attributes = OpenStruct.new(attrs)
|
18
|
+
self
|
19
|
+
else
|
20
|
+
raise Unsplash::Error.new "Missing self link for #{self.class} with ID #{self.id}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# @private
|
25
|
+
def method_missing(method, *args, &block)
|
26
|
+
@attributes.send(method, *args, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
# The connection object being used to communicate with Unsplash.
|
30
|
+
# @return [Unsplash::Connection] the connection
|
31
|
+
def connection
|
32
|
+
self.class.connection
|
33
|
+
end
|
34
|
+
|
35
|
+
class << self
|
36
|
+
# The connection object being used to communicate with Unsplash.
|
37
|
+
# @return [Unsplash::Connection] the connection
|
38
|
+
def connection
|
39
|
+
@@connection ||= Connection.new
|
40
|
+
end
|
41
|
+
|
42
|
+
# Assign a default connection object.
|
43
|
+
# @param conn [Unsplash::Connection] the connection
|
44
|
+
# @return [Unsplash::Connection] the connection
|
45
|
+
def connection=(conn)
|
46
|
+
@@connection = conn
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Unsplash # :nodoc:
|
2
|
+
class Configuration # :nodoc:
|
3
|
+
attr_accessor :application_id
|
4
|
+
attr_accessor :application_secret
|
5
|
+
attr_accessor :application_redirect_uri
|
6
|
+
attr_writer :test
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@test = true
|
10
|
+
end
|
11
|
+
|
12
|
+
def test?
|
13
|
+
!!@test
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module Unsplash #:nodoc:
|
2
|
+
|
3
|
+
# HTTP connection to and communication with the Unsplash API.
|
4
|
+
class Connection
|
5
|
+
include HTTParty
|
6
|
+
|
7
|
+
# The version of the API being used if unspecified.
|
8
|
+
DEFAULT_VERSION = "v1"
|
9
|
+
|
10
|
+
# Base URI for the Unsplash API..
|
11
|
+
DEFAULT_API_BASE_URI = "https://api.unsplash.com"
|
12
|
+
|
13
|
+
# Base URI for Unsplash OAuth.
|
14
|
+
DEFAULT_OAUTH_BASE_URI = "https://unsplash.com"
|
15
|
+
|
16
|
+
# Create a Connection object.
|
17
|
+
# @param version [String] The Unsplash API version to use.
|
18
|
+
# @param api_base_uri [String] Base URI at which to make API calls.
|
19
|
+
# @param oauth_base_uri [String] Base URI for OAuth requests.
|
20
|
+
def initialize(version: DEFAULT_VERSION, api_base_uri: DEFAULT_API_BASE_URI, oauth_base_uri: DEFAULT_OAUTH_BASE_URI)
|
21
|
+
@application_id = Unsplash.configuration.application_id
|
22
|
+
@application_secret = Unsplash.configuration.application_secret
|
23
|
+
@api_version = version
|
24
|
+
@api_base_uri = api_base_uri
|
25
|
+
|
26
|
+
oauth_base_uri = oauth_base_uri
|
27
|
+
@oauth = ::OAuth2::Client.new(@application_id, @application_secret, site: oauth_base_uri) do |http|
|
28
|
+
http.request :multipart
|
29
|
+
http.request :url_encoded
|
30
|
+
http.adapter :net_http
|
31
|
+
end
|
32
|
+
|
33
|
+
Unsplash::Connection.base_uri @api_base_uri
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get OAuth URL for user authentication and authorization.
|
37
|
+
# @param requested_scopes [Array] An array of permission scopes being requested.
|
38
|
+
# @return [String] The authorization URL.
|
39
|
+
def authorization_url(requested_scopes = ["public"])
|
40
|
+
@oauth.auth_code.authorize_url(redirect_uri: Unsplash.configuration.application_redirect_uri,
|
41
|
+
scope: requested_scopes.join(" "))
|
42
|
+
end
|
43
|
+
|
44
|
+
# Generate an access token given an auth code received from Unsplash.
|
45
|
+
# This is used internally to authenticate and authorize future user actions.
|
46
|
+
# @param auth_code [String] The OAuth authentication code from Unsplash.
|
47
|
+
def authorize!(auth_code)
|
48
|
+
@oauth_token = @oauth.auth_code.get_token(auth_code, redirect_uri: Unsplash.configuration.application_redirect_uri)
|
49
|
+
# TODO check if it succeeded
|
50
|
+
end
|
51
|
+
|
52
|
+
# Perform a GET request.
|
53
|
+
# @param path [String] The path at which to make ther request.
|
54
|
+
# @param params [Hash] A hash of request parameters.
|
55
|
+
def get(path, params = {})
|
56
|
+
request :get, path, params
|
57
|
+
end
|
58
|
+
|
59
|
+
# Perform a PUT request.
|
60
|
+
# @param path [String] The path at which to make ther request.
|
61
|
+
# @param params [Hash] A hash of request parameters.
|
62
|
+
def put(path, params = {})
|
63
|
+
request :put, path, params
|
64
|
+
end
|
65
|
+
|
66
|
+
# Perform a POST request.
|
67
|
+
# @param path [String] The path at which to make ther request.
|
68
|
+
# @param params [Hash] A hash of request parameters.
|
69
|
+
def post(path, params = {})
|
70
|
+
request :post, path, params
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def request(verb, path, params = {})
|
76
|
+
raise ArgumentError.new "Invalid http verb #{verb}" if ![:get, :post, :put].include?(verb)
|
77
|
+
|
78
|
+
headers = {
|
79
|
+
"Accept-Version" => @api_version
|
80
|
+
# Anything else? User agent?
|
81
|
+
}
|
82
|
+
|
83
|
+
response = if @oauth_token
|
84
|
+
refresh_token!
|
85
|
+
|
86
|
+
param_key = verb == :post ? :body : :params
|
87
|
+
@oauth_token.public_send(verb, @api_base_uri + path, param_key => params, headers: headers)
|
88
|
+
|
89
|
+
else
|
90
|
+
self.class.public_send verb, path, query: params, headers: public_auth_header
|
91
|
+
end
|
92
|
+
|
93
|
+
status_code = response.respond_to?(:status) ? response.status : response.code
|
94
|
+
|
95
|
+
if !(200..299).include?(status_code)
|
96
|
+
body = JSON.parse(response.body)
|
97
|
+
msg = body["error"] || body["errors"].join(" ")
|
98
|
+
raise Unsplash::Error.new msg
|
99
|
+
end
|
100
|
+
|
101
|
+
response
|
102
|
+
end
|
103
|
+
|
104
|
+
def public_auth_header
|
105
|
+
{ "Authorization" => "Client-ID #{@application_id}" }
|
106
|
+
end
|
107
|
+
|
108
|
+
def refresh_token!
|
109
|
+
return if !@oauth_token.expired?
|
110
|
+
|
111
|
+
@oauth_token = @oauth_token.refresh_token
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Unsplash # :nodoc:
|
2
|
+
|
3
|
+
# Unsplash Curated Batch operations.
|
4
|
+
class CuratedBatch < Client
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# Get a specific curated batch.
|
9
|
+
# @param id [Integer] The ID of the batch.
|
10
|
+
# @return [Unsplash::CuratedBatch] The requested batch.
|
11
|
+
def find(id)
|
12
|
+
batch = Unsplash::CuratedBatch.new JSON.parse(connection.get("/curated_batches/#{id}").body)
|
13
|
+
batch.curator = Unsplash::User.new batch.curator
|
14
|
+
batch
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get a list of all curated batches.
|
18
|
+
# @param page [Integer] Which page of search results to return.
|
19
|
+
# @param per_page [Integer] The number of search results per page.
|
20
|
+
# @return [Array] A single page of the +Unsplash::CuratedBatch+ list.
|
21
|
+
def all(page = 1, per_page = 10)
|
22
|
+
params = {
|
23
|
+
page: page,
|
24
|
+
per_page: per_page
|
25
|
+
}
|
26
|
+
list = JSON.parse(connection.get("/curated_batches/", params).body)
|
27
|
+
list.map do |cb|
|
28
|
+
batch = Unsplash::CuratedBatch.new cb
|
29
|
+
batch.curator = Unsplash::User.new batch.curator
|
30
|
+
batch
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Get a list of the photos contained in this batch.
|
36
|
+
# @return [Array] The list of +Unsplash::Photo+s in the batch.
|
37
|
+
def photos
|
38
|
+
list = JSON.parse(connection.get("/curated_batches/#{id}/photos").body)
|
39
|
+
list.map { |photo| Unsplash::Photo.new photo }
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Unsplash # :nodoc:
|
2
|
+
|
3
|
+
# Unsplash Photo operations.
|
4
|
+
class Photo < Client
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# Get a photo. Can be cropped or resized using the optional parameters.
|
9
|
+
# @param id [String] The ID of the photo to retrieve.
|
10
|
+
# @param width [Integer] Width of customized version of the photo.
|
11
|
+
# @param height [Integer] Height of the customized version of the photo.
|
12
|
+
# @param crop_rect [String] A comma-separated list (x,y,width,height) of the rectangle to crop from the photo.
|
13
|
+
# @return [Unsplash::Photo] The Unsplash Photo.
|
14
|
+
def find(id, width: nil, height: nil, crop_rect: nil)
|
15
|
+
custom = {
|
16
|
+
w: width,
|
17
|
+
h: height,
|
18
|
+
rect: crop_rect
|
19
|
+
}.delete_if { |k,v| !v }
|
20
|
+
photo = Unsplash::Photo.new JSON.parse(connection.get("/photos/#{id}", custom).body)
|
21
|
+
photo.user = Unsplash::User.new photo.user
|
22
|
+
photo
|
23
|
+
end
|
24
|
+
|
25
|
+
# Search for photos by keyword.
|
26
|
+
# @param query [String] Keywords to search for.
|
27
|
+
# @param page [Integer] Which page of search results to return.
|
28
|
+
# @param per_page [Integer] The number of search results per page.
|
29
|
+
def search(query, page = 1, per_page = 10)
|
30
|
+
params = {
|
31
|
+
query: query,
|
32
|
+
page: page,
|
33
|
+
per_page: per_page
|
34
|
+
}
|
35
|
+
parse_list connection.get("/photos/search/", params).body
|
36
|
+
end
|
37
|
+
|
38
|
+
# Get a list of all photos.
|
39
|
+
# @param page [Integer] Which page of search results to return.
|
40
|
+
# @param per_page [Integer] The number of search results per page.
|
41
|
+
# @return [Array] A single page of +Unsplash::Photo+ search results.
|
42
|
+
def all(page = 1, per_page = 10)
|
43
|
+
params = {
|
44
|
+
page: page,
|
45
|
+
per_page: per_page
|
46
|
+
}
|
47
|
+
parse_list connection.get("/photos/", params).body
|
48
|
+
end
|
49
|
+
|
50
|
+
# Upload a photo on behalf of the current user.
|
51
|
+
# @param filepath [String] The local path of the image file to upload.
|
52
|
+
# @return [Unsplash::Photo] The uploaded photo.
|
53
|
+
def create(filepath)
|
54
|
+
file = Faraday::UploadIO.new(filepath, "image/jpeg")
|
55
|
+
photo = Unsplash::Photo.new JSON.parse(connection.post("/photos", photo: file).body)
|
56
|
+
photo.user = Unsplash::User.new photo.user
|
57
|
+
photo
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def parse_list(json)
|
63
|
+
JSON.parse(json).map { |photo| new photo }
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Unsplash # nodoc:
|
2
|
+
|
3
|
+
# Unsplash User operations.
|
4
|
+
class User < Client
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# Get a user.
|
9
|
+
# @param username [String] the username of the user to retrieve.
|
10
|
+
# @return [Unsplash::User] the Unsplash User.
|
11
|
+
def find(username)
|
12
|
+
new JSON.parse(connection.get("/users/#{username}").body)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Get the currently-logged in user.
|
16
|
+
# @return [Unsplash::User] the current user.
|
17
|
+
def current
|
18
|
+
new JSON.parse(connection.get("/me").body)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Update the current user.
|
22
|
+
# @param params [Hash] the hash of attributes to update.
|
23
|
+
# @return [Unsplash::User] the updated user.
|
24
|
+
def update_current(params)
|
25
|
+
Unsplash::User.new JSON.parse(connection.put("/me", params).body)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get a list of photos uploaded by the user.
|
31
|
+
# @return [Array] a list of +Unsplash::Photo+ objects.
|
32
|
+
def photos
|
33
|
+
list = JSON.parse(connection.get("/users/#{username}/photos").body)
|
34
|
+
list.map do |photo|
|
35
|
+
Unsplash::Photo.new photo.to_hash
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/unsplash.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'unsplash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "unsplash"
|
8
|
+
spec.version = Unsplash::VERSION
|
9
|
+
spec.authors = ["Aaron Klaassen"]
|
10
|
+
spec.email = ["aaron@crew.co"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby wrapper for the Unsplash API.}
|
13
|
+
spec.homepage = "https://github.com/CrewLabs/unsplash_rb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "httparty", "~> 0.13.5"
|
22
|
+
spec.add_dependency "oauth2", "~> 1.0.0"
|
23
|
+
|
24
|
+
# spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.3.0"
|
27
|
+
spec.add_development_dependency "vcr", "~> 2.9.3"
|
28
|
+
spec.add_development_dependency "webmock", "~> 1.20.4"
|
29
|
+
spec.add_development_dependency "pry"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unsplash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.pre.rc.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Klaassen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.13.5
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.13.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oauth2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.3.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.3.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: vcr
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.9.3
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.9.3
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.20.4
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.20.4
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- aaron@crew.co
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".ruby-version"
|
121
|
+
- ".travis.yml"
|
122
|
+
- ".yardopts"
|
123
|
+
- CODE_OF_CONDUCT.md
|
124
|
+
- Gemfile
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- lib/unsplash.rb
|
131
|
+
- lib/unsplash/category.rb
|
132
|
+
- lib/unsplash/client.rb
|
133
|
+
- lib/unsplash/configuration.rb
|
134
|
+
- lib/unsplash/connection.rb
|
135
|
+
- lib/unsplash/curated_batch.rb
|
136
|
+
- lib/unsplash/errors.rb
|
137
|
+
- lib/unsplash/photo.rb
|
138
|
+
- lib/unsplash/user.rb
|
139
|
+
- lib/unsplash/version.rb
|
140
|
+
- unsplash.gemspec
|
141
|
+
homepage: https://github.com/CrewLabs/unsplash_rb
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">"
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: 1.3.1
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.4.3
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: Ruby wrapper for the Unsplash API.
|
165
|
+
test_files: []
|