Module: Kobana::Resources::Connection::ClassMethods

Defined in:
lib/kobana/resources/connection.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



116
117
118
119
120
121
# File 'lib/kobana/resources/connection.rb', line 116

def base_url
  config = client&.configuration || Kobana.configuration
  # Prioritize client configuration api_version over class api_version
  version = config.api_version&.to_sym || api_version&.to_sym
  BASE_URI[version][config.environment&.to_sym]
end

#build_multipart_connection(config) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kobana/resources/connection.rb', line 70

def build_multipart_connection(config)
  Faraday.new(url: base_url) do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.adapter :net_http
    faraday.headers = headers.merge(
      "Content-Type" => "multipart/form-data"
    )
    apply_timeouts(faraday, config)
    faraday.response :logger, logger if config.debug
  end
end

#connectionObject



32
33
34
35
36
37
38
# File 'lib/kobana/resources/connection.rb', line 32

def connection
  config = client&.configuration || Kobana.configuration
  # Don't cache connection when debugging to ensure logger works
  return build_connection(config) if config.debug

  @connection ||= build_connection(config)
end

#headersObject



24
25
26
27
28
29
30
# File 'lib/kobana/resources/connection.rb', line 24

def headers
  config = client&.configuration || Kobana.configuration
  {
    "Authorization" => "Bearer #{config.api_token}",
    "Content-Type" => "application/json"
  }.merge(config.custom_headers)
end

#loggerObject



83
84
85
86
87
88
89
90
# File 'lib/kobana/resources/connection.rb', line 83

def logger
  logger = Logger.new($stdout)
  logger.formatter = proc do |severity, datetime, _progname, msg|
    redacted_msg = msg.gsub(/(Bearer|Token)\s+[A-Za-z0-9\-_.]+/, '\1 [REDACTED]')
    "#{severity} #{datetime}: #{redacted_msg}\n"
  end
  logger
end

#multipart_connectionObject



62
63
64
65
66
67
68
# File 'lib/kobana/resources/connection.rb', line 62

def multipart_connection
  config = client&.configuration || Kobana.configuration
  # Don't cache connection when debugging
  return build_multipart_connection(config) if config.debug

  @multipart_connection ||= build_multipart_connection(config)
end

#parse_response(response) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/kobana/resources/connection.rb', line 102

def parse_response(response)
  body_parsed = begin
    JSON.parse(response.body, symbolize_names: true)
  rescue JSON::ParserError
    response.body
  end

  if body_parsed.is_a?(String) || body_parsed.is_a?(Array) || !body_parsed.key?(:data)
    { status: response.status, data: body_parsed }
  else
    body_parsed
  end
end

#request(method, url, params_or_body = nil, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/kobana/resources/connection.rb', line 92

def request(method, url, params_or_body = nil, options = {})
  self.errors = []
  response = if options[:multipart]
               multipart_connection.send(method, url, params_or_body)
             else
               connection.send(method, url, params_or_body)
             end
  parse_response(response)
end