class Unsplash::Connection
HTTP connection to and communication with the Unsplash
API.
Constants
Public Class Methods
Create a Connection
object. @param version [String] The Unsplash
API version to use. @param api_base_uri [String] Base URI at which to make API calls. @param oauth_base_uri [String] Base URI for OAuth requests.
# 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
Public Instance Methods
Create and assign new access token from extracted token. To be used with extract_token
to reauthorize app without api call. @param token_extract [Hash] OAuth token hash from extract_token
. @return [OAuth2::AccessToken, nil] New access token object.
# 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
Perform a DELETE request. @param path [String] The path at which to make ther request. @param params [Hash] A hash of request parameters.
# File lib/unsplash/connection.rb, line 94 def delete(path, params = {}) request :delete, path, params end
Extract hash with OAuth token attributes. Extracted token attributes can be used with create_and_assign_token
to prevent the need for reauthorization. @return [Hash, nil] @oauth_token converted to a hash.
# File lib/unsplash/connection.rb, line 56 def extract_token @oauth_token.to_hash if @oauth_token end
Perform a GET request. @param path [String] The path at which to make ther request. @param params [Hash] A hash of request parameters.
# File lib/unsplash/connection.rb, line 73 def get(path, params = {}) request :get, path, params end
Perform a POST request. @param path [String] The path at which to make ther request. @param params [Hash] A hash of request parameters.
# File lib/unsplash/connection.rb, line 87 def post(path, params = {}) request :post, path, params end
Perform a PUT request. @param path [String] The path at which to make ther request. @param params [Hash] A hash of request parameters.
# File lib/unsplash/connection.rb, line 80 def put(path, params = {}) request :put, path, params end
Private Instance Methods
# File lib/unsplash/connection.rb, line 139 def public_auth_header { "Authorization" => "Client-ID #{@application_access_key}" } end
# File lib/unsplash/connection.rb, line 151 def refresh_token! return if !@oauth_token.expired? @oauth_token = @oauth_token.refresh_token end
# File lib/unsplash/connection.rb, line 100 def request(verb, path, params = {}) raise ArgumentError.new "Invalid http verb #{verb}" if ![:get, :post, :put, :delete].include?(verb) params.merge!(utm_params) if !Unsplash.configuration.utm_source url = "https://community.unsplash.com/developersblog/unsplash-api-guidelines" Unsplash.configuration.logger.warn "utm_source is required as part of API Terms: #{url}" end headers = { "Accept-Version" => @api_version # Anything else? User agent? } response = 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 end if response.headers["Warning"] Unsplash.configuration.logger.warn response.headers["Warning"] end status_code = response.respond_to?(:status) ? response.status : response.code if !(200..299).include?(status_code) body = JSON.parse(response.body) msg = body["error"] || body["errors"].join(" ") raise Unsplash::Error.new msg end response end
# File lib/unsplash/connection.rb, line 143 def utm_params { utm_source: Unsplash.configuration.utm_source || "api_app", utm_medium: "referral", utm_campaign: "api-credit" } end