ibm-watson-ruby 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 987b93d5b8b4632720831f78dc5ecbe016633648
4
+ data.tar.gz: f499c7861a98ab27974a30b53888b5dc7dcd7998
5
+ SHA512:
6
+ metadata.gz: 7fcac6af6ef7871ba2845434a06111c0b6c8e6456558786cf583bdbfbb71ad9bdf431ab83d1757654b79c56992ddf4869a506f95359a33f4597159e7ffaf030b
7
+ data.tar.gz: 759a838669ecec44667e05258f6c50a1547cb4b42ae02313a8ebece7ba8fa7c6a6f9a800b8641f9d86882e8d4e5cdaa6d7f920c5db4d0b7fb29763e938d17a53
@@ -0,0 +1,96 @@
1
+ # Created by .ignore support plugin (hsz.mobi)
2
+ ### Rails template
3
+ *.rbc
4
+ capybara-*.html
5
+ .rspec
6
+ /log
7
+ /tmp
8
+ /db/*.sqlite3
9
+ /db/*.sqlite3-journal
10
+ /public/system
11
+ /coverage/
12
+ /spec/tmp
13
+ **.orig
14
+ rerun.txt
15
+ pickle-email-*.html
16
+
17
+ # TODO Comment out these rules if you are OK with secrets being uploaded to the repo
18
+ config/initializers/secret_token.rb
19
+ config/secrets.yml
20
+
21
+ # dotenv
22
+ # TODO Comment out this rule if environment variables can be committed
23
+ .env
24
+
25
+ ## Environment normalization:
26
+ /.bundle
27
+ /vendor/bundle
28
+
29
+ # these should all be checked in to normalize the environment:
30
+ # Gemfile.lock, .ruby-version, .ruby-gemset
31
+
32
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
33
+ .rvmrc
34
+
35
+ # if using bower-rails ignore default bower_components path bower.json files
36
+ /vendor/assets/bower_components
37
+ *.bowerrc
38
+ bower.json
39
+
40
+ # Ignore pow environment settings
41
+ .powenv
42
+
43
+ # Ignore Byebug command history file.
44
+ .byebug_history
45
+ ### Ruby template
46
+ *.gem
47
+ *.rbc
48
+ /.config
49
+ /coverage/
50
+ /InstalledFiles
51
+ /pkg/
52
+ /spec/reports/
53
+ /spec/examples.txt
54
+ /test/tmp/
55
+ /test/version_tmp/
56
+ /tmp/
57
+
58
+ # Used by dotenv library to load environment variables.
59
+ # .env
60
+
61
+ ## Specific to RubyMotion:
62
+ .dat*
63
+ .repl_history
64
+ build/
65
+ *.bridgesupport
66
+ build-iPhoneOS/
67
+ build-iPhoneSimulator/
68
+
69
+ ## Specific to RubyMotion (use of CocoaPods):
70
+ #
71
+ # We recommend against adding the Pods directory to your .gitignore. However
72
+ # you should judge for yourself, the pros and cons are mentioned at:
73
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
74
+ #
75
+ # vendor/Pods/
76
+
77
+ ## Documentation cache and generated files:
78
+ /.yardoc/
79
+ /_yardoc/
80
+ /doc/
81
+ /rdoc/
82
+
83
+ ## Environment normalization:
84
+ /.bundle/
85
+ /vendor/bundle
86
+ /lib/bundler/man/
87
+
88
+ # for a library or gem, you might want to ignore these files since the code is
89
+ # intended to run in multiple environments; otherwise, check them in:
90
+ # Gemfile.lock
91
+ # .ruby-version
92
+ # .ruby-gemset
93
+
94
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
95
+ .rvmrc
96
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'http'
6
+ gem 'active_attr'
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ibm-watson-ruby (0.0.1)
5
+ active_attr (~> 0.10)
6
+ http (~> 2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ active_attr (0.10.0)
12
+ activemodel (>= 3.0.2, < 5.1)
13
+ activesupport (>= 3.0.2, < 5.1)
14
+ activemodel (4.2.8)
15
+ activesupport (= 4.2.8)
16
+ builder (~> 3.1)
17
+ activesupport (4.2.8)
18
+ i18n (~> 0.7)
19
+ minitest (~> 5.1)
20
+ thread_safe (~> 0.3, >= 0.3.4)
21
+ tzinfo (~> 1.1)
22
+ addressable (2.5.0)
23
+ public_suffix (~> 2.0, >= 2.0.2)
24
+ builder (3.2.3)
25
+ diff-lcs (1.3)
26
+ domain_name (0.5.20170223)
27
+ unf (>= 0.0.5, < 1.0.0)
28
+ http (2.2.1)
29
+ addressable (~> 2.3)
30
+ http-cookie (~> 1.0)
31
+ http-form_data (~> 1.0.1)
32
+ http_parser.rb (~> 0.6.0)
33
+ http-cookie (1.0.3)
34
+ domain_name (~> 0.5)
35
+ http-form_data (1.0.1)
36
+ http_parser.rb (0.6.0)
37
+ i18n (0.8.1)
38
+ minitest (5.10.1)
39
+ public_suffix (2.0.5)
40
+ rake (0.9.6)
41
+ rspec (3.5.0)
42
+ rspec-core (~> 3.5.0)
43
+ rspec-expectations (~> 3.5.0)
44
+ rspec-mocks (~> 3.5.0)
45
+ rspec-core (3.5.4)
46
+ rspec-support (~> 3.5.0)
47
+ rspec-expectations (3.5.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.5.0)
50
+ rspec-mocks (3.5.0)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.5.0)
53
+ rspec-support (3.5.0)
54
+ rubygems-tasks (0.2.4)
55
+ thread_safe (0.3.6)
56
+ tzinfo (1.2.2)
57
+ thread_safe (~> 0.1)
58
+ unf (0.1.4)
59
+ unf_ext
60
+ unf_ext (0.0.7.2)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ active_attr
67
+ bundler (~> 1.0)
68
+ http
69
+ ibm-watson-ruby!
70
+ rake (~> 0.8)
71
+ rspec (~> 3.3)
72
+ rubygems-tasks (~> 0.2)
73
+
74
+ BUNDLED WITH
75
+ 1.14.4
@@ -0,0 +1,2 @@
1
+ # ibm-watson-ruby
2
+ IBM Watson Services in Ruby
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rake'
4
+ require 'rubygems/tasks'
5
+
6
+ Bundler.setup(:development)
7
+
8
+ task :build do
9
+ Gem::Tasks.new(release: true)
10
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/ibm_watson/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "ibm-watson-ruby"
7
+ gem.version = IBMWatson::VERSION
8
+ gem.summary = "IBM Watson services in ruby"
9
+ gem.description = "."
10
+ gem.license = "MIT"
11
+ gem.authors = ["Bram Whillock"]
12
+ gem.email = "bramski@gmail.com"
13
+ gem.homepage = "https://github.com/imrealtor/watson-ruby"
14
+ gem.required_ruby_version = '~> 2.3'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ['lib']
20
+
21
+ gem.add_dependency "http", "~> 2.0"
22
+ gem.add_dependency "active_attr", '~> 0.10'
23
+
24
+ gem.add_development_dependency 'bundler', '~> 1.0'
25
+ gem.add_development_dependency 'rake', '~> 0.8'
26
+ gem.add_development_dependency 'rspec', '~> 3.3'
27
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
28
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_attr'
2
+
3
+ require 'ibm_watson/version'
4
+ require 'ibm_watson/base_model'
5
+ require 'ibm_watson/errors'
6
+ require 'ibm_watson/conversation'
7
+ require 'ibm_watson/base_service'
@@ -0,0 +1,6 @@
1
+ module IBMWatson
2
+ class BaseModel
3
+ include ActiveAttr::Model
4
+ include ActiveAttr::AttributeDefaults
5
+ end
6
+ end
@@ -0,0 +1,70 @@
1
+ module IBMWatson
2
+ class BaseService
3
+ class << self
4
+ def url(url)
5
+ define_method :service_url do
6
+ url
7
+ end
8
+ end
9
+
10
+ def version(version)
11
+ define_method :service_version do
12
+ version
13
+ end
14
+ end
15
+ end
16
+
17
+ def initialize(username:, password:)
18
+ @username = username
19
+ @password = password
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :username, :password
25
+
26
+ def parse_array(json_string, model_class, root_key)
27
+ array = JSON.parse(json_string).fetch(root_key)
28
+ array.map do |attributes|
29
+ model_class.new attributes
30
+ end
31
+ end
32
+
33
+ def build_url(*paths, query: {})
34
+ url = [service_url, service_version, paths.join('/')].join("/")
35
+ if query.any?
36
+ url += "?" + query.to_query
37
+ end
38
+ url
39
+ end
40
+
41
+ def collection_url(cluster_id, collection_name, resource)
42
+ build_url('solr_clusters', cluster_id, 'solr', collection_name, resource)
43
+ end
44
+
45
+ def accept_json(http_chain)
46
+ http_chain.headers(accept: "application/json")
47
+ end
48
+
49
+ def verify_http_result(result, verify_json: true)
50
+ if result.status.between?(200, 299)
51
+ if verify_json
52
+ verify_no_json_failure(result)
53
+ end
54
+ elsif result.status.between?(400, 499)
55
+ raise IBMWatson::Errors::WatsonRequestError, "Server returned #{result.status} : #{result.reason}"
56
+ end
57
+ end
58
+
59
+ def verify_no_json_failure(result)
60
+ json_data = JSON.parse(result.body)
61
+ if json_data['failure']
62
+ raise IBMWatson::Errors::WatsonError, json_data['failure'].values.first
63
+ end
64
+ end
65
+
66
+ def basic_auth
67
+ HTTP.basic_auth(user: username, pass: password)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,13 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ end
4
+ end
5
+
6
+ require 'conversation/service'
7
+ require 'conversation/context'
8
+ require 'conversation/intent_result'
9
+ require 'conversation/output_result'
10
+ require 'conversation/result'
11
+ require 'conversation/workspace'
12
+ require 'conversation/intent'
13
+ require 'conversation/dialog_node'
@@ -0,0 +1,35 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ class Context < IBMWatson::BaseModel
4
+ attribute :conversation_id
5
+ attribute :dialog_stack, default: [{ dialog_node: "root" }]
6
+ attribute :dialog_turn_counter, default: 1
7
+ attribute :dialog_request_counter, default: 1
8
+ attribute :context, default: {}
9
+ attribute :_node_output_map
10
+
11
+ def as_json(*)
12
+ {
13
+ conversation_id: conversation_id,
14
+ system: {
15
+ dialog_stack: dialog_stack,
16
+ dialog_turn_counter: dialog_turn_counter,
17
+ dialog_request_counter: dialog_request_counter,
18
+ }
19
+ }
20
+ end
21
+
22
+ def from_json(json)
23
+ values = json.deep_symbolize_keys
24
+ self.attributes= {
25
+ conversation_id: values[:conversation_id],
26
+ dialog_stack: values[:system][:dialog_stack],
27
+ dialog_turn_counter: values[:system][:dialog_turn_counter],
28
+ dialog_request_counter: values[:system][:dialog_request_counter],
29
+ _node_output_map: values[:system][:_node_output_map],
30
+ context: values[:context]
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ class DialogNode < IBMWatson::BaseModel
4
+ attribute :goto, type: String
5
+ attribute :output, type: Hash
6
+ attribute :parent, type: String
7
+ attribute :context, type: Hash
8
+ attribute :created, type: String
9
+ attribute :updated, type: String
10
+ attribute :metadata
11
+ attribute :conditions, type: String
12
+ attribute :description, type: String
13
+ attribute :dialog_node, type: String
14
+ attribute :previous_sibling, type: String
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ class Intent < IBMWatson::BaseModel
4
+ attribute :intent, type: String
5
+ attribute :created, type: String
6
+ attribute :updated, type: String
7
+ attribute :examples, type: Array
8
+ attribute :description
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ class IntentResult < IBMWatson::BaseModel
4
+ attribute :intent, type: String
5
+ attribute :confidence, type: Float
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ class OutputResult < IBMWatson::BaseModel
4
+ attribute :log_messages, type: Array
5
+ attribute :text, type: Array
6
+ attribute :nodes_visited, type: Array
7
+ attribute :action, type: String
8
+ attribute :template, type: String
9
+ attribute :choices, type: Hash
10
+ attribute :boolean, type: Hash
11
+ attribute :repeat_input, type: String
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ class Result < IBMWatson::BaseModel
4
+ attribute :intents,
5
+ type: ::IBMWatson::Conversation::IntentResult,
6
+ typecaster: lambda { |values|
7
+ values.map do |value|
8
+ ::IBMWatson::Conversation::IntentResult.new(value)
9
+ end
10
+ }
11
+ attribute :entities
12
+ attribute :input
13
+ attribute :output, type: Hash
14
+ attribute :context, type: Hash
15
+ attribute :alternate_intents
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ class Service < IBMWatson::BaseService
4
+ version "v1"
5
+ url "https://gateway.watsonplatform.net/conversation/api"
6
+ QUERY_VERSION = "2017-02-03"
7
+
8
+ def workspace(workspace_id:, export: false)
9
+ url = build_url('workspaces', workspace_id, query: { version: QUERY_VERSION, export: export})
10
+ result = accept_json(basic_auth).get(url)
11
+ verify_http_result(result)
12
+ IBMWatson::Conversation::Workspace.new.tap do |result_object|
13
+ result_object.from_json(result)
14
+ end
15
+ end
16
+
17
+ def message(workspace_id:, input:, context:, alternate_intents: false)
18
+ url = build_url('workspaces',workspace_id, 'message', query: { version: QUERY_VERSION})
19
+ params = {
20
+ input: {text: input},
21
+ context: context.as_json,
22
+ alternate_intents: alternate_intents
23
+ }
24
+ result = accept_json(basic_auth).post(url, json: params)
25
+ verify_http_result(result)
26
+ IBMWatson::Conversation::Result.new.tap do |result_object|
27
+ result_object.from_json(result)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ module IBMWatson
2
+ module Conversation
3
+ class Workspace < IBMWatson::BaseModel
4
+ attribute :name, type: String
5
+ attribute :description, type: String
6
+ attribute :language, type: String
7
+ attribute :metadata, type: Hash
8
+ attribute :created, type: String
9
+ attribute :updated, type: String
10
+ attribute :workspace_id, type: String
11
+ attribute :status, type: String
12
+ attribute :intents, type: ::IBMWatson::Conversation::Intent,
13
+ typecaster: lambda { |values| values.map { |value| ::IBMWatson::Conversation::Intent.new(value) }}
14
+ attribute :entities, type: Array
15
+ attribute :counterexamples, type: Array
16
+ attribute :dialog_nodes, type: ::IBMWatson::Conversation::DialogNode,
17
+ typecaster: lambda { |values| values.map { |value| ::IBMWatson::Conversation::DialogNode.new(value)}}
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ module IBMWatson
2
+ module Errors
3
+ class WatsonError < StandardError; end
4
+ class WatsonRequestError < WatsonError; end
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module IBMWatson
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ibm-watson-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bram Whillock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_attr
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubygems-tasks
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.2'
97
+ description: "."
98
+ email: bramski@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - README.md
107
+ - Rakefile
108
+ - ibm-watson-ruby.gemspec
109
+ - lib/ibm_watson.rb
110
+ - lib/ibm_watson/base_model.rb
111
+ - lib/ibm_watson/base_service.rb
112
+ - lib/ibm_watson/conversation.rb
113
+ - lib/ibm_watson/conversation/context.rb
114
+ - lib/ibm_watson/conversation/dialog_node.rb
115
+ - lib/ibm_watson/conversation/intent.rb
116
+ - lib/ibm_watson/conversation/intent_result.rb
117
+ - lib/ibm_watson/conversation/output_result.rb
118
+ - lib/ibm_watson/conversation/result.rb
119
+ - lib/ibm_watson/conversation/service.rb
120
+ - lib/ibm_watson/conversation/workspace.rb
121
+ - lib/ibm_watson/errors.rb
122
+ - lib/ibm_watson/version.rb
123
+ homepage: https://github.com/imrealtor/watson-ruby
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - "~>"
134
+ - !ruby/object:Gem::Version
135
+ version: '2.3'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.5.2
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: IBM Watson services in ruby
147
+ test_files: []