Class: TableauApi::Resources::Auth

Inherits:
Base
  • Object
show all
Defined in:
lib/tableau_api/resources/auth.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from TableauApi::Resources::Base

Instance Method Details

#sign_inObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tableau_api/resources/auth.rb', line 19

def 
  return true if signed_in?

  request = Builder::XmlMarkup.new.tsRequest do |ts|
    ts.credentials(name: @client.username, password: @client.password) do |cred|
      cred.site(contentUrl: @client.site_name == 'Default' ? '' : @client.site_name)
    end
  end

  res = @client.connection.api_post('auth/signin', body: request)

  return false unless res.code == 200

  @token = res['tsResponse']['credentials']['token']
  @site_id = res['tsResponse']['credentials']['site']['id']
  @user_id = res['tsResponse']['credentials']['user']['id']

  true
end

#sign_outObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tableau_api/resources/auth.rb', line 43

def sign_out
  return true unless signed_in?

  res = @client.connection.api_post('auth/signout', body: nil)

  # consider 401 to be successful since signing out with an expired
  # token fails, but we can still consider the user signed out
  return false unless res.code == 204 || res.code == 401

  @token = nil
  @site_id = nil
  @user_id = nil

  true
end

#signed_in?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/tableau_api/resources/auth.rb', line 39

def signed_in?
  !@token.nil?
end

#site_idObject



9
10
11
12
# File 'lib/tableau_api/resources/auth.rb', line 9

def site_id
   unless signed_in?
  @site_id
end

#tokenObject



4
5
6
7
# File 'lib/tableau_api/resources/auth.rb', line 4

def token
   unless signed_in?
  @token
end

#trusted_ticketObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tableau_api/resources/auth.rb', line 59

def trusted_ticket
  body = {
    username: @client.username,
    target_site: @client.site_name == 'Default' ? '' : @client.site_name
  }

  begin
    res = @client.connection.post('trusted', body: body, limit: 1)
  rescue HTTParty::RedirectionTooDeep
    # redirects if the site_name is invalid
    res = false
  end

  return unless res && res.code == 200 && res.body != '-1'

  res.body
end

#user_idObject



14
15
16
17
# File 'lib/tableau_api/resources/auth.rb', line 14

def user_id
   unless signed_in?
  @user_id
end