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:



140
141
142
# File 'lib/wit_ruby/rest/session.rb', line 140

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:



119
120
121
122
123
124
125
# File 'lib/wit_ruby/rest/session.rb', line 119

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:



85
86
87
88
89
90
91
92
# File 'lib/wit_ruby/rest/session.rb', line 85

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:



109
110
111
# File 'lib/wit_ruby/rest/session.rb', line 109

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:



150
151
152
# File 'lib/wit_ruby/rest/session.rb', line 150

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:



132
133
134
# File 'lib/wit_ruby/rest/session.rb', line 132

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:



71
72
73
74
75
76
77
78
79
# File 'lib/wit_ruby/rest/session.rb', line 71

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.



53
54
55
56
57
58
59
60
61
# File 'lib/wit_ruby/rest/session.rb', line 53

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:



42
43
44
45
46
# File 'lib/wit_ruby/rest/session.rb', line 42

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:



175
176
177
178
# File 'lib/wit_ruby/rest/session.rb', line 175

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



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/wit_ruby/rest/session.rb', line 159

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
# File 'lib/wit_ruby/rest/session.rb', line 22

def send_message(message)
  ## Recieve unwrapped results
  results = @client.get("/message?q=#{message}")
  return return_with_class(Wit::REST::Message, results)
end

#send_sound_message(sound) ⇒ Object

POST - extract meaning from a audio file Do check the certain documentation of what the specific audio file should be.

Parameters:

  • sound (String)

    path to sound file.



33
34
# File 'lib/wit_ruby/rest/session.rb', line 33

def send_sound_message(sound)
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:



101
102
103
# File 'lib/wit_ruby/rest/session.rb', line 101

def update_entity(entity_id, update_entity_data)
  return @client.put("/entities/#{entity_id}", update_entity_data.json)
end