pandorabots_api 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61885335976b38620f989a31853c455b297a677a
4
+ data.tar.gz: afe9823a97ca49c578380da42fc863ba7ad421e2
5
+ SHA512:
6
+ metadata.gz: f69c8cb4de4bc8c1e58389a598b08ae73f1a613f05b7c16b32c4a02513119624a0d0b774ff910f0c210c2596152f40614dca0ed9ea3f83b0d1a4b0a4be38ee90
7
+ data.tar.gz: 7ab0e1234a7f1edcf0e8a2128e1a45b894a18b6e516325658f7554ec81bcbe6a8a80c1589f094728337fbfd3dcfa965a5d4ed0b0c4d9f2465d97cf72169c3799
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pandorabots.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Spontena LLC
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # pb-ruby
2
+
3
+ Pandorabots API module for Ruby. Please read the [documentation](http://developer.pandorabots.com/docs).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pandorabots_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pandorabots_api
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork it (https://github.com/spontena/pb-ruby/fork )
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,161 @@
1
+ require 'pandorabots/version'
2
+ require 'net/https'
3
+ require 'json'
4
+
5
+ module Pandorabots
6
+ class API
7
+ class << self
8
+ BASE_URL = 'https://aiaas.pandorabots.com'
9
+ FILE_KIND = {
10
+ aiml: 'file', set: 'set', map: 'map', substitution: 'substitution',
11
+ properties: 'properties', pdefaults: 'pdefaults'
12
+ }
13
+
14
+ def create_bot(username, botname, user_key:)
15
+ request_uri = "/bot/#{username}/#{botname}?user_key=#{user_key}"
16
+ put = Net::HTTP::Put.new(URI.escape(request_uri))
17
+ response = https.request(put)
18
+ succeed_creation?(response)
19
+ end
20
+
21
+ def delete_bot(username, botname, user_key:)
22
+ request_uri = "/bot/#{username}/#{botname}?user_key=#{user_key}"
23
+ delete = Net::HTTP::Delete.new(URI.escape(request_uri))
24
+ response = https.request(delete)
25
+ succeed_deletion?(response)
26
+ end
27
+
28
+ def upload_file(username, botname, file,
29
+ file_kind: '', filename: '', user_key:)
30
+ file_kind = file_kind(file) if file_kind.empty?
31
+ filename = filename(file) if filename.empty?
32
+ request_uri = upload_file_uri(username, botname, file_kind,
33
+ filename, user_key)
34
+ put = Net::HTTP::Put.new(URI.escape(request_uri))
35
+ put.body = file.read
36
+ response = https.request(put)
37
+ succeed_upload?(response)
38
+ end
39
+
40
+ def compile_bot(username, botname, user_key:)
41
+ request_uri = "/bot/#{username}/#{botname}/verify?user_key=#{user_key}"
42
+ get = Net::HTTP::Get.new(URI.escape(request_uri))
43
+ response = https.request(get)
44
+ succeed_compilation?(response)
45
+ end
46
+
47
+ def talk(username, botname, input, sessionid: '', reset: false,
48
+ trace: false, recent: true, user_key:)
49
+ request_uri = "/talk/#{username}/#{botname}?input=#{input}" \
50
+ "&sessionid=#{sessionid}&reset=#{reset}&trace=#{trace}" \
51
+ "&user_key=#{user_key}"
52
+ post = Net::HTTP::Post.new(URI.escape(request_uri))
53
+ response = https.request(post)
54
+ TalkResult.new(response.body) if succeed_talk?(response)
55
+ end
56
+
57
+ private
58
+
59
+ def https
60
+ uri = URI(BASE_URL)
61
+ https = Net::HTTP.new(uri.host, uri.port)
62
+ https.use_ssl = true
63
+ https
64
+ end
65
+
66
+ def filename(file)
67
+ File.basename(file, '.*')
68
+ end
69
+
70
+ def file_kind(file)
71
+ extname = File.extname(file).delete('.')
72
+ FILE_KIND[extname.to_sym]
73
+ end
74
+
75
+ def upload_file_uri(username, botname, file_kind, filename, user_key)
76
+ request_uri = "/bot/#{username}/#{botname}/#{file_kind}"
77
+ request_uri << "/#{filename}" if need_filename?(file_kind)
78
+ request_uri << "?user_key=#{user_key}"
79
+ end
80
+
81
+ def need_filename?(file_kind)
82
+ not ['properties', 'pdefaults'].include?(file_kind)
83
+ end
84
+
85
+ def succeed_creation?(response)
86
+ case response.code
87
+ when '200' then true
88
+ when '400' then raise_error_400(response.body)
89
+ when '401' then raise AuthorizationError, response.body
90
+ when '409' then raise BotExistsError, response.body
91
+ else raise Error, response.body
92
+ end
93
+ end
94
+
95
+ def succeed_deletion?(response)
96
+ case response.code
97
+ when '200' then true
98
+ when '400' then raise_error_400(response.body)
99
+ when '401' then raise AuthorizationError, response.body
100
+ else raise Error, response.body
101
+ end
102
+ end
103
+
104
+ def succeed_upload?(response)
105
+ case response.code
106
+ when '200' then true
107
+ when '400' then raise_error_400(response.body)
108
+ when '401' then raise AuthorizationError, response.body
109
+ else raise Error, response.body
110
+ end
111
+ end
112
+
113
+ def succeed_compilation?(response)
114
+ case response.code
115
+ when '200' then true
116
+ when '400' then raise_error_400(response.body)
117
+ when '401' then raise AuthorizationError, response.body
118
+ else raise Error, response.body
119
+ end
120
+ end
121
+
122
+ def succeed_talk?(response)
123
+ case response.code
124
+ when '200' then true
125
+ when '400' then raise_error_400(response.body)
126
+ when '401' then raise AuthorizationError, response.body
127
+ else raise Error, response.body
128
+ end
129
+ end
130
+
131
+ def raise_error_400(body)
132
+ json = JSON.parse(body)
133
+ case json['message']
134
+ when 'Invalid botname' then raise InvalidBotnameError, body
135
+ when 'precondition failed' then raise PreconditionError, body
136
+ when 'Compile errors encountered' then raise CompilationError, body
137
+ else raise Error, body
138
+ end
139
+ end
140
+ end
141
+
142
+ class TalkResult
143
+ attr_reader :response, :sessionid, :trace
144
+
145
+ def initialize(json_str)
146
+ json = JSON.parse(json_str)
147
+ @response = json['responses'] unless json['responses'].nil?
148
+ @sessionid = json['sessionid'] unless json['sessionid'].nil?
149
+ @trace = json['trace'] unless json['trace'].nil?
150
+ end
151
+ end
152
+
153
+ class Error < StandardError; end
154
+ class AuthorizationError < Error; end
155
+ class BotExistsError < Error; end
156
+ class CompilationError < Error; end
157
+ class InvalidBotnameError < Error; end
158
+ class MalformedRequestError < Error; end
159
+ class PreconditionError < Error; end
160
+ end
161
+ end
@@ -0,0 +1,3 @@
1
+ module Pandorabots
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pandorabots/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pandorabots_api"
8
+ spec.version = Pandorabots::VERSION
9
+ spec.authors = ["Takuya Tsuchida"]
10
+ spec.email = ["takuya.tsuchida@spontena.com"]
11
+ spec.summary = %q{Pandorabots API module for Ruby.}
12
+ spec.description = ""
13
+ spec.homepage = "https://github.com/spontena/pb-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pandorabots_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Takuya Tsuchida
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email:
43
+ - takuya.tsuchida@spontena.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/pandorabots.rb
54
+ - lib/pandorabots/version.rb
55
+ - pandorabots.gemspec
56
+ homepage: https://github.com/spontena/pb-ruby
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.0
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Pandorabots API module for Ruby.
80
+ test_files: []