wit_bot 0.2.2 → 0.3.0

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: b8f635d90f3d307a4c42932fd9b1d5a12706e3de
4
- data.tar.gz: 3729c86e923d5a135bf7dadfd060543b4f495738
3
+ metadata.gz: b3f3caa74bcc0204ca38bd033d658a6f29d7d214
4
+ data.tar.gz: 8d5abcd72bc96298c507448768b7e7cd71f06b55
5
5
  SHA512:
6
- metadata.gz: 9ec78562804b290ece5323a1d6798319c99b1b75c69cdbf0655ab3b9ba96f88ca04068dc5ecec0a63c63bfb59735a39514cd223282a4fe9be07eadfab7bc0bfa
7
- data.tar.gz: b20aebb47e2f11aeb67a3bb2c314971434d11ecb87c86cf413ce6ff955ac447a495ad88d4abe8608c4a45d1fa9e8258c45ca2ccae71a5d3097f178dfc133c942
6
+ metadata.gz: a40fb576433b153099d0fa3d0ec23617de5ccdc345975683872a65e173f1081f016b2add0e37b0dc6b142276f435ab95dca8f6fb696922bde1423160f6301f2a
7
+ data.tar.gz: ed106aafccb46edc139e9e18ccdf827b0372dcf86a179aa81481ff7d061387d3a5d3fc3217b0bad74b3bf2f00e77ebb1aefbe86cc0180e1c8bbe456e4cd35a3f
data/bin/console CHANGED
@@ -14,4 +14,8 @@ def reload!
14
14
  end
15
15
 
16
16
  require "irb"
17
+
18
+ require "awesome_print"
19
+ AwesomePrint.irb!
20
+
17
21
  IRB.start
data/examples/bot.rb CHANGED
@@ -1,13 +1,6 @@
1
- #require 'wit_bot'
2
-
3
- require_relative '../lib/wit_bot'
4
-
1
+ require_relative 'helpers/helpers'
5
2
  require_relative 'bot/bot'
6
3
 
7
- WitBot.configure do |c|
8
- c.token = ENV['WIT_ACCESS_TOKEN'] # Create a wit bot and set the token here
9
- end
10
-
11
4
  conversation = WitBot::Bot::Conversation::Base.new
12
5
 
13
6
  bot = Bot.new conversation
data/examples/hash.rb ADDED
@@ -0,0 +1,20 @@
1
+ require_relative 'helpers/helpers'
2
+
3
+ old = WitBot::Bot::Conversation::Base.new # Create a conversation
4
+
5
+ old.send_message 'This is a cool test.' # Sent a message
6
+
7
+ hash = old.as_json # Convert conversation to a hash suitable for json use.
8
+
9
+ new = WitBot::Bot::Conversation::Base.from_hash hash # Recreate the conversation from the hash
10
+
11
+ puts 'Hash:'
12
+
13
+ ap hash # Print the hash
14
+
15
+ puts "\nSize: #{hash.to_json.length.to_s :human_size}"
16
+
17
+ puts "\nOld vs New:\n\n"
18
+
19
+ p old # Print old and new on top of each other for comparison.
20
+ p new
@@ -0,0 +1,25 @@
1
+ require_relative 'helpers/helpers'
2
+ require_relative 'bot/bot'
3
+
4
+ old = WitBot::Bot::Conversation::Base.new # Create a conversation
5
+
6
+ Bot.new old
7
+
8
+ old.send_message 'This is a cool test.' # Sent a message
9
+
10
+ hash = old.as_json # Convert conversation to a hash suitable for json use.
11
+
12
+ new = WitBot::Bot::Conversation::Base.from_hash hash # Recreate the conversation from the hash
13
+
14
+ Bot.new new
15
+
16
+ puts 'Hash:'
17
+
18
+ ap hash # Print the hash
19
+
20
+ puts "\nSize: #{hash.to_json.length.to_s :human_size}"
21
+
22
+ puts "\nOld vs New:\n\n"
23
+
24
+ p old # Print old and new on top of each other for comparison.
25
+ p new
@@ -0,0 +1,6 @@
1
+ require_relative '../../lib/wit_bot'
2
+ require 'ap'
3
+
4
+ WitBot.configure do |c|
5
+ c.token = ENV['WIT_ACCESS_TOKEN'] # Create a wit bot and set the token here
6
+ end
data/examples/thread.rb CHANGED
@@ -1,13 +1,4 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'awesome_print'
4
- require 'wit_bot'
5
-
6
- WitBot.configure do |c|
7
- c.token = ENV['WIT_ACCESS_TOKEN'] # Create a wit bot and set the token here
8
- end
9
-
10
- thread = WitBot.thread # Create a new thread
1
+ require_relative 'helpers/helpers'
11
2
 
