Class: Nibbler::JsonDocument
- Inherits:
-
Object
- Object
- Nibbler::JsonDocument
- Defined in:
- lib/nibbler/json.rb
Overview
a wrapper for JSON data that provides ‘at` and `search`
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #at(selector) ⇒ Object
-
#initialize(obj, root = nil) ⇒ JsonDocument
constructor
A new instance of JsonDocument.
- #root ⇒ Object
- #search(selector) ⇒ Object
Constructor Details
#initialize(obj, root = nil) ⇒ JsonDocument
Returns a new instance of JsonDocument.
8 9 10 11 |
# File 'lib/nibbler/json.rb', line 8 def initialize(obj, root = nil) @data = obj.respond_to?(:to_str) ? JSON.parse(obj) : obj @root = root end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/nibbler/json.rb', line 6 def data @data end |
Instance Method Details
#at(selector) ⇒ Object
28 29 30 |
# File 'lib/nibbler/json.rb', line 28 def at(selector) search(selector).first end |
#root ⇒ Object
13 14 15 |
# File 'lib/nibbler/json.rb', line 13 def root @root || data end |
#search(selector) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/nibbler/json.rb', line 17 def search(selector) if selector !~ /[^\w-]/ found = Array === data ? data : data[selector] found = [] if found.nil? found = [found] unless Array === found else found = scan_selector selector end found end |