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.2"

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.



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

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, token = {}) ⇒ 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
18
# File 'lib/auth_tool.rb', line 13

def self.get_client(client_secrets, token =  {})
  raise "Expected Hash, received #{client_secrets.class}" if client_secrets.class != Hash
  client = create_client(client_secrets) if token == {}
  client = create_client(client_secrets, token) if token != {}
  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.



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

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.



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

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



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

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:



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

def self.refresh client
  client.refresh
end