12
3
  message = thread.create_message 'This is a cool test' # Create a normal message on the thread
13
4
 
data/lib/wit_bot.rb CHANGED
@@ -1,7 +1,4 @@
1
- require 'active_support/core_ext/hash/indifferent_access'
2
- require 'active_support/core_ext/module/attribute_accessors'
3
- require 'active_support/ordered_hash'
4
- require 'active_support/concern'
1
+ require 'active_support/all'
5
2
  require 'http'
6
3
  require 'require_all'
7
4
  require 'observer'
@@ -38,6 +38,21 @@ module WitBot
38
38
  notify_observers :output, message
39
39
  message
40
40
  end
41
+
42
+ def to_hash
43
+ {
44
+ thread: @thread
45
+ }
46
+ end
47
+
48
+ def from_hash(json)
49
+ @thread = WitBot::MessageThread.from_hash json[:thread]
50
+ self
51
+ end
52
+
53
+ def self.from_hash(json)
54
+ self.new.from_hash json.with_indifferent_access
55
+ end
41
56
  end
42
57
  end
43
58
  end
@@ -4,6 +4,13 @@ module WitBot
4
4
  def bot?
5
5
  true
6
6
  end
7
+ def from_hash(json)
8
+ @_text = json[:_text]
9
+ self
10
+ end
11
+ def self.from_hash(thread, id, json)
12
+ self.new(thread, json[:text], id: id).from_hash json
13
+ end
7
14
  def send
8
15
  throw "You can't send a bot message."
9
16
  end
@@ -8,14 +8,14 @@ module WitBot
8
8
  end
9
9
 
10
10
  def empty?
11
- as_json.empty?
11
+ to_hash.empty?
12
12
  end
13
13
 
14
14
  def entities
15
15
  @entities ||= ContextEntities.new
16
16
  end
17
17
 
18
- def as_json
18
+ def to_hash
19
19
  h = {}
20
20
  h[:state] = state if state && !state.empty?
21
21
  h[:reference_time] = reference_time.iso8601 if reference_time
@@ -23,5 +23,16 @@ module WitBot
23
23
  h[:location] = location if location
24
24
  h
25
25
  end
26
+
27
+ def from_hash(h)
28
+ @state = Array.wrap h[:state]
29
+ @reference_time = Date.parse h[:reference_time] if h[:reference_time]
30
+ @entities = ContextEntities.from_hash h[:entities] if h[:entities]
31
+ @location = h[:location]
32
+ self
33
+ end
34
+ def self.from_hash(json)
35
+ self.new.from_hash json
36
+ end
26
37
  end
27
38
  end
@@ -9,10 +9,19 @@ module WitBot
9
9
  def []=(entity, value, expressions=[value])
10
10
  @hash[entity] = value.is_a?(EntityValue) ? value : EntityValue.new(value, expressions)
11
11
  end
12
- def as_json
12
+ def to_hash
13
13
  @hash.map do |id, values|
14
14
  {id: id, values: values}
15
15
  end
16
16
  end
17
+ def from_hash(json)
18
+ json.each do |object|
19
+ self[object[:id]] = EntityValue.new object[:values]
20
+ end
21
+ self
22
+ end
23
+ def self.from_hash(json)
24
+ self.new.from_hash json
25
+ end
17
26
  end
18
27
  end
@@ -15,6 +15,32 @@ module WitBot
15
15
  false
16
16
  end
17
17
 
18
+ def to_hash
19
+ {
20
+ text: @text,
21
+ _text: @_text,
22
+ id: @id,
23
+ bot: bot?,
24
+ sent: @sent,
25
+ _outcomes: @_outcomes
26
+ }
27
+ end
28
+
29
+ def from_hash(json)
30
+ @_text = json[:_text]
31
+ @sent = json[:sent]
32
+ @_outcomes = json[:_outcomes]
33
+ parse_outcomes!
34
+ self
35
+ end
36
+ def self.from_hash(thread, id, json)
37
+ return Bot::Message.from_hash thread, id, json if json[:bot]
38
+ self.new(thread, json[:text], id: id).from_hash json
39
+ end
40
+ def self.many_from_hash(thread, json)
41
+ json.inject({}) { |h, (id, message)| h[id] = self.from_hash thread, id, message; h }
42
+ end
43
+
18
44
  def params(p=nil)
