Sha256: 23b713083dd15b5d7acd53bb08485e049a2f21277a93388937e3f20ddf615497

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module DocomoNlu
  class Spontaneous < ActiveResource::Base
    cattr_accessor :static_headers
    self.site = DocomoNlu.config.nlu_host

    ## Remove format in path (remove .json)
    self.include_format_in_path = false

    ## Setting Format
    self.format = :json

    self.static_headers = headers

    def initialize
      super
      {
        clientVer: "1.0.4",
        language: "ja-JP",
        location: { lat: "0", lon: "0" },
        appRecvTime: Time.now.strftime("%Y-%m-%d %H:%M:%S"),
        appSendTime: Time.now.strftime("%Y-%m-%d %H:%M:%S"),
      }.each do |k, v|
        @attributes.store(k, v)
      end
    end

    def registration(app_id = "", registration_id = "docomo-nlu", app_kind = "docomo-nlu", notification = false)
      body = {
        bot_id: @attributes[:botId],
        app_id: app_id,
        registration_id: registration_id,
        app_kind: app_kind,
        notification: notification,
      }
      res = connection.post("/UserRegistrationServer/users/applications", body.to_json, self.class.headers)
      @attributes.store(:appId, JSON.parse(res.body)["app_id"])
    end

    def dialogue(voice_text, **params)
      @attributes[:voiceText] = voice_text
      params.each do |k, v|
        @attributes[k] ||= v
      end
      res = connection.post("/SpontaneousDialogueServer/dialogue", @attributes.to_json, self.class.headers)
      JSON.parse(res.body)
    end

    class << self
      def headers
        new_headers = static_headers.clone
        new_headers["Content-Type"] = "application/json;charset=UTF-8"
        new_headers
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
docomo-nlu-0.3.6 lib/docomo_nlu/spontaneous.rb