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.



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

def initialize(client)
  @client = client
end

Instance Method Details

#api_delete(path, *args) ⇒ Object



59
60
61
# File 'lib/tableau_api/connection.rb', line 59

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

#api_get(path, *args) ⇒ Object



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

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

#api_get_collection(path, collection, *args) ⇒ 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



18
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 18

def api_get_collection(path, collection, *args)
  Enumerator.new do |enum|
    args[0] = {} unless args[0]
    page_size = (args[0].delete(:page_size) { 100 }).to_i
    page_number = (args[0].delete(:page_number) { 1 }).to_i

    loop do
      uri = URI::HTTP.build(path: "/#{path}", query: URI.encode_www_form(**args[0], pageSize: page_size, pageNumber: page_number)).request_uri

      res = api_get(uri, *args)
      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, *args) ⇒ Object



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

def api_post(path, *args)
  args[0][:headers] = {} unless args[0][:headers]
  args[0][:headers]['Content-Type'] = 'application/xml'
  api_method(:post, path, *args)
end

#api_post_multipart(path, parts, headers) ⇒ Object



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

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, *args) ⇒ Object



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

def api_put(path, *args)
  args[0][:headers] = {} unless args[0][:headers]
  args[0][:headers]['Content-Type'] = 'application/xml'
  api_method(:put, path, *args)
end

#post(path, *args) ⇒ Object



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

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