Class: Wit::REST::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/wit_ruby/rest/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Session

Initialize with the given client.

Parameters:



14
15
16
# File 'lib/wit_ruby/rest/session.rb', line 14

def initialize(client)
  @client = client
end

Instance Attribute Details

#last_resultObject (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.

Parameters:

  • new_expression_with_id_and_value (Wit::REST::BodyJson)

    includes new expression for said ID and value.

Returns:



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

TODO:

notify wit.ai that documentation is off.

POST - adds the possible value into the list of values for the given

- entity with the id.

Parameters:

  • new_value_with_entity (Wit::REST::BodyJson)

    includes the new value and entity name as ID.

Returns:



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.

Parameters:

Returns:



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.

Parameters:

  • entity_id (String)

    entity id that is going to be deleted.

Returns:



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.

Parameters:

  • entity_id (String)

    entity id that will have the expression deleted.

  • value (String)

    value name that will have the expression deleted.

  • expression (String)

    expression that will be deleted.

Returns:



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.

Parameters:

  • entity_name (String)

    name of entity that will have value deleted

  • delete_value (String)

    name of value to be deleted

Returns:



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

TODO:

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.

Parameters:

  • entity_id (String) (defaults to: nil)

    entity id for specific retrieval

Returns:



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.

Parameters:

  • intent_indicator (String) (defaults to: nil)

    the id or name of the intent

Returns:

  • (Wit:REST::Intent)
    Wit::REST::MultiIntent

    results of intent call to API.



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

TODO:

possibly renaming as it is ambigious compared to send_message.

TODO:

Notify Wit.ai as there documentation does not include the stats parameter

GET - returns stored message for specific id.

Parameters:

  • message_id (String)

    message id of message in API servers.

Returns:



64
65
66
67
# File 'lib/wit_ruby/rest/session.rb', line 64

def get_message(message_id)
  results = @client.get("/messages/#{message_id}")
  return return_with_class(Wit::REST::Message, results)
end

#refresh_lastWit::REST::Result

Used to refresh the last result given from the last request.

Returns:



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.

Parameters:

Returns:

  • (Wit::REST::Result)

    result back that will be wrapped around it’s specific wrapper object



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

TODO:

allow for JSON pass in.

GET - extracted meaning from a sentence.

Parameters:

  • message (String)

    sentence being examined from API.

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wit_ruby/rest/session.rb', line 22

def send_message(message)
  ## Check to see if message length is between 0 and 256
  length  = message.length
  if length <= 0 || length > 256
    raise NotCorrectSchema.new("The given message, \"#{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
  message.gsub!(" ", "%20")
  ## Recieve unwrapped results
  results = @client.get("/message?q=#{message}")
  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.

Parameters:

  • sound_file_path (String)

    path to sound file.



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 send_sound_message(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

TODO:

notify Wit.ai to return back the updated entity results.

PUT - updates a given entity with the specific entity id and BodyJson data.

Parameters:

  • entity_id (String)

    entity id that will be updated.

  • update_entity_data (Wit::REST::BodyJson)

    new data that will update the entity.

Returns:



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