Class: TableauApi::Resources::Workbooks

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

Constant Summary collapse

CAPABILITIES =

rubocop:enable Metrics/ParameterLists

%w(
  AddComment ChangeHierarchy ChangePermissions Delete ExportData ExportImage ExportXml
  Filter Read ShareView ViewComments ViewUnderlyingData WebAuthoring Write
).freeze

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

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

Instance Method Details

#find(workbook_id) ⇒ Object



91
92
93
94
# File 'lib/tableau_api/resources/workbooks.rb', line 91

def find(workbook_id)
  res = @client.connection.api_get("sites/#{@client.auth.site_id}/workbooks/#{workbook_id}")
  res['tsResponse']['workbook'] if res.code == 200
end

#listObject



101
102
103
104
# File 'lib/tableau_api/resources/workbooks.rb', line 101

def list
  url = "sites/#{@client.auth.site_id}/users/#{@client.auth.user_id}/workbooks"
  @client.connection.api_get_collection(url, 'workbooks.workbook')
end

#permissions(workbook_id:, user_id:, capabilities:) ⇒ Object

capabilities is a hash of symbol keys to booleans { Read: true, ChangePermissions: false }



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

def permissions(workbook_id:, user_id:, capabilities:)
  request = Builder::XmlMarkup.new.tsRequest do |ts|
    ts.permissions do |p|
      p.workbook(id: workbook_id)
      p.granteeCapabilities do |gc|
        gc.user(id: user_id)
        gc.capabilities do |c|
          capabilities.each do |k, v|
            k = k.to_s
            raise "invalid capability #{k}" unless CAPABILITIES.include? k
            c.capability(name: k, mode: v ? 'Allow' : 'Deny')
          end
        end
      end
    end
  end

  res = @client.connection.api_put("sites/#{@client.auth.site_id}/workbooks/#{workbook_id}/permissions", body: request)

  res.code == 200
end

#publish(name:, project_id:, file:, overwrite: false, show_tabs: false, connection_username: nil, connection_password: nil, connection_embed: false) ⇒ Object

rubocop:disable Metrics/ParameterLists

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tableau_api/resources/workbooks.rb', line 20

def publish(name:, project_id:, file:, overwrite: false, show_tabs: false, connection_username: nil, connection_password: nil, connection_embed: false)
  request = Builder::XmlMarkup.new.tsRequest do |ts|
    ts.workbook(name: name, showTabs: show_tabs) do |wb|
      wb.project(id: project_id)
      wb.connectionCredentials(name: connection_username, password: connection_password, embed: connection_embed) if connection_username
    end
  end

  query = URI.encode_www_form([['overwrite', overwrite]])
  path = "sites/#{@client.auth.site_id}/workbooks?#{query}"

  parts = {
    'request_payload' => request,
    'tableau_workbook' => UploadIO.new(file, 'application/octet-stream')
  }

  headers = {
    parts: {
      'request_payload' => { 'Content-Type' => 'text/xml' },
      'tableau_workbook' => { 'Content-Type' => 'application/octet-string' }
    }
  }

  res = @client.connection.api_post_multipart(path, parts, headers)

  return HTTParty::Parser.new(res.body, :xml).parse['tsResponse']['workbook'] if res.code == '201'

  raise TableauError, res
end

#update(workbook_id:, owner_user_id:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tableau_api/resources/workbooks.rb', line 79

def update(workbook_id:, owner_user_id:)
  request = Builder::XmlMarkup.new.tsRequest do |ts|
    ts.workbook(id: workbook_id) do |w|
      w.owner(id: owner_user_id)
    end
  end

  res = @client.connection.api_put("sites/#{@client.auth.site_id}/workbooks/#{workbook_id}", body: request)

  res.code == 200
end

#version(file) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tableau_api/resources/workbooks.rb', line 6

def version(file)
  version = nil

  if File.exist?(file) && File.extname(file) == '.twbx'
    Zip::File.open(file) do |zip_file|
      entry = zip_file.glob('*.twb').first
      version = HTTParty::Parser.new(entry.get_input_stream.read, :xml).parse['workbook']['version']
    end
  end

  version
end

#views(workbook_id) ⇒ Object



96
97
98
99
# File 'lib/tableau_api/resources/workbooks.rb', line 96

def views(workbook_id)
  url = "sites/#{@client.auth.site_id}/workbooks/#{workbook_id}/views"
  @client.connection.api_get_collection(url, 'views.view')
end