Skip to content

Commit

Permalink
Merge pull request #34 from Tout/PLAT-3334
Browse files Browse the repository at this point in the history
[PLAT-3334] Allow client to receive logger on initialization. Make de…
  • Loading branch information
rottgoth authored May 25, 2018
2 parents abd9437 + 0466f67 commit 56396f3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
trubl (2.0.2)
trubl (2.0.3)
activesupport
faraday (~> 0.12.2)
hashie (~> 3.5.6)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/trubl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 9 additions & 17 deletions lib/trubl/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -150,7 +142,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)")
Trubl.logger.info { "Trubl::Client multipart post-ing #{uri.to_s} (content omitted)" }

Faraday.new(url: uri) do |faraday|
faraday.headers = options
Expand All @@ -159,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]/
Trubl.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
Expand All @@ -178,7 +170,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}")
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')
Expand All @@ -192,11 +184,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}")
Trubl.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}")
Trubl.logger.fatal { "Trubl::Client #{response.code} #{method}-ing #{uri.to_s} #{response.parsed_response}" }
else
Trubl.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
Expand All @@ -213,7 +205,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}")
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)
Expand All @@ -239,7 +231,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
Expand Down Expand Up @@ -291,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)'
Trubl.logger.warn("Unexposed HTTP #{code}: #{body}") if is_problematic
Trubl.logger.warn { "Unexposed HTTP #{code}: #{body}" } if is_problematic
return is_problematic
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/trubl/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 56396f3

Please sign in to comment.