class TableauRestApi::Client

Client class wrapping a subset of the Tableau Rest API This class just contains worker methods for interacting with Tableau. See the Request sub-class for resource requests.

Attributes

config[R]
token[R]

Public Class Methods

new() click to toggle source
# File lib/tableau_rest_api/client.rb, line 10
def initialize
  @config = Config.new.options
end

Public Instance Methods

authorised?() click to toggle source
# File lib/tableau_rest_api/client.rb, line 31
def authorised?
  token = self.token
  token && !token.expired? ? true : false
end
configure(config) click to toggle source
# File lib/tableau_rest_api/client.rb, line 14
def configure(config)
  @config = Config.new(config).options
end
login(*_args) click to toggle source
# File lib/tableau_rest_api/client.rb, line 18
def login(*_args)
  return if authorised?
  url = build_url ['auth', 'signin']
  resp = post url, @config[:credentials]
  @token = Token.new(resp.credentials.token, @config[:auth_duration])
end
logout() click to toggle source
# File lib/tableau_rest_api/client.rb, line 25
def logout
  post(build_url ['auth', 'signout'])
  @token = nil
  !authorised?
end

Private Instance Methods

build_url(endpoint, page=nil) click to toggle source
# File lib/tableau_rest_api/client.rb, line 44
def build_url(endpoint, page=nil)
  endpoint = endpoint.is_a?(Array) ? endpoint.join('/') : endpoint
  url = "#{@config[:host]}/#{@config[:base]}/#{@config[:api_version]}/#{endpoint}"
  url = page ? url + "?pageNumber=#{page}" : url
end
delete(url) click to toggle source
# File lib/tableau_rest_api/client.rb, line 64
def delete(url)
  Response.new(RestClient.delete(url, header)).parse
end
fetch_paginated_set(endpoint, extract) click to toggle source
# File lib/tableau_rest_api/client.rb, line 68
def fetch_paginated_set(endpoint, extract)
  response = get build_url(endpoint)
  collection = extract.call(response)
  collection = retrieve_additional_pages(response, collection, endpoint, extract) 
end
get(url) click to toggle source
# File lib/tableau_rest_api/client.rb, line 50
def get(url)
  Response.new(RestClient.get(url, header)).parse
end
header(boundary=nil) click to toggle source
# File lib/tableau_rest_api/client.rb, line 38
def header(boundary=nil)
  head = { :accept => :json, :content_type => :json }
  head = authorised? ? head.merge({ :x_tableau_auth => self.token.value }) : head
  head = boundary ? head.merge({ :content_type => "multipart/mixed; boundary=#{boundary}"}) : head
end
post(url, data={}, boundary=nil) click to toggle source
# File lib/tableau_rest_api/client.rb, line 54
def post(url, data={}, boundary=nil)
  data = data.to_json unless boundary
  headers = header boundary
  Response.new(RestClient.post(url, data, headers)).parse
end
put(url, data={}) click to toggle source
# File lib/tableau_rest_api/client.rb, line 60
def put(url, data={})
  Response.new(RestClient.put(url, data.to_json, header)).parse
end