Class: Saper::Items::JSON
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Saper::Item
#==, [], exists?, inherited, #serialize, subclasses, try, #type, type
Constructor Details
#initialize(hash) ⇒ JSON
Returns a new instance of JSON.
26
27
28
|
# File 'lib/saper/items/json.rb', line 26
def initialize(hash)
@hash = hash
end
|
Class Method Details
.new(item, bypass = false) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/saper/items/json.rb', line 5
def self.new(item, bypass = false)
super case item
when Text
parse(item.to_s)
when String
parse(item)
when Hash
bypass ? item : raise(InvalidItem, item)
else
raise(InvalidItem, item)
end
end
|
.parse(string) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/saper/items/json.rb', line 18
def self.parse(string)
begin
::JSON.parse(string)
rescue
raise(InvalidItem, string)
end
end
|
Instance Method Details
#find(xpath) ⇒ Object
30
31
32
|
# File 'lib/saper/items/json.rb', line 30
def find(xpath)
find_all(xpath).first
end
|
#find_all(xpath) ⇒ Object
34
35
36
|
# File 'lib/saper/items/json.rb', line 34
def find_all(xpath)
sanitize JSONSearch.find(@hash, xpath)
end
|
#to_native ⇒ Object
42
43
44
|
# File 'lib/saper/items/json.rb', line 42
def to_native
to_s
end
|
#to_s ⇒ Object
38
39
40
|
# File 'lib/saper/items/json.rb', line 38
def to_s
::JSON.dump(@hash)
end
|