Class: Tableau::Site

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Site


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

def initialize(client)
  @client = client
end

Instance Method Details

#all(params = {}) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tableau_ruby/site.rb', line 8

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

#create(site) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tableau_ruby/site.rb', line 40

def create(site)
  return { error: "site name is missing." } unless site[:name]
  site_hash = {}
  #camelize
  site.each{|key, value| site_hash[key.to_s.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }] = value}
  return { error: "site name is missing." } unless site[:name]

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.tsRequest do
      xml.site(site_hash)
    end
  end

  resp = @client.conn.post "/api/2.0/sites" do |req|
    req.body = builder.to_xml
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end
  if resp.status == 201
    normalize(Nokogiri::XML(resp.body).css("site").first)
  else
    {error: resp.status}
  end
end

#delete(site) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tableau_ruby/site.rb', line 95

def delete(site)
  return { error: "site id is missing." } unless site[:id]

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

  if resp.status == 204
    {success: 'Site successfully deleted.'}
  else
    {errors: resp.status}
  end
end

#find_by(params = {}) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tableau_ruby/site.rb', line 23

def find_by(params={})
  if params.include? :name && params[:name].nil? || params[:name].gsub(" ", "").empty?
    return { error: "site name is missing." }
  elsif params.include? :id && params[:id].nil? || params[:id].gsub(" ", "").empty?
    return { error: "site id is missing." }
  end
  key = params.keys - [:include_projects]
  term = params[key[0]]
  resp = @client.conn.get "/api/2.0/sites/#{term}" do |req|
    req.params['includeProjects'] = params[:include_projects] || false
    req.params["key"] = "name" if term == params[:name]
    req.params["key"] = "contentUrl" if term == params[:url]
    req.headers['X-Tableau-Auth'] = @client.token if @client.token
  end
  normalize(Nokogiri::XML(resp.body).css("site").first)
end

#update(site) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tableau_ruby/site.rb', line 64

def update(site)
  return { error: "site id is missing." } unless site[:id]

  case_dict = {
    name: "name",
    content_url: "contentUrl",
    admin_mode: "adminMode",
    user_quota: "userQuota",
    storage_quota: "storageQuota",
    disable_subscriptions: "disableSubscriptions"
  }

  site.each do |k,v|
    next if k == :id
    (@site ||= {}).store(case_dict[k],v)
  end

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.tsRequest do
      xml.site(@site)
    end
  end

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

  normalize(resp.body)
end