From acadd2001dd1d398fe44813c5c19ae3b257c1092 Mon Sep 17 00:00:00 2001 From: Rob Cataneo Date: Thu, 24 May 2018 15:18:00 -0700 Subject: [PATCH 1/3] [PLAT-3334] Allow client to receive logger on initialization. Make default logger's level warn instead of info --- lib/trubl.rb | 8 +++++--- lib/trubl/client.rb | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/trubl.rb b/lib/trubl.rb index 2087695..e529994 100644 --- a/lib/trubl.rb +++ b/lib/trubl.rb @@ -20,9 +20,11 @@ def client(client_id='', client_secret='', callback_url='', access_token=nil) @client end - def logger(level=Logger::INFO) - @logger = Logger.new(STDOUT) unless defined?(@logger) and @logger.level != level - @logger.level = level + def logger(level=Logger::WARN) + unless @logger + @logger = Logger.new(STDOUT) + @logger.level = level + end @logger end diff --git a/lib/trubl/client.rb b/lib/trubl/client.rb index f9aaf42..edc619c 100644 --- a/lib/trubl/client.rb +++ b/lib/trubl/client.rb @@ -44,7 +44,7 @@ class << self attr_accessor :cache_store end - attr_reader :client_id, :client_secret, :access_token, :callback_url, :auth_response + attr_reader :client_id, :client_secret, :access_token, :callback_url, :auth_response, :logger # Initialize a new Tout client with creds and callback url def initialize(client_id=nil, client_secret=nil, callback_url=nil, *args) @@ -66,6 +66,7 @@ def initialize(client_id=nil, client_secret=nil, callback_url=nil, *args) @token_url = opts[:token_url] @email = opts[:email] @password = opts[:password] + @logger = opts[:logger] || Trubl.logger end def default_tout_configuration @@ -150,7 +151,7 @@ def multipart_post(path, params={}) uri = full_uri(path) payload = { 'tout[data]' => Faraday::UploadIO.new(params[:data], 'video/mp4')}.merge(params) - Trubl.logger.info("Trubl::Client multipart post-ing #{uri.to_s} (content omitted)") + self.logger.info { "Trubl::Client multipart post-ing #{uri.to_s} (content omitted)" } Faraday.new(url: uri) do |faraday| faraday.headers = options @@ -159,7 +160,7 @@ def multipart_post(path, params={}) faraday.adapter Faraday.default_adapter end.post(uri.to_s, payload).tap do |response| if !response.status =~ /20[0-9]/ - Trubl.logger.fatal("Trubl::Client multipart post-ing #{uri.to_s} #{response.code} #{response.parsed_response}") + self.logger.fatal { "Trubl::Client multipart post-ing #{uri.to_s} #{response.code} #{response.parsed_response}" } end end end @@ -178,7 +179,7 @@ def request(method, path, params = {}) body = params.delete(:body) || {} params = params[:query] if params.has_key?(:query) - Trubl.logger.info("Trubl::Client #{method}-ing #{uri} with params #{params}") + self.logger.info { "Trubl::Client #{method}-ing #{uri} with params #{params}" } conn = Faraday.new(url: api_uri_root) do |faraday| faraday.adapter :net_http do |http| http.use_ssl = (@uri_port == 443 || @uri_scheme == 'https') @@ -192,11 +193,11 @@ def request(method, path, params = {}) # For backwards compatibility response.define_singleton_method :code, -> { self.status } if response.respond_to?(:status) - Trubl.logger.info("Trubl::Client response: #{response.inspect}") + self.logger.debug { "Trubl::Client response: #{response.inspect}" } if !response.code =~ /20[0-9]/ - Trubl.logger.fatal("Trubl::Client #{response.code} #{method}-ing #{uri.to_s} #{response.parsed_response}") + self.logger.fatal { "Trubl::Client #{response.code} #{method}-ing #{uri.to_s} #{response.parsed_response}" } else - Trubl.logger.debug("Trubl::Client #{uri} response: #{response.body}") + self.logger.debug { "Trubl::Client #{uri} response: #{response.body}" } end response.body.force_encoding("utf-8") if response.body and response.body.respond_to?(:force_encoding) response @@ -213,7 +214,7 @@ def multi_request(method, requests=[], opts={}) opts.reverse_merge! max_concurrency: 10 - Trubl.logger.info("Trubl::Client multi-#{method}-ing #{requests.join(', ')} with headers #{headers}") + self.logger.info { "Trubl::Client multi-#{method}-ing #{requests.join(', ')} with headers #{headers}" } self.send(:multi_request_threaded, method, requests, opts).collect do |response| response.body.force_encoding("utf-8") if response.body and response.body.respond_to?(:force_encoding) @@ -239,7 +240,7 @@ def multi_request_threaded(method, requests=[], opts={}) end def set_logger(level) - Trubl.logger(level) + logger.warn { "This method is deprecated. Don't use it anymore." } end private @@ -291,7 +292,7 @@ def self.is_problematic_response?(response) is_problematic = code && (400..600).include?(code) body = is_problematic && response.respond_to?(:body) ? response.body : '(no body)' - Trubl.logger.warn("Unexposed HTTP #{code}: #{body}") if is_problematic + self.logger.warn { "Unexposed HTTP #{code}: #{body}" } if is_problematic return is_problematic end end From 486f7ee7c50b099e1213818c6fe5a93535fa8582 Mon Sep 17 00:00:00 2001 From: Rob Cataneo Date: Thu, 24 May 2018 17:09:28 -0700 Subject: [PATCH 2/3] [PLAT-3334] Revert back to using Trubl.logger as the top level instance of logger to be used. Removed typheous references not needed anymore --- Gemfile.lock | 12 ++++++------ lib/trubl/client.rb | 27 +++++++++------------------ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b4e686d..b08ad6c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - trubl (2.0.2) + trubl (2.0.3) activesupport faraday (~> 0.12.2) hashie (~> 3.5.6) @@ -15,9 +15,9 @@ PATH GEM remote: https://rubygems.org/ specs: - activesupport (5.1.5) + activesupport (5.2.0) concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (~> 0.7) + i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) addressable (2.5.2) @@ -38,7 +38,7 @@ GEM multipart-post httparty (0.15.7) multi_xml (>= 0.5.2) - i18n (0.9.5) + i18n (1.0.1) concurrent-ruby (~> 1.0) json (1.8.6) jwt (1.5.6) @@ -58,7 +58,7 @@ GEM coderay (~> 1.1.0) method_source (~> 0.9.0) public_suffix (3.0.0) - rack (2.0.4) + rack (2.0.5) rake (12.3.1) rspec (3.6.0) rspec-core (~> 3.6.0) @@ -91,7 +91,7 @@ GEM addressable (>= 2.3.6) crack (>= 0.3.2) hashdiff - yajl-ruby (1.3.1) + yajl-ruby (1.4.0) PLATFORMS ruby diff --git a/lib/trubl/client.rb b/lib/trubl/client.rb index edc619c..3731578 100644 --- a/lib/trubl/client.rb +++ b/lib/trubl/client.rb @@ -15,14 +15,6 @@ require 'active_support' require 'active_support/core_ext' -begin - if RUBY_ENGINE == 'ruby' - require 'typhoeus' - require 'typhoeus/adapters/faraday' - end -rescue LoadError -end - # instantiate a Tout client instance module Trubl # Wrapper for the Tout REST API @@ -44,7 +36,7 @@ class << self attr_accessor :cache_store end - attr_reader :client_id, :client_secret, :access_token, :callback_url, :auth_response, :logger + attr_reader :client_id, :client_secret, :access_token, :callback_url, :auth_response # Initialize a new Tout client with creds and callback url def initialize(client_id=nil, client_secret=nil, callback_url=nil, *args) @@ -66,7 +58,6 @@ def initialize(client_id=nil, client_secret=nil, callback_url=nil, *args) @token_url = opts[:token_url] @email = opts[:email] @password = opts[:password] - @logger = opts[:logger] || Trubl.logger end def default_tout_configuration @@ -151,7 +142,7 @@ def multipart_post(path, params={}) uri = full_uri(path) payload = { 'tout[data]' => Faraday::UploadIO.new(params[:data], 'video/mp4')}.merge(params) - self.logger.info { "Trubl::Client multipart post-ing #{uri.to_s} (content omitted)" } + Trubl.logger.info { "Trubl::Client multipart post-ing #{uri.to_s} (content omitted)" } Faraday.new(url: uri) do |faraday| faraday.headers = options @@ -160,7 +151,7 @@ def multipart_post(path, params={}) faraday.adapter Faraday.default_adapter end.post(uri.to_s, payload).tap do |response| if !response.status =~ /20[0-9]/ - self.logger.fatal { "Trubl::Client multipart post-ing #{uri.to_s} #{response.code} #{response.parsed_response}" } + Trubl.logger.fatal { "Trubl::Client multipart post-ing #{uri.to_s} #{response.code} #{response.parsed_response}" } end end end @@ -179,7 +170,7 @@ def request(method, path, params = {}) body = params.delete(:body) || {} params = params[:query] if params.has_key?(:query) - self.logger.info { "Trubl::Client #{method}-ing #{uri} with params #{params}" } + Trubl.logger.info { "Trubl::Client #{method}-ing #{uri} with params #{params}" } conn = Faraday.new(url: api_uri_root) do |faraday| faraday.adapter :net_http do |http| http.use_ssl = (@uri_port == 443 || @uri_scheme == 'https') @@ -193,11 +184,11 @@ def request(method, path, params = {}) # For backwards compatibility response.define_singleton_method :code, -> { self.status } if response.respond_to?(:status) - self.logger.debug { "Trubl::Client response: #{response.inspect}" } + Trubl.logger.debug { "Trubl::Client response: #{response.inspect}" } if !response.code =~ /20[0-9]/ - self.logger.fatal { "Trubl::Client #{response.code} #{method}-ing #{uri.to_s} #{response.parsed_response}" } + Trubl.logger.fatal { "Trubl::Client #{response.code} #{method}-ing #{uri.to_s} #{response.parsed_response}" } else - self.logger.debug { "Trubl::Client #{uri} response: #{response.body}" } + Trubl.logger.debug { "Trubl::Client #{uri} response: #{response.body}" } end response.body.force_encoding("utf-8") if response.body and response.body.respond_to?(:force_encoding) response @@ -214,7 +205,7 @@ def multi_request(method, requests=[], opts={}) opts.reverse_merge! max_concurrency: 10 - self.logger.info { "Trubl::Client multi-#{method}-ing #{requests.join(', ')} with headers #{headers}" } + Trubl.logger.info { "Trubl::Client multi-#{method}-ing #{requests.join(', ')} with headers #{headers}" } self.send(:multi_request_threaded, method, requests, opts).collect do |response| response.body.force_encoding("utf-8") if response.body and response.body.respond_to?(:force_encoding) @@ -292,7 +283,7 @@ def self.is_problematic_response?(response) is_problematic = code && (400..600).include?(code) body = is_problematic && response.respond_to?(:body) ? response.body : '(no body)' - self.logger.warn { "Unexposed HTTP #{code}: #{body}" } if is_problematic + Trubl.logger.warn { "Unexposed HTTP #{code}: #{body}" } if is_problematic return is_problematic end end From 0466f679abeb4367dad8ace60821349128768a95 Mon Sep 17 00:00:00 2001 From: Rob Cataneo Date: Fri, 25 May 2018 15:15:32 -0700 Subject: [PATCH 3/3] [PLAT-3334] Bumped to version 2.0.4 --- lib/trubl/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/trubl/version.rb b/lib/trubl/version.rb index b9db3dc..6146303 100644 --- a/lib/trubl/version.rb +++ b/lib/trubl/version.rb @@ -2,7 +2,7 @@ module Trubl class Version MAJOR = 2 unless defined? Trubl::Version::MAJOR MINOR = 0 unless defined? Trubl::Version::MINOR - PATCH = 3 unless defined? Trubl::Version::PATCH + PATCH = 4 unless defined? Trubl::Version::PATCH class << self