Class: Tipalti::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/tipalti-ruby/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, access_token: nil) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
# File 'lib/tipalti-ruby/connection.rb', line 9

def initialize(url:, access_token: nil)
  @access_token = access_token
  @url          = url
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/tipalti-ruby/connection.rb', line 7

def access_token
  @access_token
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/tipalti-ruby/connection.rb', line 7

def url
  @url
end

Instance Method Details

#delete(path, **params) ⇒ Object



14
15
16
# File 'lib/tipalti-ruby/connection.rb', line 14

def delete(path, **params)
  request(:delete, path, params)
end

#get(path, **params) ⇒ Object



18
19
20
# File 'lib/tipalti-ruby/connection.rb', line 18

def get(path, **params)
  request(:get, path, params)
end

#head(path, **params) ⇒ Object



22
23
24
# File 'lib/tipalti-ruby/connection.rb', line 22

def head(path, **params)
  request(:head, path, params)
end

#post(path, params = {}, options = {}) ⇒ Object



26
27
28
# File 'lib/tipalti-ruby/connection.rb', line 26

def post(path, params = {}, options = {})
  request(:post, path, params, options)
end

#put(path, **params) ⇒ Object



30
31
32
# File 'lib/tipalti-ruby/connection.rb', line 30

def put(path, **params)
  request(:put, path, params)
end

#request(method, path, params, options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tipalti-ruby/connection.rb', line 34

def request(method, path, params, options = {}) # rubocop:disable Metrics/MethodLength
  response = connection.public_send(method, path, params) do |request|
    request.headers["accept"] = "application/json"
    request.headers["authorization"] = "Bearer #{@access_token}" if @access_token

    if options[:body] == :form
      request.headers["Content-Type"] = "application/x-www-form-urlencoded"
      request.body = URI.encode_www_form(params)
    end
  end

  error = Error.from_response(response)

  raise error if error

  response.body
end