Class: DocomoNlu::Management::AIMLBase

Inherits:
Base
  • Object
show all
Defined in:
lib/docomo_nlu/management/aiml_base.rb

Direct Known Subclasses

Bot, Scenario

Defined Under Namespace

Classes: 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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/docomo_nlu/management/aiml_base.rb', line 114

def check_status(method, path)
  response = connection.get(path, headers)

  case method
  when :compile
    JSON.parse(response.body)["status"].tap do |status|
      raise ActiveResource::ServerError, response if %w[ErrorFinish NotCompiled].include?(status)
    end
  when :transfer
    JSON.parse(response.body)["transferStatusResponses"][0]["status"].tap do |status|
      raise ActiveResource::ServerError, response if %w[ErrorFinish NotTransfered].include?(status)
    end
  end
end

.compile(prefix_options) ⇒ Object



81
82
83
# File 'lib/docomo_nlu/management/aiml_base.rb', line 81

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

.deploy(prefix_options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/docomo_nlu/management/aiml_base.rb', line 95

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)
  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)
  end
  true
end

.deploy_request(method, prefix_options) ⇒ Object



89
90
91
92
93
# File 'lib/docomo_nlu/management/aiml_base.rb', line 89

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::DEFAULT_PARSER.parse(response_body["statusUri"]).path.gsub!(/NLPManagementAPI/, "management/v2.6")
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/docomo_nlu/management/aiml_base.rb', line 49

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

  if check_response(response)
    return 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
  nil
end

.transfer(prefix_options) ⇒ Object



85
86
87
# File 'lib/docomo_nlu/management/aiml_base.rb', line 85

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

.upload(file, prefix_options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/docomo_nlu/management/aiml_base.rb', line 67

def upload(file, prefix_options)
  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 FileModel.collection_path(prefix_options), params
  check_response(response)
end

Instance Method Details

#compileObject



33
34
35
36
# File 'lib/docomo_nlu/management/aiml_base.rb', line 33

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

#deployObject



43
44
45
46
# File 'lib/docomo_nlu/management/aiml_base.rb', line 43

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

#download(extra_path = "") ⇒ Object



22
23
24
25
# File 'lib/docomo_nlu/management/aiml_base.rb', line 22

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

#transferObject



38
39
40
41
# File 'lib/docomo_nlu/management/aiml_base.rb', line 38

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

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



27
28
29
30
31
# File 'lib/docomo_nlu/management/aiml_base.rb', line 27

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