lib/zold/json_page.rb in zold-0.19.2 vs lib/zold/json_page.rb in zold-0.20.0
- old
+ new
@@ -27,21 +27,29 @@
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
# License:: MIT
module Zold
# JSON page
class JsonPage
+ # When can't parse the JSON page.
+ class CantParse < StandardError; end
+
def initialize(text, uri = '')
raise 'JSON text can\'t be nil' if text.nil?
raise 'JSON must be of type String' unless text.is_a?(String)
@text = text
@uri = uri
end
def to_hash
- raise 'JSON is empty, can\'t parse' + (@uri.empty? ? '' : " at #{@uri}") if @text.empty?
+ raise CantParse, 'JSON is empty, can\'t parse' + (@uri.empty? ? '' : " at #{@uri}") if @text.empty?
JSON.parse(@text)
rescue JSON::ParserError => e
- raise "Failed to parse JSON #{@uri.empty? ? '' : "at #{@uri}"} (#{e.message}): \
-#{@text.inspect.gsub(/^.{200,}$/, '\1...')}"
+ raise CantParse, "Failed to parse JSON #{@uri.empty? ? '' : "at #{@uri}"} (#{short(e.message)}): #{short(@text)}"
+ end
+
+ private
+
+ def short(txt)
+ txt.gsub(/^.{128,}$/, '\1...').inspect
end
end
end