wit_ruby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 971419a9504d3b3f6a033650f424daaba39f6a3b
4
- data.tar.gz: f81c794354b97acf237ee363474b7dfb9513dff3
3
+ metadata.gz: 1640efe7f4c172e77b20dfd68abb9cd32aad90fc
4
+ data.tar.gz: cd459ecaa05c9169b7c86287137effa7efc9381c
5
5
  SHA512:
6
- metadata.gz: ab3c0ab3b94f1c6b02d82ab2e4fe717d3a88641117fe4f2e1f45b21c7ef2e124995dcca013d3b38dee36176aae6051b38444aefa6ba957259ee5a9aa7d0b59ff
7
- data.tar.gz: ec22891627f70d3d99e49abfe580c973819db15d8d6e2e052f42dc425a0fe8b9e31b13bf7b6cc05e55de23282e3a6e802030c5de20b03ae835ef4f1f5dc77ab2
6
+ metadata.gz: b48276ba61d3b267bf6596a46d2af8b2a3935155f12ad87898c21b7d7a08546cf0cac7a3849758d9cb54c7be7a7b0fdc39668678fd85442d59677f44b7fe2dea
7
+ data.tar.gz: 2c418a23e162702fae0a3b7c73e4508760852975124cf30d2a639e7ca0dd2c3def1b252bf252ea12aa74b1ddddfa45419694be1fc9544ed5356ca265b5e13b95
data/.travis.yml CHANGED
@@ -3,6 +3,7 @@ cache: bundler
3
3
  rvm:
4
4
  - jruby
5
5
  - 2.0.0
6
+ - 1.9.3
6
7
  script: bundle exec rake
7
8
  env:
8
9
  global:
data/CHANGELOG CHANGED
@@ -1,3 +1,16 @@
1
+ == 0.0.3
2
+ * Implemented getting array of entities from instance (GET).
3
+ * Implemented getting a specific entity from its name/id (GET).
4
+ * Implemented creation of entities (POST).
5
+ * Implemented the ability to update entities given the ID (PUT).
6
+ * Implemented deletion of entities (DELETE).
7
+ * Implemented addition of values (POST).
8
+ * Implemented deletion of values (DELETE).
9
+ * Implemented addition of expressions (POST).
10
+ * Implemented deletion of expressions (DELETE).
11
+ * Changed required Ruby version from 1.8.7 to 1.9.3
12
+ * Added new class definition for post / put calls assistance. Helps with JSON generation.
13
+
1
14
  == 0.0.2
2
15
  * Implemented API call to get specific information from a sent message from it's given ID (GET).
3
16
  * Intent specific API calls functional (GET).
data/README.md CHANGED
@@ -2,7 +2,11 @@
2
2
 
3
3
  # WitRuby
4
4
 
5
- Provides a unofficial Ruby API Wrapper for the Wit.ai API. Still implementing most functionalities. Go over to https://rubygems.org/gems/wit_ruby for specific information and documentation.
5
+ Provides a unofficial and (seemingly) pleasant Ruby API Wrapper for the Wit.ai API. As of 0.0.3, most functionalities have been implemented. Go over to https://rubygems.org/gems/wit_ruby for more information.
6
+
7
+ Documentation that you will definitely need : http://rubydoc.info/gems/wit_ruby/
8
+
9
+ Do also reference the Wit.ai API documentation : https://wit.ai/docs/api
6
10
 
