Class: JsonStatham::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/json_statham/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Parser

Returns a new instance of Parser.



11
12
13
14
15
16
# File 'lib/json_statham/parser.rb', line 11

def initialize(name, &block)
  Validation.check_object_class(name, [String])

  @name  = name
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'lib/json_statham/parser.rb', line 5

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/json_statham/parser.rb', line 5

def name
  @name
end

#observerObject (readonly)

Returns the value of attribute observer.



5
6
7
# File 'lib/json_statham/parser.rb', line 5

def observer
  @observer
end

#readerObject (readonly)

Returns the value of attribute reader.



5
6
7
# File 'lib/json_statham/parser.rb', line 5

def reader
  @reader
end

Class Method Details

.call(name, &block) ⇒ Object



7
8
9
# File 'lib/json_statham/parser.rb', line 7

def self.call(name, &block)
  new(name, &block).call
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
# File 'lib/json_statham/parser.rb', line 18

def call
  @reader   = JsonStatham::Requests::Reader.call(self)
  @observer = JsonStatham::Requests::Observer.call(self)
  result    = JsonStatham::Result.call(self)

  store_current_schema

  result
end

#current_schemaObject



54
55
56
# File 'lib/json_statham/parser.rb', line 54

def current_schema
  JsonStatham::Schema.call(schema)
end

#previous_durationObject



44
45
46
47
48
# File 'lib/json_statham/parser.rb', line 44

def previous_duration
  return unless reader?

  reader["duration"]
end

#reader?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/json_statham/parser.rb', line 50

def reader?
  !!reader
end

#schemaObject



34
35
36
# File 'lib/json_statham/parser.rb', line 34

def schema
  observer&.data
end

#store_current_schemaObject



28
29
30
31
32
# File 'lib/json_statham/parser.rb', line 28

def store_current_schema
  return unless JsonStatham.config.store_schema?

  JsonStatham::Requests::Writer.call(self)
end

#stored_schemaObject



38
39
40
41
42
# File 'lib/json_statham/parser.rb', line 38

def stored_schema
  return unless reader?

  reader["schema"]
end

#stored_schema?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/json_statham/parser.rb', line 58

def stored_schema?
  !!stored_schema
end

#valid?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/json_statham/parser.rb', line 62

def valid?
  !stored_schema? || (stored_schema == current_schema)
end