Class: Saper::Items::JSON

Inherits:
Saper::Item show all
Defined in:
lib/saper/items/json.rb

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

TODO:

Returns a new instance of 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

TODO:

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(Errors::InvalidItem, item)
  else
    raise(Errors::InvalidItem, item)
  end
end

.parse(string) ⇒ Object

TODO:

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(Errors::InvalidItem, string)
  end
end

Instance Method Details

#==(other) ⇒ Object

TODO:

34
35
36
# File 'lib/saper/items/json.rb', line 34

def ==(other)
  @hash == other || to_s == other.to_s
end

#find(xpath) ⇒ Object

TODO:

39
40
41
# File 'lib/saper/items/json.rb', line 39

def find(xpath)
  find_all(xpath).first
end

#find_all(xpath) ⇒ Object

TODO:

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

TODO:

54
55
56
# File 'lib/saper/items/json.rb', line 54

def to_json(*a)
  a.empty? ? self : @hash
end

#to_nativeObject

TODO:

59
60
61
# File 'lib/saper/items/json.rb', line 59

def to_native
  to_s
end

#to_sObject

TODO:

49
50
51
# File 'lib/saper/items/json.rb', line 49

def to_s
  ::JSON.dump(@hash)
end