Class: Unsplash::Connection
- Inherits:
-
Object
- Object
- Unsplash::Connection
- Includes:
- HTTParty
- Defined in:
- lib/unsplash/connection.rb
Overview
HTTP connection to and communication with the Unsplash API.
Constant Summary collapse
- DEFAULT_VERSION =
The version of the API being used if unspecified.
"v1"- DEFAULT_API_BASE_URI =
Base URI for the Unsplash API..
"https://api.unsplash.com"- DEFAULT_OAUTH_BASE_URI =
Base URI for Unsplash OAuth.
"https://unsplash.com"
Instance Method Summary collapse
-
#authorization_url(requested_scopes = ["public"]) ⇒ String
Get OAuth URL for user authentication and authorization.
-
#authorize!(auth_code) ⇒ Object
Generate an access token given an auth code received from Unsplash.
-
#create_and_assign_token(token_extract) ⇒ OAuth2::AccessToken?
Create and assign new access token from extracted token.
-
#delete(path, params = {}) ⇒ Object
Perform a DELETE request.
-
#extract_token ⇒ Hash?
Extract hash with OAuth token attributes.
-
#get(path, params = {}) ⇒ Object
Perform a GET request.
-
#initialize(version: DEFAULT_VERSION, api_base_uri: DEFAULT_API_BASE_URI, oauth_base_uri: DEFAULT_OAUTH_BASE_URI) ⇒ Connection
constructor
Create a Connection object.
-
#post(path, params = {}) ⇒ Object
Perform a POST request.
-
#put(path, params = {}) ⇒ Object
Perform a PUT request.
Constructor Details
#initialize(version: DEFAULT_VERSION, api_base_uri: DEFAULT_API_BASE_URI, oauth_base_uri: DEFAULT_OAUTH_BASE_URI) ⇒ Connection
Create a Connection object.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/unsplash/connection.rb', line 20 def initialize(version: DEFAULT_VERSION, api_base_uri: DEFAULT_API_BASE_URI, oauth_base_uri: DEFAULT_OAUTH_BASE_URI) @application_access_key = Unsplash.configuration.application_access_key @application_secret = Unsplash.configuration.application_secret @api_version = version @api_base_uri = api_base_uri oauth_base_uri = oauth_base_uri @oauth = ::OAuth2::Client.new(@application_access_key, @application_secret, site: oauth_base_uri) do |http| http.request :multipart http.request :url_encoded http.adapter :net_http end Unsplash::Connection.base_uri @api_base_uri end |
Instance Method Details
#authorization_url(requested_scopes = ["public"]) ⇒ String
Get OAuth URL for user authentication and authorization.
39 40 41 42 |
# File 'lib/unsplash/connection.rb', line 39 def (requested_scopes = ["public"]) @oauth.auth_code.(redirect_uri: Unsplash.configuration.application_redirect_uri, scope: requested_scopes.join(" ")) end |
#authorize!(auth_code) ⇒ Object
Generate an access token given an auth code received from Unsplash. This is used internally to authenticate and authorize future user actions.
47 48 49 50 |
# File 'lib/unsplash/connection.rb', line 47 def (auth_code) @oauth_token = @oauth.auth_code.get_token(auth_code, redirect_uri: Unsplash.configuration.application_redirect_uri) # TODO check if it succeeded end |
#create_and_assign_token(token_extract) ⇒ OAuth2::AccessToken?
Create and assign new access token from extracted token. To be used with extract_token to reauthorize app without api call.
64 65 66 67 68 |
# File 'lib/unsplash/connection.rb', line 64 def create_and_assign_token(token_extract) unless token_extract.nil? @oauth_token = OAuth2::AccessToken.from_hash(@oauth, token_extract) end end |
#delete(path, params = {}) ⇒ Object
Perform a DELETE request.
94 95 96 |
# File 'lib/unsplash/connection.rb', line 94 def delete(path, params = {}) request :delete, path, params end |
#extract_token ⇒ Hash?
Extract hash with OAuth token attributes. Extracted token attributes can be used with create_and_assign_token to prevent the need for reauthorization.
56 57 58 |
# File 'lib/unsplash/connection.rb', line 56 def extract_token @oauth_token.to_hash if @oauth_token end |
#get(path, params = {}) ⇒ Object
Perform a GET request.
73 74 75 |
# File 'lib/unsplash/connection.rb', line 73 def get(path, params = {}) request :get, path, params end |
#post(path, params = {}) ⇒ Object
Perform a POST request.
87 88 89 |
# File 'lib/unsplash/connection.rb', line 87 def post(path, params = {}) request :post, path, params end |
#put(path, params = {}) ⇒ Object
Perform a PUT request.
80 81 82 |
# File 'lib/unsplash/connection.rb', line 80 def put(path, params = {}) request :put, path, params end |