class TicketAbstractorClient::Base::Client

Constants

TA_RESPONSE_TIME_HEADERS_LIST

Attributes

communications_stack[R]
trace_communications[R]

Public Class Methods

new() click to toggle source
# File lib/ticket_abstractor_client/base/client.rb, line 10
def initialize
  @base_url = TicketAbstractorClient.configuration.ticket_abstractor_url
  @security_token = TicketAbstractorClient.configuration.security_token
  @ssl_options = TicketAbstractorClient.configuration.ssl_options
  @communications_stack = []
  @trace_communications = TicketAbstractorClient.configuration.trace_communications

  raise Errors::ConfigurationError, 'TicketAbstractor url is not given' if @base_url.blank?
  raise Errors::ConfigurationError, 'SecurityToken is not given' if @security_token.blank?
end

Protected Instance Methods

build_request(path, method, params) click to toggle source
# File lib/ticket_abstractor_client/base/client.rb, line 73
def build_request(path, method, params)
  request_params = @ssl_options.merge(url: "#{@base_url}/v2/#{path}", method: method)
  request_params[:headers] = { params: params } if method.inquiry.get?
  request_params[:payload] = params if method.inquiry.post?

  RestClient::Request.new(request_params)
end
communication_data(request, response, executed_at) click to toggle source
# File lib/ticket_abstractor_client/base/client.rb, line 53
def communication_data(request, response, executed_at)
  CommunicationData.new.tap do |communication|
    communication.executed_at = executed_at

    communication.method = request.method
    communication.path = request.uri.path
    communication.args = request.args.dig(:payload, :args) || request.args.dig(:headers, :params, :args)

    if response.headers.key?(:file_request)
      communication.binary_response!
    else
      communication.response = response.body
    end

    TA_RESPONSE_TIME_HEADERS_LIST.each do |key|
      communication.public_send("#{key}=", response.headers[key])
    end
  end
end
get(path, args = {}, params = {}) click to toggle source
# File lib/ticket_abstractor_client/base/client.rb, line 23
def get(path, args = {}, params = {})
  params.merge! args: args.to_json, security_token: @security_token
  with_response_handling do
    rest_client_error_handling do
      response = make_request(path, 'get', params)
      response.headers.key?(:file_request) ? { 'result' => response } : JSON.parse(response)
    end
  end
end
make_request(path, method, params) click to toggle source
# File lib/ticket_abstractor_client/base/client.rb, line 43
def make_request(path, method, params)
  request = build_request(path, method, params)
  executed_at = Time.now
  response = request.execute

  @communications_stack << communication_data(request, response, executed_at) if self.trace_communications

  response
end
post(path, args = {}, params = {}) click to toggle source
# File lib/ticket_abstractor_client/base/client.rb, line 33
def post(path, args = {}, params = {})
  params.merge! args: args.to_json, security_token: @security_token
  with_response_handling do
    rest_client_error_handling do
      response = make_request(path, 'post', params)
      JSON.parse response
    end
  end
end
rest_client_error_handling() { || ... } click to toggle source
# File lib/ticket_abstractor_client/base/client.rb, line 81
def rest_client_error_handling
  yield
rescue Errno::ECONNREFUSED => exception
  raise Errors::UnexpectedError, exception.message
rescue RestClient::Exception => exception
  response_body = exception.response.body rescue exception.message
  error_message = JSON.parse(response_body)['error'] rescue response_body
  raise Errors::UnexpectedError, error_message
end