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.



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.

Parameters:

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

    An array of permission scopes being requested.

Returns:

  • (String)

    The authorization URL.



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

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.



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

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.



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.

Parameters:

  • path (String)

    The path at which to make ther request.

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

    A hash of request parameters.



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

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.



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.

Parameters:

  • path (String)

    The path at which to make ther request.

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

    A hash of request parameters.



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.

Parameters:

  • path (String)

    The path at which to make ther request.

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

    A hash of request parameters.



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.

Parameters:

  • path (String)

    The path at which to make ther request.

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

    A hash of request parameters.



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

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

#utm_paramsObject



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