lib/supergood/client.rb in supergood-0.1.1 vs lib/supergood/client.rb in supergood-0.1.2
- old
+ new
@@ -10,45 +10,44 @@
Dotenv.load
module Supergood
DEFAULT_SUPERGOOD_BASE_URL = 'https://dashboard.supergood.ai'
-
class << self
- def init(supergood_client_id=nil, supergood_client_secret=nil, base_url=nil)
- supergood_client_id = supergood_client_id || ENV['SUPERGOOD_CLIENT_ID']
- supergood_client_secret = supergood_client_secret || ENV['SUPERGOOD_CLIENT_SECRET']
+ def init(config={})
+ supergood_client_id = config[:client_id] || ENV['SUPERGOOD_CLIENT_ID']
+ supergood_client_secret = config[:client_secret] || ENV['SUPERGOOD_CLIENT_SECRET']
if !supergood_client_id
raise SupergoodException.new ERRORS[:NO_CLIENT_ID]
end
if !supergood_client_secret
raise SupergoodException.new ERRORS[:NO_CLIENT_SECRET]
end
- @base_url = base_url || ENV['SUPERGOOD_BASE_URL'] || DEFAULT_SUPERGOOD_BASE_URL
- header_options = {
- 'Content-Type' => 'application/json',
- 'Authorization' => 'Basic ' + Base64.encode64(supergood_client_id + ':' + supergood_client_secret).gsub(/\n/, '')
- }
+ @base_url = ENV['SUPERGOOD_BASE_URL'] || DEFAULT_SUPERGOOD_BASE_URL
+ @api = Supergood::Api.new(supergood_client_id, supergood_client_secret, @base_url)
+ @config = Supergood::Utils.make_config(config)
- @api = Supergood::Api.new(header_options, @base_url)
- @config = @api.fetch_config
@ignored_domains = @config[:ignoredDomains]
@keys_to_hash = @config[:keysToHash]
- @logger = Supergood::Logger.new(@api, @config, header_options)
+ @logger = Supergood::Logger.new(@api, @config, @api.header_options)
- @api.set_error_sink_endpoint(@config[:errorSinkEndpoint])
- @api.set_event_sink_endpoint(@config[:eventSinkEndpoint])
@api.set_logger(@logger)
@request_cache = {}
@response_cache = {}
@interval_thread = set_interval(@config[:flushInterval]) { flush_cache }
- log.debug("Using config %s" % @config.inspect)
+
+ @http_clients = [
+ Supergood::Vendor::NetHTTP,
+ Supergood::Vendor::HTTPrb
+ ]
+
+ patch_all()
self
end
def log
@logger
@@ -74,40 +73,50 @@
begin
api.post_events(data)
rescue => e
log.error(data, e, e.message)
+ cleanup()
ensure
@response_cache.clear
@request_cache.clear if force
end
end
+ def cleanup()
+ @interval_thread.kill
+ unpatch_all()
+ end
+
def close(force = true)
log.debug('Cleaning up, flushing cache gracefully.')
- @interval_thread.kill
flush_cache(force)
+ cleanup()
end
+ def patch_all
+ @http_clients.each do |client|
+ client.patch
+ end
+ end
+
+ def unpatch_all
+ @http_clients.each do |client|
+ client.unpatch
+ end
+ end
+
def set_interval(delay)
Thread.new do
loop do
sleep delay / 1000.0
yield # call passed block
end
end
end
- def self.intercept(*args, &block)
- instance.intercept(*args, &block)
- end
-
- def self.instance
- @instance ||= Supergood.new
- end
-
def intercept(request)
request_id = SecureRandom.uuid
requested_at = Time.now
if !ignored?(request[:domain])
cache_request(request_id, requested_at, request)
@@ -136,10 +145,11 @@
@request_cache[request_id] = {
request: request_payload
}
rescue => e
log.error({ request: request }, e, ERRORS[:CACHING_REQUEST])
+ cleanup()
end
end
def cache_response(request_id, requested_at, response)
begin
@@ -157,14 +167,14 @@
@response_cache[request_id] = Supergood::Utils.hash_values_from_keys(request_payload.merge({
response: response_payload
}), @keys_to_hash)
@request_cache.delete(request_id)
rescue => e
- puts e
log.error(
{ request: request_payload, response: response_payload },
e, ERRORS[:CACHING_RESPONSE]
)
+ cleanup()
end
end
def ignored?(domain)
base_domain = URI.parse(@base_url).hostname