Class: Tableau::Workbook

Inherits:
Object
  • Object
show all
Includes:
Util::Permissions
Defined in:
lib/tableau_ruby/workbook.rb

Instance Method Summary collapse

Methods included from Util::Permissions

#build_permissions_for_group, #build_permissions_for_user

Constructor Details

#initialize(client) ⇒ Workbook


8
9
10
# File 'lib/tableau_ruby/workbook.rb', line 8

def initialize(client)
  @client = client
end

Instance Method Details

#add_permissions_for_group(params) ⇒ Object


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/tableau_ruby/workbook.rb', line 189

def add_permissions_for_group(params)
  # params => {
  #   :workbook_id => 'cf68994e-29c8-4f41-bf6c-398e3666131e' },
  #   :user_id => 'cf68994e-29c8-4f41-bf6c-398e3666131e'}
  #   :permissions => [
  #     :allow =>  ['Read', 'ExportImage', 'ExportData'],
  #     :deny => ['ViewComments', 'AddComment', 'Filter', 'Connect']
  #   ]
  # }
  return { error: "workbook id is missing." } unless params[:workbook_id]
  return { error: "site id is missing." } unless @client.site_id
  return { error: "group id is missing."} unless params[:group_id]

  site_id = @client.site_id

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.tsRequest do
      xml.permissions do
        xml.workbook(id: params[:workbook_id])
        build_permissions_for_group(xml, params)
      end
    end
  end

  resp = @client.conn.put "/api/2.0/sites/#{site_id}/workbooks/#{params[:workbook_id]}/permissions" 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)
end

#add_permissions_for_user(params) ⇒ Object


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/tableau_ruby/workbook.rb', line 155

def add_permissions_for_user(params)
  # params => {
  #   :workbook_id => 'cf68994e-29c8-4f41-bf6c-398e3666131e' },
  #   :user_id => 'cf68994e-29c8-4f41-bf6c-398e3666131e'}
  #   :permissions => [
  #     :allow =>  ['Read', 'ExportImage', 'ExportData'],
  #     :deny => ['ViewComments', 'AddComment', 'Filter', 'Connect']
  #   ]
  # }
  return { error: "workbook id is missing." } unless params[:workbook_id]
  return { error: "site id is missing." } unless @client.site_id
  return { error: "user id is missing."} unless params[:user_id]

  site_id = @client.site_id

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.tsRequest do
      xml.permissions do
        xml.workbook(id: params[:workbook_id])
        build_permissions_for_user(xml, params)
      end
    end
  end

  resp = @client.conn.put "/api/2.0/sites/#{site_id}/workbooks/#{params[:workbook_id]}/permissions" 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)
end

#all(params = {}) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tableau_ruby/workbook.rb', line 75

def all(params={})
  return { error: "user_id is missing." } if params[:user_id].nil? || params[:user_id].empty?

  resp = @client.conn.get "/api/2.0/sites/#{@client.site_id}/users/#{params[:user_id]}/workbooks?pageSize=1000" do |req|
    req.params['getThumbnails'] = params[:include_images] if params[:include_images]
    req.params['isOwner'] = params[:is_owner] || false
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end

  data = {workbooks: [], pagination: {}}
  doc = Nokogiri::XML(resp.body)

  doc.css("pagination").each do |p|
    data[:pagination][:page_number] = p['pageNumber']
    data[:pagination][:page_size] = p['pageSize']
    data[:pagination][:total_available] = p['totalAvailable']
  end

  doc.css("workbook").each do |w|
    workbook = {id: w["id"], name: w["name"]}

    if params[:include_images]
      resp = @client.conn.get("/api/2.0/sites/#{@client.site_id}/workbooks/#{w['id']}/previewImage") do |req|
        req.headers['X-Tableau-Auth'] = @client.token if @client.token
      end
      workbook[:image] = Base64.encode64(resp.body)
      workbook[:image_mime_type] = "image/png"
    end

    w.css('project').each do |p|
      workbook[:project] = {id: p['id'], name: p['name']}
    end

    w.css("tag").each do |t|
      (workbook[:tags] ||=[]) << t['id']
    end

    if params[:include_views]
      workbook[:views] = include_views(site_id: @client.site_id, id: w['id'])
    end

    data[:workbooks] << workbook
  end
  data
end

#create(params = {}) ⇒ Object


12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tableau_ruby/workbook.rb', line 12

def create(params = {})
  params[:site_id] ||= @client.site_id
  db_user = params[:db_user]
  db_pass = params[:db_pass]

  raise "Missing workbook file!" unless params[:file_path] || 
      (params[:file] && params[:file][:name] && params[:file][:data])
  raise "Missing project id" unless params[:project_id]

  if params[:file]
    filename = params[:file][:name]
    filedata = params[:file][:data]
  else
    filename = params[:file_path].split("/").last 
    filedata = File.read(params[:file_path])
  end

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.tsRequest do
      xml.workbook(name: filename.gsub(".twb", "")) do
        xml.connectionCredentials(name: db_user, password: db_pass) if !db_user.nil? && !db_pass.nil?
        xml.project(id: params[:project_id])
      end
    end
  end

  payload = builder.doc.root.to_xml#(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML)
  multipart_body = "--boundary-string\nContent-Disposition: name=\"request_payload\"\nContent-Type: text/xml\n\n\#{payload}\n--boundary-string\nContent-Disposition: name=\"tableau_workbook\"; filename=\"\#{filename}\"\nContent-Type: application/octet-stream\n\n\#{filedata}\n--boundary-string--\n"

  multipart_body.gsub!("\n","\r\n")

  resp = @client.conn.post("/api/2.0/sites/#{params[:site_id]}/workbooks") do |req|
    req.options.timeout = 300 # open/read timeout sould be high...if your workbook talks to datasources, the http request can time out before its done
    req.options.open_timeout = 300
    req.params["overwrite"] = true
    req.headers["Content-Type"] = "multipart/mixed; boundary=\"boundary-string\""
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
    req.body = multipart_body
  end
 
  raise resp.body if resp.status > 299
  Nokogiri::XML(resp.body).css("workbook").collect do |w|
    { id: w["id"], name: w["name"], content_url: w['contentUrl'], 
      owner: { id: w.css('owner').first['id']},
      views: w.css('views view').collect{|v| 
        {id: v['id'], name: v['name'], content_url: v['contentUrl']}
      }
    }
  end.first
end

#find(params) ⇒ Object


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tableau_ruby/workbook.rb', line 121

def find(params)
  resp = @client.conn.get "/api/2.0/sites/#{params[:site_id]}/workbooks/#{params[:workbook_id]}" do |req|
    req.params['previewImage'] = params[:preview_images] if params[:preview_images]
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end

  data = {}
  Nokogiri::XML(resp.body).css("workbook").each do |w|

    wkbk = {id: w["id"], name: w["name"], description: w['description']}

    if params[:include_views]
      wkbk[:views] = include_views(site_id: params[:site_id], id: params[:workbook_id])
    end

    data = wkbk
  end

  data
end

#preview_image(workbook) ⇒ Object

TODO: Refactor this is duplicate in all method. Also, there are many, many places that are begging to be DRYer.


143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tableau_ruby/workbook.rb', line 143

def preview_image(workbook)
  resp = @client.conn.get("/api/2.0/sites/#{params[:site_id]}/workbooks/#{workbook[:id]}/previewImage") do |req|
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end

  data = {}
  data[:image] = Base64.encode64(resp.body)
  data[:image_mime_type] = "image/png"

  data
end