db_bot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e28f734175b47109f6a295b07ef831ff72dcfafe
4
+ data.tar.gz: 81c2d563390a6f9a3a4a3d1dfaca2e319a17869e
5
+ SHA512:
6
+ metadata.gz: d2f6d9ffa8218439018299f0cc692aa92a5b654051a5321db510101b7d1033344fc630245e16df15a065d0761fc2617562192d3eb84cf7568fe94bd4d6f1f5e2
7
+ data.tar.gz: e3290ac11e9e1eb5f259c7b12365205a04d05dfdc9fb2274605cf3060f2ae704c7ebc6653fecc91fd9f26f3c431cf424c4ffc0dc01cf545a3a2e31fdc60dc2ac
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at dawilster143@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bot.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 William Porter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # Bot
2
+
3
+ DB Bot uses Natural Language Processing (NLP) to construct DB queries. What that means is you can interact with your database like you're talking to a human
4
+ **Here's an example**
5
+
6
+ ```ruby
7
+ > bot = DbBot.message('How many users signed up this week?')
8
+ > bot.response
9
+ => "5 users"
10
+ > bot = DbBot.message('Find the last 10 users')
11
+ > bot.response
12
+ => "There you go"
13
+ > bot.collection
14
+ => #<ActiveRecord::Relation [#<User id: 1...
15
+ ```
16
+ It's rather basic at the moment but I'm working on incorporating more complex database queries.
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'db_bot'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install db_bot
33
+
34
+ ## Getting Started
35
+ The only thing you'll need to do after installing `db_bot` is add our **Wit.ai** access token. This gives you access to all the language rules. You can view our project here https://wit.ai/dawilster/db_bot.
36
+
37
+ ```
38
+ WIT_ACCESS_TOKEN=M4GJHCDJLSI34OTZW5M4NPP672IR3UOS
39
+ ```
40
+
41
+ Then you can jump in and asks questions like.
42
+ ```ruby
43
+ > DbBot.message('How many users signed up last week?')
44
+ > DbBot.message('Display the 50 most recent sales')
45
+ > DbBot.message('How many sales does the item with id 103 have?') (coming soon)
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dawilster/db_bot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
51
+
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bot"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'db_bot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "db_bot"
8
+ spec.version = DbBot::VERSION
9
+ spec.authors = ["William Porter"]
10
+ spec.email = ["willports@gmail.com"]
11
+ spec.summary = "Bot to interact with your database using plain english"
12
+ spec.description = "DB Bot uses Natural Language Processing (NLP) to construct DB queries. What that means is you can interact with your database like you're talking to a human"
13
+ spec.homepage = "https://github.com/dawilster"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "activerecord", "~> 4.2.0"
30
+ spec.add_dependency "activesupport", "~> 4.2.0"
31
+ spec.add_dependency "wit", "~> 4.1.0"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.12"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ spec.add_development_dependency "rspec-activemodel-mocks", "~> 1.0"
37
+ spec.add_development_dependency "awesome_print"
38
+ end
@@ -0,0 +1,8 @@
1
+ require "db_bot/version"
2
+ require "db_bot/translate"
3
+
4
+ module DbBot
5
+ def self.message(message)
6
+ Translate.perform(message)
7
+ end
8
+ end
@@ -0,0 +1,109 @@
1
+ require 'wit'
2
+ require 'date'
3
+
4
+ module DbBot
5
+ class Translate
6
+
7
+ attr_accessor :response,
8
+ :when,
9
+ :table,
10
+ :verb,
11
+ :query,
12
+ :number,
13
+ :search_query,
14
+ :target_attribute,
15
+ :collection
16
+
17
+ def self.perform(*args)
18
+ return new(*args).tap { |use_case| use_case.perform }
19
+ end
20
+
21
+ def initialize(message)
22
+ @message = message
23
+ end
24
+
25
+ def perform
26
+ init_wit_client
27
+
28
+ construct_entities
29
+
30
+ process_verb
31
+
32
+ rescue TranslateException => e
33
+ @response = e.message
34
+ end
35
+
36
+ private
37
+
38
+ def init_wit_client
39
+ @client = Wit.new(access_token: 'RAS7ZDEPPJCGRTYONX7Q2UP6JOMY4OF2')
40
+ # @client = Wit.new(access_token: ENV['WIT_ACCESS_KEY'])
41
+ end
42
+
43
+ def construct_entities
44
+ translation = @client.message(@message)
45
+ puts entities = translation['entities']
46
+
47
+ @table = entities['table'][0]['value'] if entities['table']
48
+ @query = entities['query'][0]['value'] if entities['query']
49
+ @verb = entities['verb'][0]['value'] if entities['verb']
50
+ @when = ::DateTime.parse(entities['datetime'][0]['value']) if entities['datetime']
51
+ @number = entities['number'][0]['value'] if entities['number']
52
+ @target_attribute = entities['target_attribute'][0]['value'] if entities['target_attribute']
53
+ @search_query = entities['search_query'][0]['value'] if entities['search_query']
54
+ end
55
+
56
+ def process_verb
57
+ case @verb
58
+ when 'count'
59
+ if @table && @query
60
+ return count_verb
61
+ end
62
+ when 'return'
63
+ return return_verb
64
+ end
65
+
66
+ @response = "Please try again"
67
+ end
68
+
69
+ ## Verbs
70
+
71
+ def count_verb
72
+ find_class
73
+
74
+ if @when
75
+ @response = @class.where('created_at >= ?', @when).size.to_s
76
+ else
77
+ @response = @class.all.size.to_s
78
+ end
79
+ end
80
+
81
+ def return_verb
82
+ find_class
83
+
84
+ if @target_attribute
85
+ @collection = @class.where(@target_attribute.to_sym => @search_query)
86
+ else
87
+ @collection = @class.limit(@number || 100)
88
+ end
89
+
90
+ if @collection.any?
91
+ @response = 'There you go'
92
+ else
93
+ @response = "There weren't any #{@table}"
94
+ end
95
+ end
96
+
97
+ ## Helpers
98
+
99
+ def find_class
100
+ @class = @table.singularize.camelize.constantize
101
+
102
+ rescue NameError
103
+ raise TranslateException, 'Class cannot be found'
104
+ end
105
+
106
+ ## Our own exception
107
+ class TranslateException < StandardError; end
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module DbBot
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - William Porter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.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: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: wit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-activemodel-mocks
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: awesome_print
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: DB Bot uses Natural Language Processing (NLP) to construct DB queries.
126
+ What that means is you can interact with your database like you're talking to a
127
+ human
128
+ email:
129
+ - willports@gmail.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - ".travis.yml"
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - db_bot.gemspec
145
+ - lib/db_bot.rb
146
+ - lib/db_bot/translate.rb
147
+ - lib/db_bot/version.rb
148
+ homepage: https://github.com/dawilster
149
+ licenses:
150
+ - MIT
151
+ metadata:
152
+ allowed_push_host: https://rubygems.org
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.5.1
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Bot to interact with your database using plain english
173
+ test_files: []