Class: Tableau::Users

Inherits:
Object
  • Object
show all
Defined in:
lib/tableau_ruby/users.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Users


6
7
8
# File 'lib/tableau_ruby/users.rb', line 6

def initialize(client)
  @client = client
end

Instance Attribute Details

#workbooksObject (readonly)

Returns the value of attribute workbooks


4
5
6
# File 'lib/tableau_ruby/users.rb', line 4

def workbooks
  @workbooks
end

Instance Method Details

#all(params = {}) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tableau_ruby/users.rb', line 50

def all(params={})
  site_id = @client.site_id

  resp = @client.conn.get "/api/2.0/sites/#{site_id}/users?pageSize=#{params["page-size"]}" do |req|
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end

  data = {users: []}
  Nokogiri::XML(resp.body).css("tsResponse users user").each do |u|
    data[:users] << {
      id: u['id'],
      name: u['name'],
      site_id: site_id,
      role: u['role'],
      publish: u['publish'],
      content_admin: u['contentAdmin'],
      last_login: u['lastLogin'],
      external_auth_user_id: u['externalAuthUserId']
    }
  end
  data
end

#create(options) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tableau_ruby/users.rb', line 10

def create(options)
  site_id = options[:site_id] || @client.site_id

  return { error: "name is missing." } unless options[:name]

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.tsRequest do
      xml.user(
        name: options[:name],
        siteRole: "Interactor"
      )
    end
  end

  resp = @client.conn.post "/api/2.0/sites/#{site_id}/users" do |req|
    req.body = builder.to_xml
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end

  raise resp.body if resp.status > 299

  Nokogiri::XML(resp.body).css("tsResponse user").each do |s|
    return s["id"]
  end
end

#delete(options) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tableau_ruby/users.rb', line 36

def delete(options)
  site_id = options[:site_id] || @client.site_id

  return { error: "user_id is missing." } unless options[:user_id]

  resp = @client.conn.delete "/api/2.0/sites/#{site_id}/users/#{options[:user_id]}" do |req|
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end

  raise resp.body if resp.status > 299

  return resp.status
end

#find_by(params = {}) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tableau_ruby/users.rb', line 73

def find_by(params={})
  params.update({"page-size" => 1000})

  #BUG: if you have more than 1000 users, you wont find your users
  #needs pagination support
  all_users = all(params)[:users]

  if params[:id]
    return all_users.select {|u| u[:id] == params[:id] }.first
  elsif params[:name]
    return all_users.select {|u| u[:name] == params[:name] }.first
  else
    raise "You need :id or :name"
  end
end