Class: Wit::REST::Session
- Inherits:
-
Object
- Object
- Wit::REST::Session
- Defined in:
- lib/wit_ruby/rest/session.rb
Instance Attribute Summary collapse
-
#last_result ⇒ Object
readonly
Able to read cached result from last request.
Instance Method Summary collapse
-
#add_expression(entity_id, value, expression) ⇒ Wit::REST::Result
POST - adds a new expression to the value of the entity.
-
#add_value(new_value_with_entity) ⇒ Wit::REST::Result
POST - adds the possible value into the list of values for the given - entity with the id.
-
#create_entity(new_entity) ⇒ Wit::REST::Result
POST - creates a new entity with the given attributes.
-
#delete_entity(entity_id) ⇒ Wit::REST::Result
DELETE - deletes the given entity with the entity id.
-
#delete_expression(entity_id, value, expression) ⇒ Wit::REST::Result
DELETE - deletes the expression in the value of the entity.
-
#delete_value(entity_name, delete_value) ⇒ Wit::REST::Result
DELETE - deletes the value from the list of values in the entity with - with the given value.
-
#get_entities(entity_id = nil) ⇒ Wit::REST::MultiEntity
GET - returns a list of available entities given this instance with the given token if no id is given.
-
#get_intents(intent_indicator = nil) ⇒ Wit:REST::Intent
GET - returns either a list of intents if no id is given.
-
#get_message(message_id) ⇒ Wit::REST::Message
GET - returns stored message for specific id.
-
#initialize(client) ⇒ Session
constructor
Initialize with the given client.
-
#refresh_last ⇒ Wit::REST::Result
Used to refresh the last result given from the last request.
-
#refresh_results(result) ⇒ Wit::REST::Result
Used to refresh the results from the given results.
-
#send_message(message) ⇒ Wit::REST::Message
GET - extracted meaning from a sentence.
-
#send_sound_message(sound_file_path) ⇒ Object
POST - extract meaning from a audio file Do check the certain documentation of what the specific audio file should be.
-
#update_entity(entity_id, update_entity_data) ⇒ Wit::REST::Result
PUT - updates a given entity with the specific entity id and BodyJson data.
Constructor Details
#initialize(client) ⇒ Session
Initialize with the given client.
14 15 16 |
# File 'lib/wit_ruby/rest/session.rb', line 14 def initialize(client) @client = client end |
Instance Attribute Details
#last_result ⇒ Object (readonly)
Able to read cached result from last request.
10 11 12 |
# File 'lib/wit_ruby/rest/session.rb', line 10 def last_result @last_result end |
Instance Method Details
#add_expression(entity_id, value, expression) ⇒ Wit::REST::Result
POST - adds a new expression to the value of the entity.
160 161 162 |
# File 'lib/wit_ruby/rest/session.rb', line 160 def add_expression(entity_id, value, expression) return @client.post("/entities/#{entity_id}/values/#{value}/expressions", "{\"expression\":\"#{expression}\"}") end |
#add_value(new_value_with_entity) ⇒ Wit::REST::Result
notify wit.ai that documentation is off.
POST - adds the possible value into the list of values for the given
- entity with the id.
139 140 141 142 143 144 145 |
# File 'lib/wit_ruby/rest/session.rb', line 139 def add_value(new_value_with_entity) ## Makes sure values exist and has a value and id as well. if new_value_with_entity.id.nil? || new_value_with_entity.one_value_to_json == "null" raise NotCorrectSchema.new("The current BodyJson object passed in does not have either an \"id\" or a \"value\" defined.") end return @client.post("/entities/#{new_value_with_entity.id}/values", new_value_with_entity.one_value_to_json) end |
#create_entity(new_entity) ⇒ Wit::REST::Result
POST - creates a new entity with the given attributes.
105 106 107 108 109 110 111 112 |
# File 'lib/wit_ruby/rest/session.rb', line 105 def create_entity(new_entity) ## Checks to make sure it has an id, if not, raise error. if new_entity.id.nil? raise NotCorrectSchema.new("The current BodyJson object passed in does not have an \"id\" defined.") end return @client.post("/entities", new_entity.json) end |
#delete_entity(entity_id) ⇒ Wit::REST::Result
DELETE - deletes the given entity with the entity id.
129 130 131 |
# File 'lib/wit_ruby/rest/session.rb', line 129 def delete_entity(entity_id) return @client.delete("/entities/#{entity_id}") end |
#delete_expression(entity_id, value, expression) ⇒ Wit::REST::Result
DELETE - deletes the expression in the value of the entity.
170 171 172 |
# File 'lib/wit_ruby/rest/session.rb', line 170 def delete_expression(entity_id, value, expression) return @client.delete("/entities/#{entity_id}/values/#{value}/expressions/#{expression}") end |
#delete_value(entity_name, delete_value) ⇒ Wit::REST::Result
DELETE - deletes the value from the list of values in the entity with
- with the given value.
152 153 154 |
# File 'lib/wit_ruby/rest/session.rb', line 152 def delete_value(entity_name, delete_value) return @client.delete("/entities/#{entity_name}/values/#{delete_value}") end |
#get_entities(entity_id = nil) ⇒ Wit::REST::MultiEntity
notify Wit.ai to fix their documentations as there is a wrong description.
GET - returns a list of available entities given this instance with the
given token if no id is given.
- returns the specific entity and its parameters with a given id.
91 92 93 94 95 96 97 98 99 |
# File 'lib/wit_ruby/rest/session.rb', line 91 def get_entities(entity_id = nil) ## No specific id, so get list of entities results = entity_id.nil? ? @client.get("/entities") : @client.get("/entities/#{entity_id}") ## Same concept but wrap it properly if neccessary. returnObject = entity_id.nil? ? MultiEntity : Entity return return_with_class(returnObject, results) end |
#get_intents(intent_indicator = nil) ⇒ Wit:REST::Intent
GET - returns either a list of intents if no id is given.
- returns the specific intent of the id given.
73 74 75 76 77 78 79 80 81 |
# File 'lib/wit_ruby/rest/session.rb', line 73 def get_intents(intent_indicator = nil) ## No specific id, so get list of intents or specific id, return it as Intent object results = intent_indicator.nil? ? @client.get("/intents") : @client.get("/intents/#{intent_indicator}") ## Same concept but wrap it around proper object returnObject = intent_indicator.nil? ? MultiIntent : Intent return return_with_class(returnObject, results) end |
#get_message(message_id) ⇒ Wit::REST::Message
possibly renaming as it is ambigious compared to send_message.
Notify Wit.ai as there documentation does not include the stats parameter
GET - returns stored message for specific id.
62 63 64 65 66 |
# File 'lib/wit_ruby/rest/session.rb', line 62 def () results = @client.get("/messages/#{}") return return_with_class(Wit::REST::Message, results) end |
#refresh_last ⇒ Wit::REST::Result
Used to refresh the last result given from the last request.
195 196 197 198 |
# File 'lib/wit_ruby/rest/session.rb', line 195 def refresh_last refreshed_last_result = @client.request_from_result(@last_result.restCode, @last_result.restPath, @last_result.restBody) return_with_class(@last_result.class, refreshed_last_result) end |
#refresh_results(result) ⇒ Wit::REST::Result
Used to refresh the results from the given results. Only applicable to result objects that directly came from the session.
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/wit_ruby/rest/session.rb', line 179 def refresh_results(result) ## Call client with refresh results method ## Checks to see if its part of the specified objects in the Wit module ## Checks to see if the object is refreshable ## If it isn't part of one of these two, then raise error for not being refreshable. result_class = result.class unless result_class.name.split("::")[0] == "Wit" && result.refreshable? raise NotRefreshable.new(%(The inputted object with class "#{result.class}" is not refreshable.)) end refreshed_result = @client.request_from_result(result.restCode, result.restPath, result.restBody) return return_with_class(result_class, refreshed_result) end |
#send_message(message) ⇒ Wit::REST::Message
allow for JSON pass in.
GET - extracted meaning from a sentence.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/wit_ruby/rest/session.rb', line 22 def () ## Check to see if message length is between 0 and 256 length = .length if length <= 0 || length > 256 raise NotCorrectSchema.new("The given message, \"#{}\" is either too short or too long. Message length needs to be between 0 and 256.") end ## Recieve unwrapped results results = @client.get("/message?q=#{}") return return_with_class(Wit::REST::Message, results) end |
#send_sound_message(sound_file_path) ⇒ Object
POST - extract meaning from a audio file Do check the certain documentation of what the specific audio file should be.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/wit_ruby/rest/session.rb', line 38 def (sound_file_path) ## Given the path, we send the file and add proper headers ## Check if it is a specifc file type ## This seems dirty, look more into it. sound_file_type = sound_file_path.split(".")[-1] ## Raise error if not accepted unless ["wav", "mp3", "ulaw", "raw"].include?(sound_file_type) raise NotCorrectSchema.new("The current sound file is not one of the supported types. The file types accepted are .wav, .mp3, .ulaw and .raw") end ## Set Content-Type header by overiding it with the correct filetype. ## If it is raw, add the extra params to the end of it. content_overide = "audio/#{sound_file_type}" content_overide += ";encoding=unsigned-integer;bits=16;rate=8000;endian=big" if sound_file_type == "raw" results = @client.post("/speech", File.read(sound_file_path), "audio/#{sound_file_type}") return return_with_class(Wit::REST::Message, results) end |
#update_entity(entity_id, update_entity_data) ⇒ Wit::REST::Result
notify Wit.ai to return back the updated entity results.
PUT - updates a given entity with the specific entity id and BodyJson data.
121 122 123 |
# File 'lib/wit_ruby/rest/session.rb', line 121 def update_entity(entity_id, update_entity_data) return @client.put("/entities/#{entity_id}", update_entity_data.json) end |