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, uri = '') ⇒ JsonPage

Returns a new instance of JsonPage.



32
33
34
35
36
37
# File 'lib/zold/json_page.rb', line 32

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

Instance Method Details

#to_hashObject



39
40
41
42
43
44
# File 'lib/zold/json_page.rb', line 39

def to_hash
  raise '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}"
end