Class: TableauApi::Connection

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

Constant Summary collapse

API_VERSION =
'3.1'

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Connection

Returns a new instance of Connection.



10
11
12
# File 'lib/tableau_api/connection.rb', line 10

def initialize(client)
  @client = client
end

Instance Method Details

#api_delete(path, **kwargs) ⇒ Object



61
62
63
# File 'lib/tableau_api/connection.rb', line 61

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

#api_get(path, **kwargs) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity



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

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 rubocop:disable Metrics/CyclomaticComplexity



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

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



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

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tableau_api/connection.rb', line 65

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



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

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



14
15
16
# File 'lib/tableau_api/connection.rb', line 14

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