Class: Unsplash::Connection

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • version (String) (defaults to: DEFAULT_VERSION)

    The Unsplash API version to use.

  • api_base_uri (String) (defaults to: DEFAULT_API_BASE_URI)

    Base URI at which to make API calls.

  • oauth_base_uri (String) (defaults to: DEFAULT_OAUTH_BASE_URI)

    Base URI for OAuth requests.


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.

Parameters:

  • requested_scopes (Array) (defaults to: ["public"])

    An array of permission scopes being requested.

Returns:

  • (String)

    The authorization URL.


39
40
41
42
# File 'lib/unsplash/connection.rb', line 39

def authorization_url(requested_scopes = ["public"])
  @oauth.auth_code.authorize_url(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.

Parameters:

  • auth_code (String)

    The OAuth authentication code from Unsplash.


47
48
49
50
# File 'lib/unsplash/connection.rb', line 47

def authorize!(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.

Parameters:

  • token_extract (Hash)

    OAuth token hash from #extract_token.

Returns:

  • (OAuth2::AccessToken, nil)

    New access token object.


64
65
66
67
# File 'lib/unsplash/connection.rb', line 64

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.

Parameters:

  • path (String)

    The path at which to make ther request.

  • params (Hash) (defaults to: {})

    A hash of request parameters.


93
94
95
# File 'lib/unsplash/connection.rb', line 93

def delete(path, params = {})
  request :delete, path, params
end

#extract_tokenHash?

Extract hash with OAuth token attributes. Extracted token attributes can be used with create_and_assign_token to prevent the need for reauthorization.

Returns:

  • (Hash, nil)

    @oauth_token converted to a hash.


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.

Parameters:

  • path (String)

    The path at which to make ther request.

  • params (Hash) (defaults to: {})

    A hash of request parameters.


72
73
74
# File 'lib/unsplash/connection.rb', line 72

def get(path, params = {})
  request :get, path, params
end

#post(path, params = {}) ⇒ Object

Perform a POST request.

Parameters:

  • path (String)

    The path at which to make ther request.

  • params (Hash) (defaults to: {})

    A hash of request parameters.


86
87
88
# File 'lib/unsplash/connection.rb', line 86

def post(path, params = {})
  request :post, path, params
end

#put(path, params = {}) ⇒ Object

Perform a PUT request.

Parameters:

  • path (String)

    The path at which to make ther request.

  • params (Hash) (defaults to: {})

    A hash of request parameters.


79
80
81
# File 'lib/unsplash/connection.rb', line 79

def put(path, params = {})
  request :put, path, params
end

#utm_paramsObject


97
98
99
100
101
102
103
# File 'lib/unsplash/connection.rb', line 97

def utm_params
  {
    "utm_source"   => Unsplash.configuration.utm_source || "api_app",
    "utm_medium"   => "referral",
    "utm_campaign" => "api-credit"
  }
end