Class: DucklingService::TimeResponse

Inherits:
Object
  • Object
show all
Defined in:
src/services/duckling_service.rb

Overview

Describes all extracted dates and times

Instance Method Summary collapse

Instance Method Details

#first_optionTimeOption, IntervalOption

The first option (extracted time)

Returns:



98
99
100
# File 'src/services/duckling_service.rb', line 98

def first_option
  options.first
end

#has_options?Boolean

Are there any options (extracted times)?

Returns:

  • (Boolean)


91
92
93
# File 'src/services/duckling_service.rb', line 91

def has_options?
  options.size > 0
end

#optionsArray<TimeOption, IntervalOption>

Get all options (extracted times)

Returns:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'src/services/duckling_service.rb', line 112

def options
  @options ||=
    @data['value']['values'].map do |option|
      case option['type']
      when 'value'
        TimeOption.new(
          Time.parse(option['value']),
          option['grain']
        )
      when 'interval'
        IntervalOption.new(
          option['from'] ? Time.parse(option['from']['value']) : nil,
          option['to'] ? Time.parse(option['to']['value']) : nil,
          option['from'] ? option['from']['grain'] : option['to']['grain']
        )
      else
        raise "Unkown duckling time type: #{option['type']}"
      end
    end
end

#single_option?Boolean

Is there a single option (extracted time)?

Returns:

  • (Boolean)


105
106
107
# File 'src/services/duckling_service.rb', line 105

def single_option?
  options.size == 1
end