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
29
30
31
|
# File 'lib/saper/items/json.rb', line 29
def initialize(hash)
@hash = hash
end
|
Class Method Details
.new(item, bypass = false) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/saper/items/json.rb', line 6
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
20
21
22
23
24
25
26
|
# File 'lib/saper/items/json.rb', line 20
def self.parse(string)
begin
::JSON.parse(string)
rescue
raise(InvalidItem, string)
end
end
|
Instance Method Details
#==(other) ⇒ Object
34
35
36
|
# File 'lib/saper/items/json.rb', line 34
def ==(other)
@hash == other
end
|
#find(xpath) ⇒ Object
39
40
41
|
# File 'lib/saper/items/json.rb', line 39
def find(xpath)
find_all(xpath).first
end
|
#find_all(xpath) ⇒ Object
44
45
46
|
# File 'lib/saper/items/json.rb', line 44
def find_all(xpath)
sanitize JPath.find(@hash, xpath)
end
|
#to_json(*a) ⇒ Object
54
55
56
|
# File 'lib/saper/items/json.rb', line 54
def to_json(*a)
a.empty? ? self : @hash
end
|
#to_native ⇒ Object
59
60
61
|
# File 'lib/saper/items/json.rb', line 59
def to_native
to_s
end
|
#to_s ⇒ Object
49
50
51
|
# File 'lib/saper/items/json.rb', line 49
def to_s
::JSON.dump(@hash)
end
|