Class: SerialSpec::ParsedBody
- Inherits:
-
Object
- Object
- SerialSpec::ParsedBody
- Defined in:
- lib/serial_spec/parsed_body.rb
Instance Attribute Summary collapse
-
#raw_body ⇒ Object
readonly
Returns the value of attribute raw_body.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Class Method Summary collapse
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #execute ⇒ Object
- #first(*args) ⇒ Object
-
#initialize(body) ⇒ ParsedBody
constructor
A new instance of ParsedBody.
- #last(*args) ⇒ Object
Constructor Details
#initialize(body) ⇒ ParsedBody
Returns a new instance of ParsedBody.
11 12 13 14 |
# File 'lib/serial_spec/parsed_body.rb', line 11 def initialize(body) @selector = [] @raw_body = JSON.parse(body) end |
Instance Attribute Details
#raw_body ⇒ Object (readonly)
Returns the value of attribute raw_body.
8 9 10 |
# File 'lib/serial_spec/parsed_body.rb', line 8 def raw_body @raw_body end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
9 10 11 |
# File 'lib/serial_spec/parsed_body.rb', line 9 def selector @selector end |
Class Method Details
.add_selector(*methuds) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/serial_spec/parsed_body.rb', line 16 def self.add_selector(*methuds) methuds.each do |methud| class_eval " def \#{methud.to_s}(*args)\n selector.push([\#{methud.to_sym}, args])\n self\n end\n METHOD\n end\nend\n" |
Instance Method Details
#[](*args) ⇒ Object
29 30 31 32 |
# File 'lib/serial_spec/parsed_body.rb', line 29 def [](*args) selector.push([:[], *args]) self end |
#execute ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/serial_spec/parsed_body.rb', line 44 def execute copy = selector.clone selector.clear copy.inject(raw_body) do |remainder, method_and_args| begin methud, *args = method_and_args if remainder.kind_of?(Hash) remainder.with_indifferent_access.send methud, *args else remainder.send methud, *args end rescue NoMethodError => ex raise ArgumentError, "could not find '#{methud.inspect}' \nfor:\n '#{remainder.inspect}' \nin\n #{raw_body}" end end end |
#first(*args) ⇒ Object
34 35 36 37 |
# File 'lib/serial_spec/parsed_body.rb', line 34 def first(*args) selector.push([:first]) self end |
#last(*args) ⇒ Object
39 40 41 42 |
# File 'lib/serial_spec/parsed_body.rb', line 39 def last(*args) selector.push([:last]) self end |