Class: Tableau::Project

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

Instance Method Summary collapse

Methods included from Util::Permissions

#build_permissions_for_group, #build_permissions_for_user

Constructor Details

#initialize(client) ⇒ Project

Returns a new instance of Project.


5
6
7
# File 'lib/tableau_ruby/project.rb', line 5

def initialize(client)
  @client = client
end

Instance Method Details

#add_permissions_for_group(params) ⇒ Object


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
120
121
122
123
124
125
# File 'lib/tableau_ruby/project.rb', line 90

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

  # get the group id
  params[:group_id] = @client.groups.all_users_group[: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.project(id: params[:project_id])
        build_permissions_for_group(xml, params)
      end
    end
  end

  resp = @client.conn.put "/api/2.0/sites/#{site_id}/projects/#{params[:project_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


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/tableau_ruby/project.rb', line 127

def add_permissions_for_user(params)
  # params => {
  #   :project_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: "project id is missing." } unless params[:project_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.project(id: params[:project_id])
        build_permissions_for_user(xml, params)
      end
    end
  end

  resp = @client.conn.put "/api/2.0/sites/#{site_id}/projects/#{params[:project_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

#allObject


9
10
11
12
13
14
15
16
17
18
# File 'lib/tableau_ruby/project.rb', line 9

def all
  resp = @client.conn.get "/api/2.0/sites/#{@client.site_id}/projects" do |req|
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end
  projects = {projects: []}
  Nokogiri::XML(resp.body).css("tsResponse projects project").each do |s|
    projects[:projects] << {id: s["id"], name: s["name"]}
  end
  projects
end

#create(project) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tableau_ruby/project.rb', line 26

def create(project)
  site_id = project[:site_id] || @client.site_id
  return { error: "name is missing." } unless project[:name]

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.tsRequest do
      xml.project(
        name: project[:name],
        description: project[:description]
      )
    end
  end

  resp = @client.conn.post "/api/2.0/sites/#{site_id}/projects" 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 project").each do |s|
    return s["id"]
  end
end

#delete(project) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tableau_ruby/project.rb', line 76

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

  return { error: "project id is missing." } unless project[:id]

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

  raise resp.body if resp.status > 299

  true
end

#find_by(params) ⇒ Object


20
21
22
23
24
# File 'lib/tableau_ruby/project.rb', line 20

def find_by(params)
  site_id = params[:site_id] || @client.site_id

  all[:projects].select{|proj| proj[:name] == params[:name] }.first
end

#update(project) ⇒ Object


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/project.rb', line 51

def update(project)
  return { error: "site_id is missing." } unless project[:site_id]
  return { error: "name is missing." } unless project[:name]

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.tsRequest do
      xml.project(
        name: project[:name],
        description: project[:description]
      )
    end
  end

  resp = @client.conn.put "/api/2.0/sites/#{project[:site_id]}/projects/#{project[:project_id]}" do |req|
    req.body = builder.to_xml
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end
  if resp.status == 200
    {project: resp.body}
  else
    {error: resp.status}
  end
end