Skip to content

Commit

Permalink
Make logging shut up
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahzs committed Feb 28, 2018
1 parent 9c95059 commit d48061d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/se/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ class Client
attr_reader :quota, :quota_used
attr_accessor :params

def initialize(key = "", **params)
def initialize(key = "", log_api_raw: false, log_api_json: false, log_meta: true, **params)
@key = key
@params = params.merge({filter: '!*1_).BnZb8pdvWlZpJYNyauMekouxK9-RzUNUrwiB'})
@quota = nil
@quota_used = 0
@backoff = Time.now
@logger_raw = Logger.new 'api_raw.log'
@logger_json = Logger.new 'api_json.log'
@logger = Logger.new 'se-api.log'
@logger_raw.level = Logger::Severity::UNKNOWN unless log_api_raw
@logger_json.level = Logger::Severity::UNKNOWN unless log_api_json
@logger.level = Logger::Severity::UNKNOWN unless log_meta
end

def posts(*ids, **params)
Expand Down Expand Up @@ -85,15 +89,16 @@ def json(uri, **params)
backoff_for = @backoff-Time.now
backoff_for = 0 if backoff_for <= 0
if backoff_for > 0
puts "Backing off for #{backoff_for}"
@logger.warn "Backing off for #{backoff_for}"
sleep backoff_for+2
puts "Finished backing off!"
@logger.warn "Finished backing off!"
end
params = @params.merge(params).merge({key: @key}).map { |k,v| "#{k}=#{v}" }.join('&')
puts "Posting to https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params}"
@logger.info "Posting to https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params}"
begin
resp_raw = Net::HTTP.get_response(URI("https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params}")).body
rescue Net::OpenTimeout, SocketError => e
@logger.warn "Got timeout on API request (#{e}). Retrying..."
puts "Got timeout on API request (#{e}). Retrying..."
sleep 0.3
retry
Expand Down

0 comments on commit d48061d

Please sign in to comment.