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.
161 162 163 |
# File 'lib/wit_ruby/rest/session.rb', line 161 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.
140 141 142 143 144 145 146 |
# File 'lib/wit_ruby/rest/session.rb', line 140 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.
106 107 108 109 110 111 112 113 |
# File 'lib/wit_ruby/rest/session.rb', line 106 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.
130 131 132 |
# File 'lib/wit_ruby/rest/session.rb', line 130 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.
171 172 173 |
# File 'lib/wit_ruby/rest/session.rb', line 171 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.
153 154 155 |
# File 'lib/wit_ruby/rest/session.rb', line 153 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.
92 93 94 95 96 97 98 99 100 |
# File 'lib/wit_ruby/rest/session.rb', line 92 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.
74 75 76 77 78 79 80 81 82 |
# File 'lib/wit_ruby/rest/session.rb', line 74 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.
64 65 66 67 |
# File 'lib/wit_ruby/rest/session.rb', line 64 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.
196 197 198 199 |
# File 'lib/wit_ruby/rest/session.rb', line 196 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.
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/wit_ruby/rest/session.rb', line 180 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 32 33 |
# 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 # Replace spaces with "%20" for usage in URL .gsub!(" ", "%20") ## 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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wit_ruby/rest/session.rb', line 40 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.
122 123 124 |
# File 'lib/wit_ruby/rest/session.rb', line 122 def update_entity(entity_id, update_entity_data) return @client.put("/entities/#{entity_id}", update_entity_data.json) end |