Skip to content

Commit

Permalink
Merge pull request #64 from unsplash/v2
Browse files Browse the repository at this point in the history
Version 2.0
  • Loading branch information
aaronklaassen committed Mar 21, 2019
2 parents a25c994 + 81622fc commit 09dc5b8
Show file tree
Hide file tree
Showing 29 changed files with 2,709 additions and 2,972 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
/pkg/
/spec/reports/
/tmp/
.idea/
.idea/

config.env
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.4.0
ruby-2.5.3
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: ruby
rvm:
- 2.2.5
- 2.3.1
- 2.4.0
before_install: gem install bundler -v 1.10.2
- 2.3.8
- 2.4.5
- 2.5.4
- 2.6.2
before_install: gem install bundler
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ Unsplash.configure do |config|
config.application_secret = "YOUR APPLICATION SECRET"
config.application_redirect_uri = "https://your-application.com/oauth/callback"
config.utm_source = "alices_terrific_client_app"

# optional:
config.logger = MyCustomLogger.new
end
```

#### API Guidelines

All API applications must abide by the [API Guidelines](https://medium.com/unsplash/unsplash-api-guidelines-28e0216e6daa).
All API applications must abide by the [API Guidelines](https://help.unsplash.com/api-guidelines/unsplash-api-guidelines).

As part of [the API guidelines](https://medium.com/unsplash/unsplash-api-guidelines-28e0216e6daa), all API uses are required to use utm links when providing credit to photographers and Unsplash. Set the `config.utm_source` to your app's name to automatically append the utm source.
As part of [the API guidelines](https://help.unsplash.com/api-guidelines/unsplash-api-guidelines), all API uses are required to use utm links when providing credit to photographers and Unsplash. Set the `config.utm_source` to your app's name to automatically append the utm source.

### Public-scope actions

Expand Down Expand Up @@ -83,7 +86,7 @@ permission scopes you requested and the user authorized.

### Hotlinking

Hotlinking the [Unsplash image files is required](https://medium.com/@lukechesser/unsplash-api-guidelines-hotlinking-images-6c6b51030d2a)
Hotlinking the [Unsplash image files is required](https://help.unsplash.com/api-guidelines/more-on-each-guideline/guideline-hotlinking-images)

Unlike most APIs, Unsplash requires for the image URLs returned by the API to be directly used or embedded in your applications (generally referred to as hotlinking). By using the CDN and embedding the photo URLs in your application, Unsplash can better track photo views and pass those stats on to the photographer, providing them with context for how popular their photo is and how it's being used.

Expand Down
5 changes: 5 additions & 0 deletions config.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UNSPLASH_ACCESS_KEY=abc123
UNSPLASH_SECRET_KEY=def456
UNSPLASH_BEARER_TOKEN=ghi789
UNSPLASH_API_URI=https://api.unsplash.com/
UNSPLASH_OAUTH_URI=https://unsplash.com/
2 changes: 0 additions & 2 deletions lib/unsplash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
require "unsplash/connection"
require "unsplash/user"
require "unsplash/photo"
require "unsplash/category"
require "unsplash/curated_batch"
require "unsplash/collection"
require "unsplash/stats"
require "unsplash/search"
Expand Down
33 changes: 0 additions & 33 deletions lib/unsplash/category.rb

This file was deleted.

18 changes: 2 additions & 16 deletions lib/unsplash/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ class << self
# Get a specific collection.
# @param id [Integer] The ID of the collection.
# @return [Unsplash::Collection] The requested collection.
def find(id, curated = false)
url = ["/collections", (curated ? "curated" : nil), id].compact.join("/")
Unsplash::Collection.new JSON.parse(connection.get(url).body)
def find(id)
Unsplash::Collection.new JSON.parse(connection.get("/collections/#{id}").body)
end

# Get a list of all collections.
Expand Down Expand Up @@ -40,19 +39,6 @@ def featured(page = 1, per_page = 10)
list.map { |data| Unsplash::Collection.new(data) }
end

# Get a list of all curated collections.
# @param page [Integer] Which page of search results to return.
# @param per_page [Integer] The number of search results per page. (default: 10, maximum: 30)
# @return [Array] A single page of the +Unsplash::Collection+ list.
def curated(page = 1, per_page = 10)
params = {
page: page,
per_page: per_page
}
list = JSON.parse(connection.get("/collections/curated", params).body)
list.map { |data| Unsplash::Collection.new(data) }
end

# Create a new collection on behalf of current user.
# @param title [String] The title of the collection.
# @param description [String] The collection's description. (optional)
Expand Down
10 changes: 0 additions & 10 deletions lib/unsplash/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,5 @@ def initialize
def test?
!!@test
end

def application_id=(key)
logger.warn "Configuring application_id is deprecated. Use application_access_key."
self.application_access_key = key
end

def application_id
application_access_key
end

end
end
47 changes: 30 additions & 17 deletions lib/unsplash/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ def extract_token
# @param token_extract [Hash] OAuth token hash from #extract_token.
# @return [OAuth2::AccessToken, nil] New access token object.
def create_and_assign_token(token_extract)
unless token_extract.nil?
@oauth_token = OAuth2::AccessToken.from_hash(@oauth, token_extract)
end
return if !token_extract
@oauth_token = OAuth2::AccessToken.from_hash(@oauth, token_extract)
end

# Perform a GET request.
Expand Down Expand Up @@ -112,13 +111,17 @@ def request(verb, path, params = {})
# Anything else? User agent?
}

response = if @oauth_token
refresh_token!
response = begin
if @oauth_token
refresh_token!

param_key = verb == :post ? :body : :params
@oauth_token.public_send(verb, @api_base_uri + path, param_key => params, headers: headers)
else
self.class.public_send verb, path, query: params, headers: public_auth_header
param_key = verb == :post ? :body : :params
@oauth_token.public_send(verb, @api_base_uri + path, param_key => params, headers: headers)
else
self.class.public_send verb, path, query: params, headers: public_auth_header
end
rescue OAuth2::Error => e
OpenStruct.new(headers: {}, status: 403, body: e.error_message(e.response.body))
end

if response.headers["Warning"]
Expand All @@ -127,14 +130,17 @@ def request(verb, path, params = {})

status_code = response.respond_to?(:status) ? response.status : response.code

begin
if !(200..299).include?(status_code)
body = JSON.parse(response.body)
msg = body["error"] || body["errors"].join(" ")
raise Unsplash::Error.new msg
end
rescue JSON::ParserError
raise Unsplash::Error.new response.body
case status_code
when 200..299
response
when 401
raise Unsplash::UnauthorizedError.new(error_message(response))
when 403
raise Unsplash::ForbiddenError.new(error_message(response))
when 404
raise Unsplash::NotFoundError.new(error_message(response))
else
raise Unsplash::Error.new(error_message(response))
end

response
Expand All @@ -157,5 +163,12 @@ def refresh_token!

@oauth_token = @oauth_token.refresh_token
end

def error_message(response)
body = JSON.parse(response.body)
body["error"] || body["errors"].join(" ")
rescue JSON::ParserError
raise Unsplash::Error.new(response.body)
end
end
end
43 changes: 0 additions & 43 deletions lib/unsplash/curated_batch.rb

This file was deleted.

4 changes: 4 additions & 0 deletions lib/unsplash/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ module Unsplash # :nodoc:
class Error < StandardError; end
# Raise for deprecation errors
class DeprecationError < Error; end

class UnauthorizedError < Error; end
class ForbiddenError < Error; end
class NotFoundError < Error; end
end
46 changes: 5 additions & 41 deletions lib/unsplash/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,38 @@ def unlike!
true
end

# Download a photo.
# Track the download of a photo.
# @return [String] URL of image file for download.
def download!
def track_download
connection.get(links.download_location)["url"]
end

class << self

# Get a photo. Can be cropped or resized using the optional parameters.
# @param id [String] The ID of the photo to retrieve.
# @param width [Integer] Width of customized version of the photo.
# @param height [Integer] Height of the customized version of the photo.
# @param crop_rect [String] A comma-separated list (x,y,width,height) of the rectangle to crop from the photo.
# @return [Unsplash::Photo] The Unsplash Photo.
def find(id, width: nil, height: nil, crop_rect: nil)
custom = {
w: width,
h: height,
rect: crop_rect
}.select { |k,v| v }
photo = Unsplash::Photo.new JSON.parse(connection.get("/photos/#{id}", custom).body)
def find(id)
photo = Unsplash::Photo.new JSON.parse(connection.get("/photos/#{id}").body)
photo.user = Unsplash::User.new photo.user
photo
end

# Get a random photo or set of photos. The photo selection pool can be narrowed using
# a combination of optional parameters. Can also optionally specify a custom image size.
# @param count [Integer] Number of photos required. Default=1, Max=30
# @param categories [Array] Limit selection to given category ID's.
# @param featured [Boolean] Limit selection to featured photos.
# @param user [String] Limit selection to given User's ID.
# @param query [String] Limit selection to given search query.
# @param width [Integer] Width of customized version of the photo.
# @param height [Integer] Height of the customized version of the photo.
# @param orientation [String] Filter by orientation of the photo. Valid values are landscape, portrait, and squarish.
# @return [Unsplash::Photo] An Unsplash Photo if count parameter is omitted
# @return [Array] An array of Unsplash Photos if the count parameter is specified. An array is returned even if count is 1
def random(count: nil, categories: nil, collections: nil, featured: nil, user: nil, query: nil, width: nil, height: nil, orientation: nil)
def random(count: nil, collections: nil, featured: nil, user: nil, query: nil, orientation: nil)
params = {
category: (categories && categories.join(",")),
collections: (collections && collections.join(",")),
featured: featured,
username: user,
query: query,
w: width,
h: height,
orientation: orientation
}.select { |k,v| v }
if count
Expand Down Expand Up @@ -109,28 +95,6 @@ def all(page = 1, per_page = 10, order_by = "latest")
parse_list connection.get("/photos/", params).body
end

# Get a single page from the list of the curated photos (front-page’s photos).
# @param page [Integer] Which page of search results to return.
# @param per_page [Integer] The number of search results per page. (default: 10, maximum: 30)
# @param order_by [String] How to sort the photos. (Valid values: latest, oldest, popular; default: latest)
# @return [Array] A single page of +Unsplash::Photo+ search results.
def curated(page = 1, per_page = 10, order_by = "latest")
params = {
page: page,
per_page: per_page,
order_by: order_by
}
parse_list connection.get("/photos/curated", params).body
end

# Upload a photo on behalf of the current user.
# @param filepath [String] The local path of the image file to upload.
# @return [Unsplash::Photo] The uploaded photo.
# <b>DEPRECATED</b>
def create(filepath)
raise Unsplash::DeprecationError.new "API photo-upload endpoint has been deprecated and removed."
end

private

def parse_list(json)
Expand Down
Loading

0 comments on commit 09dc5b8

Please sign in to comment.