Module: AuthTool

Defined in:
lib/auth_tool.rb,
lib/auth_tool/client.rb,
lib/auth_tool/oauth_1.rb,
lib/auth_tool/oauth_2.rb,
lib/auth_tool/version.rb

Defined Under Namespace

Modules: OAuth1, OAuth2 Classes: Client

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.call(client, http_verb, uri, params = {}) ⇒ Hash

Makes an authenticated call to the API resource.

Examples:

response = AuthTool.call(
  client, "get", "https://api.twitter.com/1.1/users/show.json",
    {:screen_name => "username"})

Parameters:

  • client (AuthTool::Client)

    The client containing the API information.

  • uri (String)

    The API endpoint to hit.

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

    Optional. Hash of additional parameters for the call.

Returns:

  • (Hash)

    The endpoint’s response.



83
84
85
86
# File 'lib/auth_tool.rb', line 83

def self.call(client, http_verb, uri, params = {})
  response = client.oauth_version == 1 ? AuthTool::OAuth1.call(client, http_verb, uri, params) : AuthTool::OAuth2.call(client, http_verb, uri, params)
  return response
end

.get_client(client_secrets) ⇒ AuthTool::Client

Creates a client object for the specified API.

Parameters:

  • client_secrets (String)

    The client_secrets hash for the API

Returns:



13
14
15
16
17
# File 'lib/auth_tool.rb', line 13

def self.get_client client_secrets
  raise "Expected Hash, received #{client_secrets.class}" if client_secrets.class != Hash
  client = create_client(client_secrets)
  return client
end

.get_redirect_url(client) ⇒ String

Returns the redirect url so the user can authenticate with the service.

Parameters:

Returns:

  • (String)

    The redirect url.



26
27
28
29
30
31
# File 'lib/auth_tool.rb', line 26

def self.get_redirect_url client
  params = client.params if client.has_params?
  params ||= {}
  redirect_url = client.oauth_version == 1 ? AuthTool::OAuth1.redirect_url(client, params) : AuthTool::OAuth2.redirect_url(client)
  return redirect_url
end

.get_token(client) ⇒ Hash

Returns the authentication token information of the client

Parameters:

Returns:

  • (Hash)

    The token hash for the client.



53
54
55
# File 'lib/auth_tool.rb', line 53

def self.get_token client
  return client.token
end

.receive(client, response) ⇒ Object

Handles service’s user authentication response delivered by the front-end. Sets the client’s access_token.

Parameters:

  • client (AuthTool::Client)

    The client containing the API information.

  • response (String)

    The service’s response to the callback url



42
43
44
# File 'lib/auth_tool.rb', line 42

def self.receive(client, response)
  client.oauth_version == 1 ? AuthTool::OAuth1.receive(client, response) : AuthTool::OAuth2.receive(client,response)
end

.refresh(client) ⇒ Object

Attempts to refresh the auth token for the client

Parameters:



61
62
63
# File 'lib/auth_tool.rb', line 61

def self.refresh client
  client.refresh
end