wit-ruby 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65f38b09d6036ef26cebf939021a5de5dbf069b4
4
- data.tar.gz: 8e710c72d4a80c6bf6dba6bae98063185480459f
3
+ metadata.gz: ac3ac77f6ced1ee1c8fd03093b35d4746c81c30e
4
+ data.tar.gz: 0cb58a138f3bb3d08ff07f51ae406fd332893c48
5
5
  SHA512:
6
- metadata.gz: 5f78942fae930ab0a8808a94ead5ea91ffa76d569bc732f9fe7cf5dba9c3e6ae0f5057802d1bd3879b438a5c1ad7586372988221166525b66537c8742cadad21
7
- data.tar.gz: 42e0286fb7f1521113b287a659ae6229edf15f9c2dd4a6803cf42e95c9dc4c8aab886844e2afa8d4eaba336b94fc2d2dabbe46c8c916a4e5dcb59fb086370958
6
+ metadata.gz: 9b69a88dd619449ba409d51905b584260c8362e322329c87085f9bec41cc003a018fed95f7681a0a8f2d2d56f501d03ee9f6daa8056a19ca3d8703f865004995
7
+ data.tar.gz: 7f32a52883faf74eaa789a93d27f626e9b1357c56df9b0c57522d396be8a10f167f8481fe2ab5dd42e0547572cdc1f22225b00381d8e9a50b52f9788c4a75eda
data/README.md CHANGED
@@ -3,11 +3,88 @@ wit-ruby
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/wit-ruby.png)](http://badge.fury.io/rb/wit-ruby)
5
5
 
6
+ Easy interface for interacting with the [Wit.ai](http://wit.ai) natural language parsing API.
7
+
8
+ This project will expand as the Wit.ai API expands, but as it stands there's a single endpoint. You can hit this
9
+ endpoint easily with `Wit.message([your message])`, which uses Wit.ai to convert that phrase or sentence into an object
10
+ with an intent, and entities if any are available.
11
+
12
+ You will need to create a Wit.ai account and begin training it.
13
+
6
14
  ## Installation
7
15
 
16
+ ```ruby
17
+ gem 'wit-ruby', '>= 0.0.2'
18
+ ```
19
+
20
+ You'll need to set an environment variable named `WIT_TOKEN` or specify your token with `Wit.token = [your token]`.
21
+
22
+ ```shell
23
+ export WIT_TOKEN=[your token]
24
+ ```
25
+
8
26
 
9
27
  ## Usage
10
28
 
29
+ ```ruby
30
+ result = Wit.message('Hi')
31
+ result.intent # will be Hello with the default Wit instance.
32
+ result.confidence # will be relatively low initially.
33
+ result.entities # will be {}, but if there are entities the can accessed as [:name] or .name
34
+ ```
35
+
36
+ ### Result properties/methods
37
+
38
+ <dl>
39
+
40
+ <dt> raw </dt><dd>
41
+ Raw response hash (parsed JSON)
42
+ </dd>
43
+
44
+ <dt> msgId </dt><dd>
45
+ The unique message id provided back from Wit.
46
+ </dd>
47
+
48
+ <dt> msgBody </dt><dd>
49
+ The original message sent.
50
+ </dd>
51
+
52
+ <dt> intent </dt><dd>
53
+ The intent, as determined by Wit.
54
+ </dd>
55
+
56
+ <dt> confidence </dt><dd>
57
+ The confidence level that Wit determined.
58
+ </dd>
59
+
60
+ <dt> entities </dt><dd>
61
+ Hash of entities, which contain the value, and the start/end position within msgBody.
62
+ </dd>
63
+
64
+ </dl>
65
+
66
+ ### Entity properties/methods
67
+
68
+ <dl>
69
+
70
+ <dt> value </dt><dd>
71
+ The value as determined by Wit (might not be the same as body).
72
+ </dd>
73
+
74
+ <dt> start </dt><dd>
75
+ The start position index from msgBody.
76
+ </dd>
77
+
78
+ <dt> end </dt><dd>
79
+ The end position index from msgBody.
80
+ </dd>
81
+
82
+ <dt> body </dt><dd>
83
+ The actual value as specified in msgBody.
84
+ </dd>
85
+
86
+ </dl>
87
+
11
88
 
12
89
  ## License
13
90
 
@@ -1,3 +1,3 @@
1
1
  module Wit
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -1,30 +1,37 @@
1
- require 'net/http'
1
+ require 'faraday'
2
2
  require 'json'
3
+ require 'ostruct'
3
4
 
4
5
  module Wit
5
6
 
6
- TOKEN = ENV['WIT_TOKEN']
7
+ class << self
8
+ attr_accessor :token
7
9
 
8
- def self.message(message = '')
9
- uri = URI("https://api.wit.ai/message?q=#{URI.escape(message)}")
10
- http = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
11
- req = Net::HTTP::Get.new(uri)
12
- req['Authorization'] = "Bearer #{TOKEN}"
13
- http.request(req)
10
+ def token
11
+ @token || ENV['WIT_TOKEN']
14
12
  end
13
+ end
15
14
 
16
- if http.is_a?(Net::HTTPUnauthorized)
17
- raise Unauthorized, "incorrect token set for Wit::TOKEN set an env for WIT_TOKEN or set Wit::TOKEN manually"
15
+ def self.message(message = '')
16
+ response = connection.get do |req|
17
+ req.headers['Authorization'] = "Bearer #{token}"
18
+ req.url '/message', q: message
18
19
  end
19
20
 
20
- if http.is_a?(Net::HTTPSuccess)
21
- return Response.new(JSON.parse(http.body))
21
+ case response.status
22
+ when 200 then return Result.new JSON.parse(response.body)
23
+ when 401 then raise Unauthorized, "incorrect token set for Wit.token set an env for WIT_TOKEN or set Wit::TOKEN manually"
24
+ else raise BadResponse, "response code: #{response.status}"
22
25
  end
26
+ end
23
27
 
24
- false
28
+ def self.connection
29
+ @connection ||= Faraday.new url: 'https://api.wit.ai' do |faraday|
30
+ faraday.adapter Faraday.default_adapter
31
+ end
25
32
  end
26
33
 
27
- class Response
34
+ class Result
28
35
 
29
36
  attr_reader :raw, :msg_id, :msg_body, :intent, :confidence, :entities
30
37
 
@@ -34,26 +41,25 @@ module Wit
34
41
  @msg_body = hash['msg_body']
35
42
  @intent = hash['outcome']['intent']
36
43
  @confidence = hash['outcome']['confidence']
37
- @entities = {}
44
+ @entities = EntityCollection.new
38
45
  hash['outcome']['entities'].each do |name, entity|
39
- @entities[name.to_sym] = Entity.new(entity)
46
+ @entities.send(:"#{name}=", Entity.new(entity))
40
47
  end
41
48
  end
42
49
 
43
50
  end
44
51
 
45
- class Entity
52
+ class EntityCollection < OpenStruct
46
53
 
47
- attr_reader :start, :end, :value, :body
48
-
49
- def initialize(hash)
50
- @start = hash['start']
51
- @end = hash['end']
52
- @value = hash['value']
53
- @body = hash['body']
54
+ def [](name)
55
+ self.send(name)
54
56
  end
55
57
 
56
58
  end
57
59
 
60
+ class Entity < OpenStruct
61
+ end
62
+
58
63
  class Unauthorized < Exception; end
64
+ class BadResponse < Exception; end
59
65
  end
@@ -6,10 +6,10 @@ describe Wit do
6
6
 
7
7
  it "returns the expected output" do
8
8
  res = Wit.message('deploy master foo to bar')
9
- expect( res ).to be_a Wit::Response
9
+ expect( res ).to be_a Wit::Result
10
10
  expect( res.msg_body ).to eq 'deploy master foo to bar'
11
11
  expect( res.intent ).to eq 'deploy'
12
- expect( res.entities[:branch] ).to be_a Wit::Entity
12
+ expect( res.entities.branch ).to be_a Wit::Entity
13
13
  expect( res.entities[:branch].value ).to eq 'master'
14
14
  end
15
15
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wit-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Jackson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-18 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2013-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.8
13
27
  description: A simple library for interacting with wit.ai -- will expand as the api
14
28
  does
15
29
  email: