Class: Zold::JsonPage

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

Overview

JSON page

Defined Under Namespace

Classes: CantParse

Instance Method Summary collapse

Constructor Details

#initialize(text, uri = '') ⇒ JsonPage

Returns a new instance of JsonPage.



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

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



42
43
44
45
46
47
# File 'lib/zold/json_page.rb', line 42

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