7
11
  [![Build Status](https://travis-ci.org/gching/wit_ruby.svg?branch=master)](https://travis-ci.org/gching/wit_ruby)
8
12
  [![Gem Version](https://badge.fury.io/rb/wit_ruby.png)](http://badge.fury.io/rb/wit_ruby)
@@ -30,51 +34,115 @@ Remember to put this up to access it!
30
34
 
31
35
  To start using the wrapper, create a client with an authorization token given from Wit.ai. Set this either in ENV["WIT_AI_TOKEN"] or pass it on it the parameters as a hash. Default settings can be overridden by this hash as well.
32
36
 
33
- $ ## Default to ENV["WIT_AI_TOKEN"]
37
+ ## Default to ENV["WIT_AI_TOKEN"]
34
38
  $ client = Wit::REST::Client.new()
35
- $ ## Override token when created
39
+ ## Override token when created
36
40
  $ client = Wit::REST::Client.new(token: "Insert Token Here")
37
41
 
38
42
  The client provides also a session for you to mainly do API calls. I suggest you save the session somewhere for easy access.
39
43
 
40
44
  $ session = client.session
41
45
 
42
- As of 0.0.2, only sending a message, getting message information and intent specific calls are implemented.
46
+ Please again, do look over documentation to see the full scope of usage and configuration.
47
+
48
+ ### Result
49
+
50
+ Every method returns a class wrapper corresponding specifically to the results pertaining to it. The superclass that is inherited (Wit::REST::Result) allows for you to easily access the results of the API call. The results is converted to a hash and is saved in this result class.
51
+ You can call methods on it and if it matches the result's hash, it will return it's value. For example,
52
+
53
+ ## results.hash = {"a" => "b"}
54
+ $ results.a
55
+ ## = "b"
56
+
57
+ Every direct result returned from each method call defined from the session will be refreshable.
58
+
59
+ ### JSON Specific Calls
43
60
 
44
- ## Message
61
+ Methods that require JSON for the API calls will be generated through use of the class Wit::REST::BodyJson.
62
+ BodyJson inherits from OpenStruct and will assist in providing properly formatted JSON for the methods.
63
+
64
+ Depending on the data needed, certain methods are provided. For example:
65
+
66
+ ## First instantiate.
67
+ $ new_body = Wit::REST::BodyJson.new
68
+ ## Adding an ID and doc parameter
69
+ $ new_body.id = "Some ID"
70
+ $ new_body.doc = "Some doc"
71
+ ## Adding value and expression.
72
+ $ new_body.add_value("Some value", "possible expressions that--", "--that can be added to this value")
73
+ $ new_body.add_expression("Some existing value", "possible expressions that--", "--that can be added to this value")
74
+
75
+
76
+ ### Message
45
77
 
46
78
  To send a specific message, use the saved session to send a given string as a parameter.
47
79
 
48
- $ results = session.send_message("Your Message")
80
+ $ session.send_message("Your Message")
49
81
 
50
82
  To get a specific messages information from the wit.ai, pass in the message's ID and use the method below.
51
83
 
52
- $ results = session.get_message("Message ID")
84
+ $ session.get_message("Message ID")
53
85
 
54
- ## Intent
86
+ ### Intent
55
87
 
56
88
  To get a list of intents in the specific instance over at wit.ai.
57
89
 
58
- $ ## Array of all intents used.
59
- $ results = session.get_intents
90
+ $ session.get_intents
60
91
 
61
92
  To get a specific intent information, pass in it's ID or name.
62
93
 
63
- $ ## Specific intent information.
64
- $ results = session.get_intents("Intent ID or Name")
94
+ $ session.get_intents("Intent ID or Name")
65
95
 
66
- ## Result
96
+ ### Entities
67
97
 
68
- Every method returns a class wrapper corresponding specifically to that result. The superclass that is inherited (Wit::REST::Result) allows for you to easily access the results of the API call. The results is converted to a hash and is saved in this result class.
69
- You can call methods on it and if it matches the result's hash, it will return it's value. For example,
98
+ To get a list of entities for this instance.
70
99
 
71
- $ ## results.hash = {"a" => "b"}
72
- $ results.a
73
- $ => "b"
100
+ $ session.get_entities
101
+
102
+ To get a specific entity, pass it in's ID
103
+
104
+ $ session.get_entities("Entity ID")
105
+
106
+ To create and update entities, methods require a Wit::REST::BodyJson object with an id defined and optional doc, values and expressions defined.
107
+
108
+ ## New entity
109
+ $ new_entity = Wit::REST::BodyJson.new(id: "some id")
110
+ $ session.create_entity(new_entity)
111
+ ## Update it with a new doc parameter
112
+ $ new_entity.doc = "some doc"
113
+
114
+ Deleting the entity requires the passing of it's ID
115
+
116
+ $ session.delete_entity("some entity id")
117
+
118
+
119
+ ### Values
120
+
121
+ To create a new value, a Wit::REST::BodyJson object needs to be created with the ID of the entity and new value.
122
+
123
+ $ value_create = Wit::REST::BodyJson.new(id: "entity for new value")
124
+ $ value_create.add_value("some new value")
125
+ $ session.add_value(value_create)
126
+
127
+ To delete, require the passing of the entity id and value name.
128
+
129
+ $ session.delete_value("entity id", "value name")
130
+
131
+ ### Expressions
132
+
133
+ Add an expression by passing in the entity's id, value name, and the new expression.
134
+
135
+ $ session.add_expression("some entity id", "some value name", "new expression")
136
+
137
+ Same goes for the deletion of an expression.
138
+
139
+ $ session.delete_expression("some entity id", "some value name", "to be deleted expression")
74
140
 
75
141
  ## Contributing
76
142
 
77
- I am a beginner developer so do contribute or help as much as possible! I definitely need to learn a lot :).
143
+ I am a beginner developer so do contribute or help as much as possible! I definitely need to learn a lot :). Whoever helps will also have there name put here below this line. Amazing!
144
+ _________________________________
145
+
78
146
 
79
147
  1. Fork it
80
148
  2. Create your feature branch (`git checkout -b my-new-feature`)
data/lib/wit_ruby.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'net/http'
2
2
  require 'net/https'
3
3
  require 'multi_json'
4
-
4
+ require 'ostruct'
5
5
 
6
6
  require "wit_ruby/version"
7
7
  require "wit_ruby/rest/client"
@@ -11,3 +11,4 @@ require "wit_ruby/rest/message"
11
11
  require "wit_ruby/rest/intent"
12
12
  require "wit_ruby/rest/entity"
13
13
  require "wit_ruby/rest/expression"
14
+ require "wit_ruby/rest/bodyjson"
@@ -0,0 +1,136 @@
1
+ ## bodyjson.rb
2
+ ## Wrapper for JSON data that will be sent over to API
3
+ ## TODO - better seraching for specific hash
4
+
5
+ module Wit
6
+ module REST
7
+ class BodyJson < OpenStruct
8
+
9
+ ## Allows for reading for values instance variable
10
+ attr_reader :values
11
+
12
+ ## Mainly generates instance variable to store values.
13
+ ##
14
+ ## @param possible_hash [Hash] used for OpenStruct if necessary
15
+ def initialize(possible_hash=nil)
16
+ ## Initialize instance variable for values
17
+ if( !possible_hash.nil? && possible_hash.has_key?("values") )
18
+ @values = possible_hash["values"]
19
+ ## Delete values from it and pass it to OpenStruct constructor
20
+ new_hash = possible_hash.clone
21
+ deleted_value = new_hash.delete("values")
22
+ new_hash_to_os = new_hash
23
+ else
24
+ @values = Array.new
25
+ new_hash_to_os = possible_hash
26
+ end
27
+ super(new_hash_to_os)
28
+ end
29
+
30
+ ## TODO - include metadata
31
+ ## Used to add value for an entity
32
+ ##
33
+ ## @param value [String] a possible value
34
+ ## @param args [Array] posible expressions for the given value
35
+ ## @return [Wit::REST::BodyJson] the current BodyJson with new value.
36
+ def add_value(value, *args)
37
+ ## Check to see if the value already exists
38
+ @values.each do |value_hash|
39
+ if value_hash["value"] == value
40
+ raise ValueAlreadyExists.new(%(The current value being inserted, "#{value}", already exists.))
41
+ end
42
+ end
43
+ ## Adds it if it isn't there with the given expressions
44
+ @values << {"value" => value,
45
+ "expressions" => args
46
+ }
47
+ return self
48
+ end
49
+
50
+ ## Used to add an expression given a value.
51
+ ##
52
+ ## @param value [String] value that will have a new expression
53
+ ## @param args [Array] new expressions for the value
54
+ ## @return [Wit::REST::BodyJson] returns self with added expression to value
55
+ def add_expression(value, *args)
56
+ ## Look for it, and insert new expressions if found.
57
+ ## If not found, raise error
58
+ @values.each do |value_hash|
59
+ if value_hash["value"] == value ## Found it and insert.
60
+ ## Set union for arrays, removes duplicates
61
+ value_hash["expressions"] = value_hash["expressions"] | args
62
+ else ## Not found and raise error
63
+ raise NotFound.new(%(The value, "#{value}", cannot be found.))
64
+ end
65
+ end
66
+
67
+ return self
68
+ end
69
+
70
+ ## Overide values to instead go into OpenStruct to work directly on the instance.
71
+ ##
72
+ ## @param newValues [Array] new values as an array.
73
+ def values=(newValues)
74
+ @values = newValues
75
+ end
76
+
77
+
78
+ ## Used to convert the current hash to JSON
79
+ ##
80
+ ## @return [String] JSON string of the hash
81
+ def json
82
+ ## Use the current to_h method and MultiJson convert it
83
+ MultiJson.dump(self.to_h)
84
+
85
+ end
86
+
87
+ ## Used to overide current to_h method for OpenStruct. Returns a hash
88
+ ## with string equivalent for symbols and adds the current instance variable
89
+ ## @values to it.
90
+ ##
91
+ ## @return [Hash] converted hash of the given BodyJson
92
+ def to_h
93
+ ## Use to_h on OpenStruct to get the current hash in the OpenStruct inheritance
94
+ ## Depending on version of ruby, to_h might not be supported, so instead use table method
95
+ ## to get the table instance variable
96
+ current_os_hash = self.table
97
+ ## Convert symbols to strings
98
+ converted_hash = current_os_hash.reduce({}) do |memo, (k, v)|
99
+ memo.merge({ k.to_s => v})
100
+ end
101
+
102
+ ## Merge values instance to this converted hash.
103
+ converted_hash["values"] = self.values
104
+
105
+ ## Return it.
106
+ return converted_hash
107
+ end
108
+
109
+ ## Used to properly convert the value in instance to JSON specifically for value calls.
110
+ ##
111
+ ## @return [String] JSON string of the one hash value.
112
+ def one_value_to_json
113
+ ## Array of one hash, and convert it to JSON
114
+ MultiJson.dump(@values[0])
115
+ end
116
+
117
+ ## Used to properly convert the first expression of the given value to JSON.
118
+ ##
119
+ ## @return [String] JSON string of the expression.
120
+ def one_expression_to_json(value_add)
121
+ @values.each do |value|
122
+ if value["value"] == value_add
123
+ ## Generate new hash with the given first expression and dump it.
124
+ expression_hash = {"expression" => value["expressions"][0]}
125
+ return MultiJson.dump(expression_hash)
126
+ end
127
+ end
128
+ end
129
+
130
+
131
+ end
132
+
133
+ class ValueAlreadyExists < Exception; end
134
+ class NotFound < Exception; end
135
+ end
136
+ end
@@ -64,9 +64,14 @@ module Wit
64
64
  method_rest_class = Net::HTTP.const_get rest_method.to_s.capitalize
65
65
 
66
66
  ## Define the actual method for Wit::Session:Client
67
- define_method rest_method do |path|#|path, params|
67
+ define_method rest_method do |path, params=nil|
68
68
  request = method_rest_class.new path, {"Authorization" => "Bearer #{@auth_token}"}
69
- #request.set_form_data(params)#params if [:post, :put].include?(rest_method)
69
+ ## If post or put, set content-type to be JSON
70
+ if [:post, :put].include?(rest_method)
71
+ request.body = params
72
+ request["Content-Type"] = "application/json"
73
+ request["Accept"] = "application/vnd.wit.20160202+json"
74
+ end
70
75
  return connect_send(request)
71
76
  end
72
77
  end
@@ -88,12 +93,14 @@ module Wit
88
93
  private
89
94
 
90
95
  ## Setup the session that allows for calling of each method.
96
+ ##
91
97
  def setup_session
92
98
  @session = Wit::REST::Session.new(self)
93
99
  end
94
100
 
95
101
  ## Used to setup a connection using Net::HTTP object when making requests
96
102
  ## to the API.
103
+ ##
97
104
  def setup_conn
98
105
 
99
106
  ## Setup connection through the @conn instance variable and proxy if
@@ -109,6 +116,7 @@ module Wit
109
116
  end
110
117
 
111
118
  ## Setup SSL for the given connection in @conn.
119
+ ##
112
120
  def setup_ssl
113
121
  @conn.use_ssl = @params[:use_ssl]
114
122
  if @params[:ssl_verify_peer]
@@ -132,12 +140,13 @@ module Wit
132
140
  begin
133
141
  ## Save last response and depending on the response, return back the
134
142
  ## given body as a hash.
143
+ #binding.pry if request.method == "POST"
135
144
  response = @conn.request request
136
145
  @last_response = response
137
146
  case response.code
138
147
  when "200" then save_result_and_return(request, response)
139
148
  when "401" then raise Unauthorized, "Incorrect token or not set. Set ENV[\"WIT_AI_TOKEN\"] or pass into the options parameter as :token"
140
- else raise BadResponse, "response code: #{response.code}"
149
+ else raise BadResponse, "response code: #{response.code} #{response.body}"
141
150
  end
142
151
 
143
152
  end
@@ -159,6 +168,8 @@ module Wit
159
168
 
160
169
  end
161
170
 
171
+ ## Raised when response code is not 200 or 401.
172
+ ##
162
173
  class BadResponse < Exception; end
163
174
  end
164
175
  end
@@ -32,5 +32,44 @@ module Wit
32
32
  end
33
33
 
34
34
  end
35
+
36
+ ## Wrapper for array of entities as strings. Inherits from Results so it can
37
+ ## be refreshed.
38
+ ## TODO - Propagate these methods into Result
39
+ class EntityArray < Result
40
+
41
+ ## Generates instance variable that holds list of entities as strings in array.
42
+ ##
43
+ ## @param resultData [Hash] data from the call.
44
+ ## @param requestRest [String] rest code for the call.
45
+ ## @param requestPath [String] request path for the call.
46
+ ## @param requestBody [Hash] body of the call.
47
+ ## @return [Wit::REST::MultiIntent] object that holds the array of intents as result objects.
48
+ def initialize(resultData, requestRest=nil, requestPath=nil, requestBody=nil)
49
+ ## Pass in empty hash to default to method missing for everything not defined here.
50
+ super({}, requestRest, requestPath, requestBody)
51
+ @entities = resultData
52
+ end
53
+
54
+ ## Overide that assists in calling the proper index in the array of entity strings.
55
+ ##
56
+ ## @param num [Integer] index of the array of Strings
57
+ ## @return [Wit::REST::Result] specific entity at the index given.
58
+ def [](num)
59
+ @entities[num]
60
+ end
61
+
62
+ ## Assists in going through each entity string in the instance variable.
63
+ ##
64
+ ## @return [String] lambda that provides each specific entity id.
65
+ def each
66
+ @entities.each do |entity|
67
+ lambda{entity}
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+
35
74
  end
36
75
  end
@@ -37,8 +37,8 @@ module Wit
37
37
  ## If it is then we can return the given value.
38
38
  ## If not, then go to method_missing in Wit::REST::Result.
39
39
  ##
40
- ## @params [Symbol] possible method or key in the hash or entity.
41
- ## @return [String] [Integer] [Hash] depending on the given results in the data.
40
+ ## @param possible_key [Symbol] possible method or key in the hash or entity.
41
+ ## @return [Class] depending on the given results in the data.
42
42
  def method_missing(possible_key, *args, &block)
43
43
  if @rawdata["outcome"]["entities"].has_key?(possible_key.to_s)
44
44
  entity_value = @rawdata["outcome"]["entities"][possible_key.to_s]
@@ -74,8 +74,8 @@ module Wit
74
74
  ## If it is then we can return the given value.
75
75
  ## If not, then raise a NoMethodError.
76
76
  ##
77
- ## @params [Symbol] possible method or key in the hash
78
- ## @return [String] [Integer] [Hash] depending on the given results in the data.
77
+ ## @param possible_key [Symbol] possible method or key in the hash
78
+ ## @return [Class] depending on the given results in the data.
79
79
  def method_missing(possible_key, *args, &block)
80
80
  @rawdata.has_key?(possible_key.to_s) ? @rawdata[possible_key.to_s] : super
81
81
  end
@@ -18,7 +18,7 @@ module Wit
18
18
  def send_message(message)
19
19
  ## Recieve unwrapped results
20
20
  results = @client.get("/message?q=#{message}")
21
- return Message.new(results.raw_data, results.restCode, results.restPath, results.restBody)
21
+ return return_with_class(Wit::REST::Message, results)
22
22
  end
23
23
 
24
24
  ## POST - extract meaning from a audio file
@@ -38,7 +38,7 @@ module Wit
38
38
  def get_message(message_id)
39
39
  results = @client.get("/messages/#{message_id}")
40
40
 
41
- return Message.new(results.raw_data, results.restCode, results.restPath, results.restBody)
41
+ return return_with_class(Wit::REST::Message, results)
42
42
  end
43
43
 
44
44
  ## GET - returns either a list of intents if no id is given.
@@ -54,7 +54,7 @@ module Wit
54
54
 
55
55
  ## Same concept but wrap it around proper object
56
56
  returnObject = intent_indicator.nil? ? MultiIntent : Intent
57
- return returnObject.new(results.raw_data, results.restCode, results.restPath, results.restBody)
57
+ return return_with_class(returnObject, results)
58
58
 
59
59
  end
60
60
 
@@ -65,42 +65,88 @@ module Wit
65
65
  ## - returns the specific entity and its parameters with a given id.
66
66
  ## TODO - notify Wit.ai to fix their documentations as there is a wrong
67
67
  ## - description.
68
- def entities(entity_id = nil)
68
+ ## @param entity_id [String] entity id for specific retrieval
69
+ ## @return [Wit::REST::EntityArray] [Wit::REST::Entity] results and returned in either wrapper
70
+ def get_entities(entity_id = nil)
71
+ ## No specific id, so get list of entities
72
+ results = entity_id.nil? ? @client.get("/entities") : @client.get("/entities/#{entity_id}")
73
+
74
+ ## Same concept but wrap it properly if neccessary.
75
+ returnObject = entity_id.nil? ? EntityArray : Entity
76
+
77
+ return return_with_class(returnObject, results)
69
78
 
70
79
  end
71
80
 
72
81
  ## POST - creates a new entity with the given attributes.
82
+ ##
83
+ ## @param new_entity [Wit::REST::BodyJson] object with data to be sent over to API.
84
+ ## @return [Wit::REST::Result] results of the posting of the new entity.
73
85
  def create_entity(new_entity)
86
+ if new_entity.id.nil?
87
+ raise NotCorrectSchema.new("The current BodyJson object passed in does not have an \"id\" defined.")
88
+ end
89
+ return @client.post("/entities", new_entity.json)
74
90
  end
75
91
 
76
- ## PUT - updates a given entity with the specific entity id.
77
- def update_entity(entity_id)
92
+ ## TODO - notify Wit.ai to return back the updated entity results.
93
+ ## PUT - updates a given entity with the specific entity id and BodyJson data.
94
+ ##
95
+ ## @param entity_id [String] entity id that will be updated.
96
+ ## @param update_entity_data [Wit::REST::BodyJson] new data that will update the entity.
97
+ ## @return [Wit::REST::Result] results of updating the entity
98
+ def update_entity(entity_id, update_entity_data)
99
+ return @client.put("/entities/#{entity_id}", update_entity_data.json)
78
100
  end
79
101
 
80
102
  ## DELETE - deletes the given entity with the entity id.
103
+ ##
104
+ ## @param entity_id [String] entity id that is going to be deleted.
105
+ ## @return [Wit::REST::Result] results of the deletion of the entity
81
106
  def delete_entity(entity_id)
107
+ return @client.delete("/entities/#{entity_id}")
82
108
  end
83
109
 
84
110
  ## POST - adds the possible value into the list of values for the given
85
111
  ## - entity with the id.
86
- def add_value(entity_id, new_value)
87
-
112
+ ## TODO - restrict to only one value in BodyJson
113
+ ## TODO - notify wit.ai that documentation is off.
114
+ ##
115
+ ## @param new_value_with_entity [Wit::REST::BodyJson] includes the new value and entity name as ID.
116
+ ## @return [Wit::REST::Result] the results of the addition of the value
117
+ def add_value(new_value_with_entity)
118
+ return @client.post("/entities/#{new_value_with_entity.id}/values", new_value_with_entity.one_value_to_json)
88
119
  end
89
120
 
90
121
  ## DELETE - deletes the value from the list of values in the entity with
91
122
  ## - with the given value.
92
- def delete_value(entity_id, delete_value)
93
-
123
+ ## @param entity_name [String] name of entity that will have value deleted
124
+ ## @param delete_value [String] name of value to be deleted
125
+ ## @return [Wit::REST::Result] results of the deletion of the value.
126
+ def delete_value(entity_name, delete_value)
127
+ return @client.delete("/entities/#{entity_name}/values/#{delete_value}")
94
128
  end
95
129
 
96
130
  ## POST - adds a new expression to the value of the entity.
97
- def add_expression(entity_id, value, new_expression)
98
-
131
+ ##
132
+ ## @param new_expression_with_id_and_value [Wit::REST::BodyJson] includes new expression for said ID and value.
133
+ ## @return [Wit::REST::Result] results of the addition of the expression
134
+ def add_expression(new_expression_with_id_and_value)
135
+ ## Rename it for better reading
136
+ new_express = new_expression_with_id_and_value
137
+ ## Get the value that will had the expression inserted
138
+ value = new_express.values[0]["value"]
139
+ return @client.post("/entities/#{new_express.id}/values/#{value}/expressions", new_express.one_expression_to_json(value))
99
140
  end
100
141
 
101
- ## DELETE - deleetes the expression in the value of the entity.
142
+ ## DELETE - deletes the expression in the value of the entity.
143
+ ##
144
+ ## @param entity_id [String] entity id that will have the expression deleted.
145
+ ## @param value [String] value name that will have the expression deleted.
146
+ ## @param expression [String] expression that will be deleted.
147
+ ## @return [Wit::REST::Result] results of the deletion of the given expression.
102
148
  def delete_expression(entity_id, value, expression)
103
-
149
+ return @client.delete("/entities/#{entity_id}/values/#{value}/expressions/#{expression}")
104
150
  end
105
151
 
106
152
  ## Used to refresh the results from the given results. Only applicable to result objects that directly
@@ -118,7 +164,7 @@ module Wit
118
164
  raise NotRefreshable.new(%(The inputted object with class "#{result.class}" is not refreshable.))
119
165
  end
120
166
  refreshed_result = @client.request_from_result(result.restCode, result.restPath, result.restBody)
121
- return result_class.new(refreshed_result.raw_data, refreshed_result.restCode, refreshed_result.restPath, refreshed_result.restBody)
167
+ return return_with_class(result_class, refreshed_result)
122
168
  end
123
169
 
124
170
  ## Used to refresh the last response given from the last request.
@@ -130,8 +176,23 @@ module Wit
130
176
  return @client.request_from_result(last_result.restCode, last_result.restPath, last_result.restBody)
131
177
  end
132
178
 
179
+ private
180
+ ## Used to return using the given return class and results.
181
+ ##
182
+ ## @param return_class return class for the specific method.
183
+ ## @param results [Wit::REST::Result] holding the specific results from client.
184
+ def return_with_class(return_class, results)
185
+ return return_class.new(results.raw_data, results.restCode, results.restPath, results.restBody)
186
+ end
187
+
133
188
  end
134
189
 
190
+ ## Raised when the given result object cannot be refreshed.
191
+ ##
135
192
  class NotRefreshable < Exception; end
193
+
194
+ ## Raised when the given result object does not have required parameters.
195
+ ##
196
+ class NotCorrectSchema < Exception; end
136
197
  end
137
198
  end
@@ -1,3 +1,3 @@
1
1
  module Wit
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,75 @@
1
+ ## bodyjson_spec.rb
2
+ ## Used to test class that assists in posting / putting JSON data to API
3
+
4
+ require 'spec_helper'
5
+
6
+ describe Wit::REST::BodyJson do
7
+ let(:id) {"some ID"}
8
+ let(:doc) {"some documentation"}
9
+ let(:value_1) {"some random value 1"}
10
+ let(:value_2) {"some random value 2"}
11
+ let(:express_1) {"some expression 1"}
12
+ let(:express_2) {"some expression 2"}
13
+ let(:json) {%({
14
+ "id": "#{id}",
15
+ "doc": "#{doc}",
16
+ "values": [
17
+ {
18
+ "value": "#{value_1}",
19
+ "expressions": ["#{express_1}"]
20
+ },
21
+ {
22
+ "value": "#{value_2}",
23
+ "expressions": ["#{express_2}"]
24
+ }
25
+ ]
26
+ }
27
+ )}
28
+ let(:eql_hash) {{"doc"=> doc,
29
+ "id" => id,
30
+ "values"=>
31
+ [{"value"=> value_1,
32
+ "expressions"=>[express_1]}, {"value" => value_2, "expressions" => [express_2]}]}}
33
+ let(:new_body) {Wit::REST::BodyJson.new}
34
+ let(:new_body_with_value) {Wit::REST::BodyJson.new({"values" => "a"})}
35
+ let(:new_body_with_one_value) {Wit::REST::BodyJson.new.add_value(value_1)}
36
+ let(:new_body_with_one_expression) {new_body_with_one_value.add_expression(value_1, express_1)}
37
+
38
+ before do
39
+ new_body.id = id
40
+ new_body.doc = doc
41
+ new_body.add_value(value_1)
42
+ new_body.add_expression(value_1, express_1)
43
+ new_body.add_value(value_2, express_2)
44
+ end
45
+
46
+ it "should have the correct values in id and doc" do
47
+ expect(new_body.id).to eql(id)
48
+ expect(new_body.doc).to eql(doc)
49
+ end
50
+
51
+ it "should have the right values and expressions" do
52
+ expect(new_body.values).to eql([{"value" => value_1, "expressions" => [express_1]}, {"value" => value_2, "expressions" => [express_2]}])
53
+ end
54
+
55
+ it "should be able to generate JSON" do
56
+ expect(new_body.json).to eql(MultiJson.dump(MultiJson.load(json)))
57
+ end
58
+
59
+ it "should be able to generate a hash with no symbols" do
60
+ expect(new_body.to_h).to eql(eql_hash)
61
+ end
62
+
63
+ it "should be able to remove values from the initial hash if given and be put in instance" do
64
+ expect(new_body_with_value.instance_variable_get("@values")).to eql("a")
65
+ end
66
+
67
+ it "should provide a method to provide JSON for a hash rather than array for a single value" do
68
+ expect(new_body_with_one_value.one_value_to_json).to eql("{\"value\":\"some random value 1\",\"expressions\":[]}")
69
+ end
70
+
71
+ it "should provide a method to provide JSON for the first expression of a given value" do
72
+ expect(new_body_with_one_expression.one_expression_to_json(value_1)).to eql(%({"expression":"#{express_1}"}))
73
+ end
74
+
75
+ end
@@ -47,3 +47,23 @@ describe Wit::REST::MultiEntity do
47
47
  end
48
48
 
49
49
  end
50
+
51
+ describe Wit::REST::EntityArray do
52
+ let(:json_three) {%([
53
+ "wit$amount_of_money",
54
+ "wit$contact",
55
+ "wit$datetime",
56
+ "wit$on_off",
57
+ "wit$phrase_to_translate",
58
+ "wit$temperature"
59
+ ])}
60
+ let(:entity_array) {Wit::REST::EntityArray.new(MultiJson.load(json_three))}
61
+
62
+
63
+ it "should be each transversable and be strings" do
64
+ entity_array.each do |entity_sring|
65
+ expect(entity_string.class).to eql(String)
66
+ end
67
+ end
68
+
69
+ end
@@ -10,6 +10,7 @@ describe Wit::REST::Session do
10
10
  let(:rand_hash) {{"a" => "a", "b" => "b"}}
11
11
  let(:randSession) {Wit::REST::Session.new(randClient)}
12
12
  let(:session) {Wit::REST::Client.new.session}
13
+ random_name = (0...10).map { ('a'..'z').to_a[rand(26)] }.join
13
14
 
14
15
 
15
16
  ## Testing for method response
@@ -34,15 +35,15 @@ describe Wit::REST::Session do
34
35
  randSession.should respond_to(:get_intents)
35
36
  end
36
37
 
37
- it ".entities(entity_id = nil)" do
38
- randSession.should respond_to(:entities)
38
+ it "get.entities(entity_id = nil)" do
39
+ randSession.should respond_to(:get_entities)
39
40
  end
40
41
 
41
42
  it ".create_entity(new_entity)" do
42
43
  randSession.should respond_to(:create_entity)
43
44
  end
44
45
 
45
- it ".update_entity(entity_id)" do
46
+ it ".update_entity(entity_id, update_entity_data)" do
46
47
  randSession.should respond_to(:update_entity)
47
48
  end
48
49
 
@@ -50,7 +51,7 @@ describe Wit::REST::Session do
50
51
  randSession.should respond_to(:delete_entity)
51
52
  end
52
53
 
53
- it ".add_value(entity_id, new_value)" do
54
+ it ".add_value(entity_name, new_value)" do
54
55
  randSession.should respond_to(:add_value)
55
56
  end
56
57
 
@@ -58,7 +59,7 @@ describe Wit::REST::Session do
58
59
  randSession.should respond_to(:delete_value)
59
60
  end
60
61
 
61
- it ".add_expression(entity_id, value, new_expression)" do
62
+ it ".add_expression(new_expression_with_id_and_value)" do
62
63
  randSession.should respond_to(:add_expression)
63
64
  end
64
65
 
@@ -183,4 +184,191 @@ describe Wit::REST::Session do
183
184
 
184
185
  end
185
186
 
187
+
188
+ describe "getting entities" do
189
+ let(:get_entities) {session.get_entities}
190
+ let(:get_entity) {session.get_entities(get_entities[0])}
191
+
192
+ before do
193
+ VCR.insert_cassette 'get_entities', record: :new_episodes
194
+ end
195
+ after do
196
+ VCR.eject_cassette
197
+ end
198
+
199
+ describe "get list of entities" do
200
+
201
+ it "should return an array of used entities as strings" do
202
+ expect(get_entities.class).to eql(Wit::REST::EntityArray)
203
+ expect(get_entities[0].class).to eql(String)
204
+ end
205
+
206
+ end
207
+
208
+ describe "get specific entity from id" do
209
+
210
+
211
+ it "should return an entity class with same id" do
212
+ expect(get_entity.class).to eql(Wit::REST::Entity)
213
+ ## TODO - tell WIT.AI there documentation is wrong as they have name and id switched.
214
+ expect(get_entity.name).to eql(get_entities[0].split("$")[1])
215
+ end
216
+
217
+ end
218
+ end
219
+
220
+ describe "posting, deleting, updating entities" do
221
+ let(:json) {%({
222
+ "doc": "A city that I like",
223
+ "id": "#{random_name}",
224
+ "values": [
225
+ {
226
+ "value": "Paris",
227
+ "expressions": ["Paris", "City of Light", "Capital of France"]
228
+ }
229
+ ]
230
+ })}
231
+ let(:no_id_hash) {{"doc" => "AMAZING", "values" => [
232
+ {
233
+ "value" => "Paris",
234
+ "expressions" => ["Paris", "City of Light", "Capital of France"]
235
+ }
236
+ ] }}
237
+ let(:new_body) {Wit::REST::BodyJson.new(MultiJson.load(json))}
238
+ let(:no_id_body){Wit::REST::BodyJson.new(no_id_hash)}
239
+ let(:resulting_post) {session.create_entity(new_body)}
240
+ let(:resulting_post_name) {resulting_post.name}
241
+ let(:resulting_post_id) {resulting_post.id}
242
+
243
+
244
+ before do
245
+ VCR.insert_cassette 'post_update_delete_entity', record: :new_episodes
246
+ end
247
+ after do
248
+ VCR.eject_cassette
249
+ end
250
+ it "should raise error when the BodyJson does not have an ID parameter" do
251
+ expect{session.create_entity(no_id_body)}.to raise_error(Wit::REST::NotCorrectSchema)
252
+ end
253
+
254
+ it "should pass and return Result" do
255
+ expect(resulting_post.class).to eql(Wit::REST::Result)
256
+ end
257
+
258
+ describe "updating entities" do
259
+ let(:json_two) {%({
260
+ "doc": "A city that I like",
261
+ "values": [
262
+ {
263
+ "value": "Paris",
264
+ "expressions": ["Paris", "City of Light", "Capital of France"]
265
+ },
266
+ {
267
+ "value": "Seoul",
268
+ "expressions": ["Seoul", "Kimchi paradise"],
269
+ "metadata":"city_343"
270
+ }
271
+ ]
272
+ })}
273
+ let(:update_body) {Wit::REST::BodyJson.new(MultiJson.load(json_two))}
274
+ let(:resulting_update) {session.update_entity(resulting_post_name, update_body)}
275
+
276
+ it "should return a Result class" do
277
+ expect(resulting_update.class).to eql(Wit::REST::Result)
278
+ end
279
+ end
280
+
281
+ describe "deleting entities" do
282
+ let(:resulting_delete) {session.delete_entity(resulting_post_name)}
283
+
284
+ it "should pass and return Result with the same deleted id" do
285
+ expect(resulting_delete.class).to eql(Wit::REST::Result)
286
+ expect(resulting_delete.deleted).to eql(resulting_post_id)
287
+ end
288
+
289
+ end
290
+
291
+ end
292
+
293
+
294
+ describe "creating and deleting value for an entity" do
295
+ let(:entity_json) {
296
+ %({"id": "#{random_name}"})
297
+ }
298
+ let(:entity_body) {Wit::REST::BodyJson.new(MultiJson.load(entity_json))}
299
+ #let(:resulting_add_value) {session.add_value(@resulting_entity_name, with_value_added_body)}
300
+
301
+
302
+ before :each do
303
+ VCR.insert_cassette 'add_delete_value', record: :new_episodes
304
+ @resulting_entity = session.create_entity(entity_body)
305
+ @resulting_entity_name = @resulting_entity.name
306
+ @resulting_value_name = @resulting_entity_name
307
+ @body_for_value_insert = Wit::REST::BodyJson.new(id: @resulting_entity_name)
308
+ @body_with_id_and_value = @body_for_value_insert.add_value(@resulting_value_name)
309
+ @resulting_add_value = session.add_value(@body_with_id_and_value)
310
+ @resulting_delete_value = session.delete_value(@resulting_entity_name, @resulting_value_name)
311
+ end
312
+ after :each do
313
+ session.delete_entity(@resulting_entity_name)
314
+ VCR.eject_cassette
315
+ end
316
+ describe "creation" do
317
+ it "should return a Result class after creation" do
318
+ expect(@resulting_add_value.class).to eql(Wit::REST::Result)
319
+ end
320
+
321
+ it "should have the new value inserted" do
322
+ expect(@resulting_add_value.values[0]["value"]).to eql(@resulting_value_name)
323
+ end
324
+ end
325
+
326
+ describe "deletion" do
327
+ it "should return a Result class after deletion" do
328
+ expect(@resulting_delete_value.class).to eql(Wit::REST::Result)
329
+ end
330
+
331
+ it "should have the value deleted" do
332
+ expect(@resulting_delete_value.deleted).to eql(@resulting_value_name)
333
+ end
334
+ end
335
+ end
336
+
337
+ describe "creating and deleting expression from a value" do
338
+ let(:entity_body) {Wit::REST::BodyJson.new(id: random_name)}
339
+ before :each do
340
+ VCR.insert_cassette 'add_delete_expression', record: :new_episodes
341
+ entity_creation = session.create_entity(entity_body)
342
+ @entity_value_expression_name = entity_creation.name
343
+ session.add_value(Wit::REST::BodyJson.new(id: @entity_value_expression_name).add_value(@entity_value_expression_name))
344
+ @expression_creation = session.add_expression(Wit::REST::BodyJson.new(id: @entity_value_expression_name).add_value(@entity_value_expression_name).add_expression(@entity_value_expression_name, @entity_value_expression_name))
345
+ @expression_deletion = session.delete_expression(@entity_value_expression_name, @entity_value_expression_name, @entity_value_expression_name)
346
+ end
347
+ after :each do
348
+ session.delete_value(@entity_value_expression_name, @entity_value_expression_name)
349
+ session.delete_entity(@entity_value_expression_name)
350
+ VCR.eject_cassette
351
+ end
352
+
353
+ describe "addition" do
354
+ it "should return a Result class" do
355
+ expect(@expression_creation.class).to eql(Wit::REST::Result)
356
+ end
357
+ it "should have the expression added" do
358
+ expect(@expression_creation.values[0]["expressions"][0]).to eql(@entity_value_expression_name)
359
+ end
360
+ end
361
+
362
+ describe "deletion" do
363
+ it "should return a Result class" do
364
+ expect(@expression_deletion.class).to eql(Wit::REST::Result)
365
+ end
366
+ it "should have deleted as the expression name" do
367
+ expect(@expression_deletion.deleted).to eql(@entity_value_expression_name)
368
+ end
369
+ end
370
+
371
+ end
372
+
373
+
186
374
  end
data/wit_ruby.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["gavinchingy@gmail.com"]
11
11
  spec.description = %q{Provides a Ruby API wrapper with the Wit.ai API.}
12
12
  spec.summary = %q{Provides a Ruby API wrapper with the Wit.ai API.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/gching/wit_ruby"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.required_ruby_version = ">= 1.8.7"
21
+ spec.required_ruby_version = ">= 1.9.3"
22
22
  spec.add_dependency('multi_json', '>= 1.3.0')
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wit_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-15 00:00:00.000000000 Z
11
+ date: 2014-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -210,6 +210,7 @@ files:
210
210
  - Rakefile
211
211
  - conf/cacert.pem
212
212
  - lib/wit_ruby.rb
213
+ - lib/wit_ruby/rest/bodyjson.rb
213
214
  - lib/wit_ruby/rest/client.rb
214
215
  - lib/wit_ruby/rest/entity.rb
215
216
  - lib/wit_ruby/rest/expression.rb
@@ -219,6 +220,7 @@ files:
219
220
  - lib/wit_ruby/rest/session.rb
220
221
  - lib/wit_ruby/version.rb
221
222
  - spec/spec_helper.rb
223
+ - spec/wit_ruby/rest/bodyjson_spec.rb
222
224
  - spec/wit_ruby/rest/client_spec.rb
223
225
  - spec/wit_ruby/rest/entity_spec.rb
224
226
  - spec/wit_ruby/rest/expression_spec.rb
@@ -227,7 +229,7 @@ files:
227
229
  - spec/wit_ruby/rest/result_spec.rb
228
230
  - spec/wit_ruby/rest/session_spec.rb
229
231
  - wit_ruby.gemspec
230
- homepage: ''
232
+ homepage: https://github.com/gching/wit_ruby
231
233
  licenses:
232
234
  - MIT
233
235
  metadata: {}
@@ -239,7 +241,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
241
  requirements:
240
242
  - - ">="
241
243
  - !ruby/object:Gem::Version
242
- version: 1.8.7
244
+ version: 1.9.3
243
245
  required_rubygems_version: !ruby/object:Gem::Requirement
244
246
  requirements:
245
247
  - - ">="
@@ -253,6 +255,7 @@ specification_version: 4
253
255
  summary: Provides a Ruby API wrapper with the Wit.ai API.
254
256
  test_files:
255
257
  - spec/spec_helper.rb
258
+ - spec/wit_ruby/rest/bodyjson_spec.rb
256
259
  - spec/wit_ruby/rest/client_spec.rb
257
260
  - spec/wit_ruby/rest/entity_spec.rb
258
261
  - spec/wit_ruby/rest/expression_spec.rb
@@ -260,3 +263,4 @@ test_files:
260
263
  - spec/wit_ruby/rest/message_spec.rb
261
264
  - spec/wit_ruby/rest/result_spec.rb
262
265
  - spec/wit_ruby/rest/session_spec.rb
266
+ has_rdoc: