Class: TableauServerClient::Resources::Resource

Inherits:
Object
  • Object
show all
Includes:
TableauServerClient::RequestBuilder
Defined in:
lib/tableau_server_client/resources/resource.rb

Defined Under Namespace

Classes: Attribute, Location

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TableauServerClient::RequestBuilder

#build_request

Constructor Details

#initialize(client, path, attributes) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
# File 'lib/tableau_server_client/resources/resource.rb', line 7

def initialize(client, path, attributes)
  @client = client
  @path = path
  attributes.each {|k,v| instance_variable_set("@#{k}",v) }
end

Class Method Details

.attr_reader(*vars) ⇒ Object



13
14
15
16
17
# File 'lib/tableau_server_client/resources/resource.rb', line 13

def self.attr_reader(*vars)
    @attributes ||= []
    @attributes.concat (vars.map { |v| Attribute.new(v.to_s) })
    super(*vars)
end

.attributesObject



19
20
21
# File 'lib/tableau_server_client/resources/resource.rb', line 19

def self.attributes
  @attributes
end

.extract_attributes(xml) ⇒ Object



36
37
38
39
40
41
# File 'lib/tableau_server_client/resources/resource.rb', line 36

def self.extract_attributes(xml)
  unless xml.name == resource_name
    raise "Element name (#{xml.name}) does not match with resource name (#{resource_name})"
  end
  attributes.select {|a| xml.key?(a.camelCase) }.map {|a| [a.to_sym, xml[a.camelCase]] }.to_h
end

.extract_site_path(path) ⇒ Object



43
44
45
46
# File 'lib/tableau_server_client/resources/resource.rb', line 43

def self.extract_site_path(path)
  p = path.split('/')
  p.slice(p.index('sites'),2).join('/')
end

.location(prefix, id = nil, filter: []) ⇒ Object



31
32
33
34
# File 'lib/tableau_server_client/resources/resource.rb', line 31

def self.location(prefix, id=nil, filter: [])
  path = [prefix, plural_resource_name, id].compact.join("/")
  Location.new(self, path, filter.empty? ? {} : {filter: filter.join(',')})
end

.plural_resource_nameObject



27
28
29
# File 'lib/tableau_server_client/resources/resource.rb', line 27

def self.plural_resource_name
  "#{self.resource_name}s"
end

.resource_nameObject



23
24
25
# File 'lib/tableau_server_client/resources/resource.rb', line 23

def self.resource_name
  self.name.split("::").last.sub(/./){ $&.downcase }
end

Instance Method Details

#attributesObject



48
49
50
# File 'lib/tableau_server_client/resources/resource.rb', line 48

def attributes
  self.class.attributes
end

#delete!Object



72
73
74
# File 'lib/tableau_server_client/resources/resource.rb', line 72

def delete!
  @client.delete self
end

#location(query_params: {}) ⇒ Object



56
57
58
# File 'lib/tableau_server_client/resources/resource.rb', line 56

def location(query_params: {})
  Location.new(self, path, query_params)
end

#pathObject



52
53
54
# File 'lib/tableau_server_client/resources/resource.rb', line 52

def path
  @path
end

#server_urlObject



68
69
70
# File 'lib/tableau_server_client/resources/resource.rb', line 68

def server_url
  @client.server_url
end

#site_idObject



64
65
66
# File 'lib/tableau_server_client/resources/resource.rb', line 64

def site_id
  site_path.split('/')[1]
end

#site_pathObject



60
61
62
# File 'lib/tableau_server_client/resources/resource.rb', line 60

def site_path
  self.class.extract_site_path(path)
end