Class: TableauApi::Connection

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/tableau_api/connection.rb

Constant Summary collapse

API_VERSION =
'3.1'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Connection

Returns a new instance of Connection.



8
9
10
# File 'lib/tableau_api/connection.rb', line 8

def initialize(client)
  @client = client
end

Instance Method Details

#api_delete(path, **kwargs) ⇒ Object



57
58
59
# File 'lib/tableau_api/connection.rb', line 57

def api_delete(path, **kwargs)
  api_method(:delete, path, kwargs)
end

#api_get(path, **kwargs) ⇒ Object



43
44
45
# File 'lib/tableau_api/connection.rb', line 43

def api_get(path, **kwargs)
  api_method(:get, path, kwargs)
end

#api_get_collection(path, collection, page_number: 1, page_size: 100, **kwargs) ⇒ Object

if the result is paginated, it will fetch subsequent pages collection can be delimited with a period to do nested hash lookups e.g. objects.object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tableau_api/connection.rb', line 19

def api_get_collection(path, collection, page_number: 1, page_size: 100, **kwargs)
  Enumerator.new do |enum|
    loop do
      query = kwargs.fetch(:query, '')
      query += '&' unless query.empty?
      query += "pageSize=#{page_size}&pageNumber=#{page_number}"
      new_kwargs = kwargs.merge(query: query)

      res = api_get(path, **new_kwargs)
      raise TableauError, res if res.code.to_s != '200'

      # ensure the result is an array because it will not be an array if there is only one element
      [collection.split('.').reduce(res['tsResponse']) { |acc, elem| acc && acc[elem] }].flatten.compact.each do |obj|
        enum.yield obj
      end

      break if res['tsResponse']['pagination'].nil?
      break if page_number >= (res['tsResponse']['pagination']['totalAvailable'].to_i / page_size.to_f).ceil

      page_number += 1
    end
  end
end

#api_post(path, **kwargs) ⇒ Object



47
48
49
50
# File 'lib/tableau_api/connection.rb', line 47

def api_post(path, **kwargs)
  new_headers = kwargs.fetch(:headers, {}).merge('Content-Type' => 'application/xml')
  api_method(:post, path, kwargs.merge(headers: new_headers))
end

#api_post_multipart(path, parts, headers) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tableau_api/connection.rb', line 61

def api_post_multipart(path, parts, headers)
  headers = auth_headers(headers)
  headers['Content-Type'] = 'multipart/mixed'

  uri = URI.parse(url_for(path))

  req = Net::HTTP::Post::Multipart.new(uri.to_s, parts, headers)

  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(req)
  end
end

#api_put(path, **kwargs) ⇒ Object



52
53
54
55
# File 'lib/tableau_api/connection.rb', line 52

def api_put(path, **kwargs)
  new_headers = kwargs.fetch(:headers, {}).merge('Content-Type' => 'application/xml')
  api_method(:put, path, kwargs.merge(headers: new_headers))
end

#post(path, **kwargs) ⇒ Object



12
13
14
# File 'lib/tableau_api/connection.rb', line 12

def post(path, **kwargs)
  self.class.post("#{@client.host}/#{path}", kwargs)
end