Class: TableauServerClient::Client

Inherits:
Object
  • Object
show all
Includes:
RequestBuilder
Defined in:
lib/tableau_server_client/client.rb

Defined Under Namespace

Classes: EmptyEncoder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestBuilder

#build_request

Constructor Details

#initialize(server_url, username, password, site_name, api_version, token_lifetime, logger) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
# File 'lib/tableau_server_client/client.rb', line 17

def initialize(server_url, username, password, site_name, api_version, token_lifetime, logger)
  @server_url = server_url
  @username = username
  @password = password
  @site_name = site_name
  @api_version = api_version
  @token_lifetime = token_lifetime
  @logger = logger
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#site_nameObject (readonly)

Returns the value of attribute site_name.



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

def site_name
  @site_name
end

#token_lifetimeObject (readonly)

Returns the value of attribute token_lifetime.



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

def token_lifetime
  @token_lifetime
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#create(resource, path: nil, request: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/tableau_server_client/client.rb', line 49

def create(resource, path: nil, request: nil)
  path = path || resource.path
  request = request || resource.to_request
  response = session.post do |req|
    req.url request_url(path).to_s
    req.body = request
  end
  Nokogiri::XML(response.body).xpath("//xmlns:tsResponse").children.first
end

#delete(resource) ⇒ Object



85
86
87
# File 'lib/tableau_server_client/client.rb', line 85

def delete(resource)
  session.delete request_url(resource.path).to_s
end

#download(resource_location) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tableau_server_client/client.rb', line 59

def download(resource_location)
  req_url = request_url("#{resource_location.path}/content", resource_location.query_params)
  response = session.get req_url.to_s
  type, disposition = response.headers.values_at('content-type', 'content-disposition')
  case type
  when 'application/xml'
    return Nokogiri::XML(response.body)
  when 'application/octet-stream'
    Zip::InputStream.open(StringIO.new(response.body)) do |io|
      while entry = io.get_next_entry
        return Nokogiri::XML(io.read) if entry.name =~ /.*\.(tds|twb)/
      end
      raise "TDS or TWB file not found for: #{resource_location.path}"
    end
  else
    raise "Unknown content-type: #{type}"
  end
end

#get(resource_location) ⇒ Object



42
43
44
45
46
47
# File 'lib/tableau_server_client/client.rb', line 42

def get(resource_location)
  req_url = request_url(resource_location.path)
  response = session.get req_url.to_s
  xml =  Nokogiri::XML(response.body).xpath("//xmlns:tsResponse").children.first
  resource_location.klass.from_response(self, resource_location.path, xml)
end

#get_collection(resource_location, &block) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/tableau_server_client/client.rb', line 33

def get_collection(resource_location, &block)
  return self.to_enum(:get_collection, resource_location) unless block
  req_url = request_url(resource_location.path, resource_location.query_params)
  response = session.get req_url.to_s
  TableauServerClient::PaginatableResponse.new(self, req_url, response).each_body do |b|
    resource_location.klass.from_collection_response(self, resource_location.path, b) {|r| yield r }
  end
end

#server_urlObject



29
30
31
# File 'lib/tableau_server_client/client.rb', line 29

def server_url
  @_server_url ||= URI(@server_url.chomp("/"))
end

#sessionObject



89
90
91
92
# File 'lib/tableau_server_client/client.rb', line 89

def session
  faraday.headers['X-Tableau-Auth'] = token.to_s
  faraday
end

#tokenObject



94
95
96
97
98
99
# File 'lib/tableau_server_client/client.rb', line 94

def token
  unless @token and @token.valid?
    @token = 
  end
  @token
end

#update(resource) ⇒ Object



78
79
80
81
82
83
# File 'lib/tableau_server_client/client.rb', line 78

def update(resource)
  session.put do |req|
    req.url request_url(resource.path).to_s
    req.body = resource.to_request
  end
end