19
45
  params = {
20
46
  q: text,
@@ -33,11 +59,20 @@ module WitBot
33
59
 
34
60
  @_text = response['_text']
35
61
  @_outcomes = response['outcomes']
36
- @outcomes = @_outcomes.each_with_index.map { |outcome, i| Outcome.new self, outcome, i }
62
+
63
+ parse_outcomes!
37
64
 
38
65
  self
39
66
  end
40
67
 
68
+ def parse_outcomes(outcomes=@_outcomes)
69
+ outcomes.each_with_index.map { |outcome, i| Outcome.new self, outcome, i }
70
+ end
71
+
72
+ def parse_outcomes!(outcomes=@_outcomes)
73
+ @outcomes = parse_outcomes outcomes
74
+ end
75
+
41
76
  def outcome
42
77
  outcomes.first
43
78
  end
@@ -8,6 +8,7 @@ module WitBot
8
8
  @id = id
9
9
  @messages = ActiveSupport::OrderedHash.new
10
10
  @bot_messages = ActiveSupport::OrderedHash.new
11
+ @metadata = nil
11
12
  end
12
13
 
13
14
  def message(id)
@@ -35,5 +36,31 @@ module WitBot
35
36
  def reset_context
36
37
  @context = Context.new
37
38
  end
39
+
40
+ def to_hash
41
+ {
42
+ id: @id,
43
+ context: @context,
44
+ messages: {
45
+ bot: @bot_messages,
46
+ user: @messages
47
+ },
48
+ metadata: @metadata
49
+ }
50
+ end
51
+
52
+ def from_hash(json)
53
+ m = json[:messages]
54
+ @messages = WitBot::Message.many_from_hash(self, m[:user])
55
+ @bot_messages = WitBot::Message.many_from_hash(self, m[:bot])
56
+
57
+ @context = Context.from_hash json[:context]
58
+ @metadata = json[:metadata]
59
+ self
60
+ end
61
+
62
+ def self.from_hash(json)
63
+ self.new(json[:id]).from_hash json
64
+ end
38
65
  end
39
66
  end
@@ -16,5 +16,6 @@ module WitBot
16
16
  def to_h
17
17
  {value: value, expressions: expressions}
18
18
  end
19
+ alias :to_hash :to_h
19
20
  end
20
21
  end
@@ -1,7 +1,7 @@
1
1
  module WitBot
2
2
  class MessageRequest < WitRequest
3
3
  def request(message, n)
4
- super http.get '/message', params: message.params(n: n)
4
+ super http.get('/message', params: message.params(n: n))
5
5
  end
6
6
  end
7
7
  end
@@ -5,10 +5,10 @@ module WitBot
5
5
  end
6
6
 
7
7
  def request(request=http)
8
+ response = process_response request.parse
8
9
  if request.code.between? 200, 299
9
- process_response request.parse
10
+ response
10
11
  else
11
- response = request.parse.with_indifferent_access
12
12
  raise WitError.new(response[:status]), (response[:error] || response[:body])
13
13
  end
14
14
  end
@@ -17,7 +17,7 @@ module WitBot
17
17
  def process_response(response)
18
18
  if response.respond_to? :with_indifferent_access
19
19
  response.with_indifferent_access
20
- elsif response.respond_to? :map
20
+ elsif response.is_a? Array
21
21
  response.map {|r| process_response r}
22
22
  else
23
23
  response
@@ -1,3 +1,3 @@
1
1
  module WitBot
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wit_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben (@penne12_)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-15 00:00:00.000000000 Z
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,9 @@ files:
114
114
  - bin/wit
115
115
  - examples/bot.rb
116
116
  - examples/bot/bot.rb
117
+ - examples/hash.rb
118
+ - examples/hash_with_bot.rb
119
+ - examples/helpers/helpers.rb
117
120
  - examples/simple.rb
118
121
  - examples/thread.rb
119
122
  - lib/wit_bot.rb