Sha256: c7ae09a9423418d5a35233ba3d3c04c63125b9be974be0fc8a9139c4e74174a4

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require "zip"

module DocomoNlu
  module Management::V26
    class BotLog < Base
      self.element_name = "botlogs"
      self.prefix = "/management/v2.6/projects/:project_id/"

      def all
        Rails.logger.debug "You shoud use 'download' method"
      end

      def find
        Rails.logger.debug "You shoud use 'download' method"
      end

      def where
        Rails.logger.debug "You shoud use 'download' method"
      end

      def download(bot_id, params = {})
        attributes[:file] = self.class.download(prefix_options, bot_id, params.slice(:start, :end))
        attributes[:file]
      end

      def extract_data
        return unless attributes[:file]

        logs = []
        self.class.unzip(attributes[:file]) do |_name, body|
          logs = body.map {|b| JSON.parse(b) }
        end
        logs
      end

      class << self
        def download(prefix_options, bot_id, params = {})
          conn = Faraday.new(url: site.to_s, ssl: { verify: false }) do |builder|
            builder.adapter :net_http
          end
          conn.headers["Authorization"] = access_token
          response = conn.get("#{collection_path(prefix_options)}?botId=#{bot_id}&#{params.to_query}")

          if check_response(response)
            Tempfile.open(["docomo-nlu", ".zip"]) do |f|
              f.binmode
              f.write response.body
              f
            end
          end
        end

        def unzip(file)
          ::Zip::File.open(file.path) do |zf|
            zf.each do |entry|
              next unless entry.file?

              name = entry.name
              body = entry.get_input_stream.read.split(/\R/)
              yield name, body if block_given?
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
docomo-nlu-0.2.7 lib/docomo-nlu/management/V26/bot_log.rb
docomo-nlu-0.2.6 lib/docomo-nlu/management/V26/bot_log.rb