Class: Zold::JsonPage

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/json_page.rb

Overview

JSON page

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ JsonPage

Returns a new instance of JsonPage.



30
31
32
33
34
# File 'lib/zold/json_page.rb', line 30

def initialize(text)
  raise 'JSON text can\'t be nil' if text.nil?
  raise 'JSON must be of type String' unless text.is_a?(String)
  @text = text
end

Instance Method Details

#to_hashObject



36
37
38
39
40
41
# File 'lib/zold/json_page.rb', line 36

def to_hash
  raise 'JSON is empty, can\'t parse' if @text.empty?
  JSON.parse(@text)
rescue JSON::ParserError => e
  raise "Failed to parse JSON (#{e.message}): #{@text}"
end