Class: DocomoNlu::Management::V23::AIMLBase

Inherits:
Base
  • Object
show all
Defined in:
lib/docomo-nlu/management/V23/aiml_base.rb

Defined Under Namespace

Classes: FAQModel, FileModel

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

check_response, headers, #id_from_response, instantiate_collection, instantiate_record, #login, #logout, #static_headers

Class Method Details

.check_status(method, path) ⇒ Object



161
162
163
164
165
166
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 161

def check_status(method, path)
  case method
  when :compile, :faq then JSON.parse(connection.get(path, headers).body)["status"]
  when :transfer      then JSON.parse(connection.get(path, headers).body)["transferStatusResponses"][0]["status"]
  end
end

.compile(prefix_options) ⇒ Object



126
127
128
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 126

def compile(prefix_options)
  return if deploy_request(:compile, prefix_options)  != ""
end

.deploy(prefix_options) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 140

def deploy(prefix_options)
  # compile and status check
  compile_status = false
  check_path = deploy_request(:compile, prefix_options)
  while check_path && compile_status != "Completed"
    sleep(0.2)
    compile_status = check_status(:compile, check_path)
    raise ActiveResource::ServerError if %w[ErrorFinish NotCompiled].include?(compile_status)
  end

  # transfer and status check
  transfer_status = false
  check_path = deploy_request(:transfer, prefix_options)
  while check_path && transfer_status != "Completed"
    sleep(0.2)
    transfer_status = check_status(:transfer, check_path)
    raise ActiveResource::ServerError if %w[ErrorFinish NotTransfered].include?(transfer_status)
  end
  true
end

.deploy_request(method, prefix_options) ⇒ Object



134
135
136
137
138
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 134

def deploy_request(method, prefix_options)
  response_body = JSON.parse(connection.post(Scenario.element_path(method, prefix_options), "", headers).body)
  # Sometimes, API returns wrong url, replace correct path.
  URI.parse(response_body["statusUri"]).path.gsub!(/NLPManagementAPI/, "management/v2.2")
end

.download(prefix_options, extra_path = "", model = FileModel) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 95

def download(prefix_options, extra_path = "", model = FileModel)
  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("#{model.collection_path(prefix_options)}/#{extra_path}")

  if check_response(response)
    instantiate_record({}, prefix_options).tap do |record|
      record.file = Tempfile.open(["docomo-nlu", ".#{prefix_options[:method]}"]) do |f|
        f.write response.body.force_encoding("UTF-8")
        f
      end
    end
  end
end

.transfer(prefix_options) ⇒ Object



130
131
132
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 130

def transfer(prefix_options)
  return if deploy_request(:transfer, prefix_options) != ""
end

.upload(file, prefix_options, model = FileModel) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 112

def upload(file, prefix_options, model = FileModel)
  conn = Faraday.new(url: site.to_s, ssl: { verify: false }) do |builder|
    builder.request :multipart # マルチパートでデータを送信
    builder.request :url_encoded
    builder.adapter :net_http
  end
  conn.headers["Authorization"] = access_token
  params = {
    uploadFile: Faraday::UploadIO.new(file.path, "text/plain"),
  }
  response = conn.put model.collection_path(prefix_options), params
  check_response(response)
end

Instance Method Details

#check_faq_status(method) ⇒ Object



88
89
90
91
92
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 88

def check_faq_status(method)
  prefix_options[:bot_id] ||= botId
  prefix_options[:method] = method
  self.class.check_status(:faq, "#{FileModel.collection_path(prefix_options)}/status")
end

#compileObject



35
36
37
38
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 35

def compile
  prefix_options[:bot_id] ||= botId
  self.class.compile(prefix_options)
end

#deployObject



45
46
47
48
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 45

def deploy
  prefix_options[:bot_id] ||= botId
  self.class.deploy(prefix_options)
end

#download(extra_path = "") ⇒ Object

For AIML



24
25
26
27
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 24

def download(extra_path = "")
  prefix_options[:bot_id] ||= botId
  @attributes[:file] = self.class.download(prefix_options, extra_path).file
end

#download_stopkeyObject



64
65
66
67
68
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 64

def download_stopkey
  prefix_options[:bot_id] ||= botId
  prefix_options[:method] = "stopkey"
  @attributes[:stopkey] = self.class.download(prefix_options, FAQModel).file
end

#download_truthlistObject



76
77
78
79
80
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 76

def download_truthlist
  prefix_options[:bot_id] ||= botId
  prefix_options[:method] = "truthlist"
  @attributes[:truthlist] = self.class.download(prefix_options, FAQModel).file
end

#transferObject



40
41
42
43
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 40

def transfer
  prefix_options[:bot_id] ||= botId
  self.class.deploy_request(prefix_options)
end

#upload(file, type = :aiml) ⇒ Object



29
30
31
32
33
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 29

def upload(file, type = :aiml)
  prefix_options[:bot_id] ||= botId
  prefix_options[:method] ||= type
  self.class.upload(file, prefix_options)
end

#upload_entry(file) ⇒ Object



82
83
84
85
86
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 82

def upload_entry(file)
  prefix_options[:bot_id] ||= botId
  prefix_options[:method] = "entry"
  self.class.upload(file, prefix_options, FAQModel)
end

#upload_stopkey(file) ⇒ Object



58
59
60
61
62
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 58

def upload_stopkey(file)
  prefix_options[:bot_id] ||= botId
  prefix_options[:method] = "stopkey"
  self.class.upload(file, prefix_options, FAQModel)
end

#upload_truthlist(file) ⇒ Object



70
71
72
73
74
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 70

def upload_truthlist(file)
  prefix_options[:bot_id] ||= botId
  prefix_options[:method] = "truthlist"
  self.class.upload(file, prefix_options, FAQModel)
end

#upload_userdic(file) ⇒ Object

For FAQ



52
53
54
55
56
# File 'lib/docomo-nlu/management/V23/aiml_base.rb', line 52

def upload_userdic(file)
  prefix_options[:bot_id] ||= botId
  prefix_options[:method] = "userDic"
  self.class.upload(file, prefix_options, FAQModel)
end