Skip to content

Commit

Permalink
allow fallback for 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
sethherr committed Nov 21, 2016
1 parent 999cd30 commit 650bc3e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
20 changes: 17 additions & 3 deletions lib/papertrail/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
require 'papertrail'
require 'papertrail/connection'
require 'papertrail/cli_helpers'
require 'papertrail/okjson'
if RUBY_VERSION < '1.9'
# Ruby 1.8 doesn't have json in the standard lib - so we have to use okjson
require 'papertrail/okjson'
else
require 'json'
end

module Papertrail
class Cli
Expand Down Expand Up @@ -147,7 +152,7 @@ def query_time_range

connection.each_event(@query, query_options.merge(:min_time => min_time, :max_time => max_time)) do |event|
if options[:json]
$stdout.puts Papertrail::OkJson.encode(event.data)
output_raw_json(event.data)
else
display_result(event)
end
Expand Down Expand Up @@ -184,7 +189,7 @@ def display_result(event)

def display_results(results)
if options[:json]
$stdout.puts Papertrail::OkJson.encode(results.data)
output_raw_json(results.data)
else
results.events.each do |event|
display_result(event)
Expand All @@ -194,6 +199,15 @@ def display_results(results)
$stdout.flush
end

def output_raw_json(hash)
if RUBY_VERSION < '1.9'
# Ruby 1.8 doesn't have json in the standard lib - so we have to use okjson, which is slow
$stdout.puts Papertrail::OkJson.encode(hash)
else
$stdout.puts JSON.generate(hash)
end
end

def usage
<<-EOF
Expand Down
15 changes: 7 additions & 8 deletions lib/papertrail/http_client.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
require 'delegate'
require 'net/https'
require 'json'
if RUBY_VERSION < '1.9'
# Ruby 1.8 doesn't have json in the standard lib - so we have to use okjson
require 'papertrail/okjson'
else
require 'json'
end

module Papertrail

# Used because Net::HTTPOK in Ruby 1.8 has no body= method
class HttpResponse < SimpleDelegator

def initialize(response)
if RUBY_VERSION < '1.9'
# Ruby 1.8 doesn't have json in the standard lib - so we have to use okjson
require 'papertrail/okjson'
else
require 'json'
end
super(response)
end

def body
if RUBY_VERSION < '1.9'
# This is really slow. Avoid it if possible
# This is really slow. Avoid it if possible (by upgrading ruby)
if __getobj__.body.respond_to?(:force_encoding)
@body ||= Papertrail::OkJson.decode(__getobj__.body.dup.force_encoding('UTF-8'))
else
Expand Down

0 comments on commit 650bc3e

Please sign in to comment.