Class: DocomoNlu::Management::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_response(response) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/docomo_nlu/management/base.rb', line 74

def check_response(response)
  case response.status
  when 400       then raise ActiveResource::BadRequest, response
  when 401       then raise ActiveResource::UnauthorizedAccess, response
  when 403       then raise ActiveResource::ForbiddenAccess, response
  when 404       then raise ActiveResource::ResourceNotFound, response
  when 409       then raise ActiveResource::ResourceConflict, response
  when 503       then raise ActiveResource::ServerError, response
  when 200..204  then true
  end
end

.headersObject



68
69
70
71
72
# File 'lib/docomo_nlu/management/base.rb', line 68

def headers
  new_headers = static_headers.clone
  new_headers["Authorization"] = access_token
  new_headers
end

.instantiate_collection(collection, original_params = {}, prefix_options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/docomo_nlu/management/base.rb', line 48

def instantiate_collection(collection, original_params = {}, prefix_options = {})
  if collection.is_a?(Hash)
    collection = if collection.compact.empty?
                   []
                 else
                   [collection]
                 end
  elsif collection[0].is_a?(String)
    collection = [{ params: collection }]
  end
  super
end

.instantiate_record(record, prefix_options = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/docomo_nlu/management/base.rb', line 61

def instantiate_record(record, prefix_options = {})
  record = record[0] if record.is_a?(Array)
  resource_id = record["#{to_s.split("::").last.downcase!}Id"]
  record["id"] = resource_id if resource_id
  super
end

Instance Method Details

#id_from_response(response) ⇒ Object

Override. Insert generated id to parameter ‘id’ after save or create



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

def id_from_response(response)
  ActiveSupport::JSON.decode(response.body)["#{self.class.to_s.split("::").last.downcase!}Id"] if response.body.present?
end

#login(account_name, password) ⇒ Object

Get NLPManagement’s AccessToken.



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

def (, password)
  request_body = { accountName: , password: password }.to_json
  response_body = JSON.parse(connection.post("/management/v2.6/login", request_body, self.class.headers).body)
  self.access_token = response_body["accessToken"]
end

#logoutObject

Delete NLPManagement’s AccessToken.

Raises:

  • (ActiveResource::BadRequest)


34
35
36
37
38
39
40
# File 'lib/docomo_nlu/management/base.rb', line 34

def logout
  res = connection.get("/management/v2.6/logout", self.class.headers) if access_token.present?
  raise ActiveResource::BadRequest, "Invalid access token" unless res

  self.access_token = nil
  true
end

#static_headersObject



9
# File 'lib/docomo_nlu/management/base.rb', line 9

cattr_accessor :static_headers