Class: DocomoNlu::Management::Base
- Inherits:
-
ActiveResource::Base
- Object
- ActiveResource::Base
- DocomoNlu::Management::Base
- Defined in:
- lib/docomo_nlu/management/base.rb
Direct Known Subclasses
AIMLBase, AIMLBase::FileModel, Account, BotLog, Config, DefaultPredicate, Entry, Log, MultipartBase, OKNGBase, Organization, OrganizationMember, PredicateName, Project, ProjectMember, Property, Provider, Scenario::TemplateScenarios, Scenario::UserScenarios, ScenarioProject, ScenarioUtil, TaskProject
Class Method Summary collapse
- .check_response(response) ⇒ Object
- .headers ⇒ Object
- .instantiate_collection(collection, original_params = {}, prefix_options = {}) ⇒ Object
- .instantiate_record(record, prefix_options = {}) ⇒ Object
Instance Method Summary collapse
-
#id_from_response(response) ⇒ Object
Override.
-
#login(account_name, password) ⇒ Object
Get NLPManagement’s AccessToken.
-
#logout ⇒ Object
Delete NLPManagement’s AccessToken.
-
#static_headers ⇒ Object
For Dynamically generated headers via rmosolgo.github.io/blog/2014/02/05/dynamically-generated-headers-for-activeresource-requests/.
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 |
.headers ⇒ Object
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 = {}, = {}) 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, = {}) 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 login(account_name, password) request_body = { accountName: account_name, 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 |
#logout ⇒ Object
Delete NLPManagement’s AccessToken.
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_headers ⇒ Object
For Dynamically generated headers via rmosolgo.github.io/blog/2014/02/05/dynamically-generated-headers-for-activeresource-requests/
9 |
# File 'lib/docomo_nlu/management/base.rb', line 9 cattr_accessor :static_headers |