Class: Nibbler::JsonDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/nibbler/json.rb

Overview

a wrapper for JSON data that provides ‘at` and `search`

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ JsonDocument

Returns a new instance of JsonDocument.



7
8
9
# File 'lib/nibbler/json.rb', line 7

def initialize(obj)
  @data = String === obj ? JSON.parse(obj) : obj
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/nibbler/json.rb', line 5

def data
  @data
end

Class Method Details

.[](obj) ⇒ Object



11
12
13
# File 'lib/nibbler/json.rb', line 11

def self.[](obj)
  self.class === obj ? obj : new(obj)
end

Instance Method Details

#at(selector) ⇒ Object



19
20
21
# File 'lib/nibbler/json.rb', line 19

def at(selector)
  data[selector]
end

#search(selector) ⇒ Object



15
16
17
# File 'lib/nibbler/json.rb', line 15

def search(selector)
  Array === data ? data : Array(at(selector))
end