Module: Bullhorn::Rest::Authentication

Included in:
Client
Defined in:
lib/bullhorn/rest/authentication.rb

Overview

Instance Method Summary collapse

Instance Method Details

#auth_connObject



7
8
9
# File 'lib/bullhorn/rest/authentication.rb', line 7

def auth_conn
  @auth_conn ||= Faraday.new
end

#authenticateObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bullhorn/rest/authentication.rb', line 58

def authenticate
  unless rest_token
    unless access_token
      unless auth_code
        authorize
      end
      retrieve_tokens
    end
    
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/bullhorn/rest/authentication.rb', line 71

def authenticated?
  # TODO: check expires
  !!rest_token
end

#authorizeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bullhorn/rest/authentication.rb', line 11

def authorize
  url = "https://auth.bullhornstaffing.com/oauth/authorize"
  params = {
    client_id: client_id,
    username: username,
    password: password,
    action: 'Login',
    response_type: 'code'
  }
  res = auth_conn.get url, params
  location = res.headers['location']

  @auth_code = CGI::parse(URI(location).query)["code"].first
end

#loginObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bullhorn/rest/authentication.rb', line 44

def 
  url = "https://rest.bullhornstaffing.com/rest-services/login"
  params = {
    version: '*',
    access_token: access_token
  }
  response = auth_conn.get url, params
  hash = JSON.parse(response.body)

  @rest_token = hash['BhRestToken']
  @rest_url = hash['restUrl']
end

#retrieve_tokensObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bullhorn/rest/authentication.rb', line 27

def retrieve_tokens
  url = "https://auth.bullhornstaffing.com/oauth/token"
  params = {
    grant_type: 'authorization_code',
    code: auth_code,
    client_id: client_id,
    client_secret: client_secret
  }
  res = auth_conn.post url, params
  hash = JSON.parse(res.body)

  @access_token = hash['access_token']
  @access_token_expires_in = hash['expires_in']
  @refresh_token = hash['refresh_token']
end