Sha256: 5761f27630fa66d7314796d7677a222724bc2eea67a07b84967b94125ef53afe

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require "json"

module GoogleAssistant
  class DialogState
    DEFAULT_STATE = { "state" => nil, "data" => {} }.freeze

    def initialize(state_hash_or_conversation_token = nil)
      if state_hash_or_conversation_token.is_a?(String)
        @raw_token = state_hash_or_conversation_token
        @state_hash = parse_token(state_hash_or_conversation_token)
      elsif state_hash_or_conversation_token.is_a?(Hash)
        @state_hash = state_hash_or_conversation_token
      else
        @state_hash = DEFAULT_STATE.dup
      end
    end

    def state
      state_hash["state"]
    end

    def state=(state)
      state_hash["state"] = state
    end

    def data
      state_hash["data"]
    end

    def data=(data)
      raise "DialogState data must be a hash" unless data.is_a?(Hash)

      state_hash["data"] = data
    end

    def to_json
      state_hash.to_json
    end

    private

    attr_reader :state_hash, :raw_token

    def parse_token(token)
      JSON.parse(token)
    rescue JSON::ParserError, TypeError
      DEFAULT_STATE.dup
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
google_assistant-1.0.0 lib/google_assistant/dialog_state.rb
google_assistant-1.0.0.beta lib/google_assistant/dialog_state.rb
google_assistant-0.2.0 lib/google_assistant/dialog_state.rb
google_assistant-0.1.0 lib/google_assistant/dialog_state.rb