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.
- #utm_params ⇒ Object
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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/unsplash/connection.rb', line 22 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 = ::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.
40 41 42 43 |
# File 'lib/unsplash/connection.rb', line 40 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.
48 49 50 51 |
# File 'lib/unsplash/connection.rb', line 48 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.
65 66 67 68 |
# File 'lib/unsplash/connection.rb', line 65 def create_and_assign_token(token_extract) return if !token_extract @oauth_token = OAuth2::AccessToken.from_hash(@oauth, token_extract) 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.
57 58 59 |
# File 'lib/unsplash/connection.rb', line 57 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 |
#utm_params ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/unsplash/connection.rb', line 98 def utm_params { "utm_source" => Unsplash.configuration.utm_source || "api_app", "utm_medium" => "referral", "utm_campaign" => "api-credit" } end |