Class: VoiceBase::JSON

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/voicebase/json.rb

Defined Under Namespace

Classes: ParseError, Word

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word_array = nil) ⇒ JSON

Returns a new instance of JSON.

Raises:

  • (StandardError)


48
49
50
51
# File 'lib/voicebase/json.rb', line 48

def initialize(word_array = nil)
  raise StandardError, "Must be initialized with words." if word_array.is_a?(Array) && !words.all? {|w| w.is_a?(VoiceBase::JSON::Word)}
  @words = word_array || []
end

Instance Attribute Details

#wordsObject



53
54
55
# File 'lib/voicebase/json.rb', line 53

def words
  @words ||= []
end

Class Method Details

.parse(input, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/voicebase/json.rb', line 9

def parse(input, options = {})
  @debug = options.fetch(:debug, false)
  if input.is_a?(String)
    parse_string(input)
  elsif input.is_a?(::File)
    parse_file(input)
  else
    raise "Invalid input. Expected a String or File, got #{input.class.name}."
  end
end

Instance Method Details

#each(&block) ⇒ Object



61
62
63
# File 'lib/voicebase/json.rb', line 61

def each(&block)
  @words.each {|word| block.call(word)}
end

#errorsObject



57
58
59
# File 'lib/voicebase/json.rb', line 57

def errors
  @words.map {|w| w.error if w.error}.compact
end

#gt(start_time) ⇒ Object



65
66
67
# File 'lib/voicebase/json.rb', line 65

def gt(start_time)
  VoiceBase::JSON.new(select {|w| w.start_time > start_time})
end

#gteq(start_time) ⇒ Object



69
70
71
# File 'lib/voicebase/json.rb', line 69

def gteq(start_time)
  VoiceBase::JSON.new(select {|w| w.start_time >= start_time})
end

#lt(start_time) ⇒ Object



73
74
75
# File 'lib/voicebase/json.rb', line 73

def lt(start_time)
  VoiceBase::JSON.new(select {|w| w.start_time < start_time})
end

#lteq(start_time) ⇒ Object



77
78
79
# File 'lib/voicebase/json.rb', line 77

def lteq(start_time)
  VoiceBase::JSON.new(select {|w| w.start_time <= start_time})
end

#to_aObject



81
82
83
# File 'lib/voicebase/json.rb', line 81

def to_a
  @words
end

#to_jsonObject



85
86
87
# File 'lib/voicebase/json.rb', line 85

def to_json
  map {|w| w.to_hash}.to_json
end