barrymore 0.1.0

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: 46a6ce64d4da08b2adc438ab6f02ca73b918f7ee
4
+ data.tar.gz: 6295c842ad2bbd6a21cbab24a41db5ee0b2b796b
5
+ SHA512:
6
+ metadata.gz: 52e11fc0c07b69ca085414e3dd4899244a6e4fe896d547faaf126375bb8a561bcbe5cc88cc357b7ceb0894cf8c79decd318bc6cb666b6fe5ef50280907eeeafe
7
+ data.tar.gz: 1284d90ec8fd22372fd147a798f03e30ea8b3ee4be4e73aa539bde208a5d39f7e4d3e334a3c16792ffff4a8a0abfb5f63685c146556fe099a319094a5a41ed41
data/.gitignore ADDED
@@ -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
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in barrymore.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Dmitriy Non
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.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # Barrymore
2
+
3
+ Barrymore - is a DSL for defining chat bot commands (like telegram bot
4
+ commands).
5
+ To create telegram bot you will need to use bot api gem (like `telegram-bot-ruby`).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'barrymore'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install barrymore
22
+
23
+ ## Usage
24
+
25
+ This is an example of usage Barrymore for telegram bot (using gem [`telegram-bot-ruby`](https://github.com/atipugin/telegram-bot-ruby).
26
+ But Barrymore can be used for whatever chats you want.
27
+ For example, [specs](spec/barrymore_spec.rb) are written without any
28
+ chat at all.
29
+
30
+
31
+ ```ruby
32
+ require 'telegram/bot'
33
+ require 'barrymore'
34
+
35
+ class MyBot < Telegram::Bot::Client
36
+ include Barrymore
37
+
38
+ #
39
+ # /start
40
+ #
41
+
42
+ define_command('/start') do |msg|
43
+ if chat_data(msg)[:started_at]
44
+ api.send_message(chat_id: msg.chat, text: 'We met already')
45
+ else
46
+ set_chat_data(msg, started_at: Time.now)
47
+ api.send_message(chat_id: msg.chat, text: "Hello, #{msg.data[:name]}!")
48
+ end
49
+ end
50
+
51
+ #
52
+ # /google
53
+ #
54
+
55
+ define_command('/google') do |msg|
56
+ start_command_processing(msg)
57
+ api.send_message(chat_id: msg.chat, text: 'What do you want to google?')
58
+ end
59
+
60
+ define_command_continuation('/google') do |msg|
61
+ stop_command_processing(msg)
62
+ api.send_message(chat_id: msg.chat, text: "http://lmgtfy.com/?q=#{URI.escape msg.text}")
63
+ end
64
+
65
+ # msg [Barrymore::Message]
66
+ def process_message(msg)
67
+ if command_in_progress?(msg)
68
+ continue_command(msg)
69
+ elsif command_defined?(msg)
70
+ execute_command(msg)
71
+ else
72
+ api.send_message(chat_id: msg.chat, text: 'wrong command!')
73
+ end
74
+ end
75
+
76
+ # converts telegram message to barrymore message
77
+ def berrymore_message(telegram_message)
78
+ Message.new text: telegram_message.text,
79
+ chat: telegram_message.chat.id,
80
+ data: { name: telegram_message.from.first_name }
81
+ end
82
+ end
83
+
84
+ token = raise 'REPLACE IT WITH YOUR TOKEN'
85
+
86
+ MyBot.run(token) do |bot|
87
+ bot.listen do |telegram_message|
88
+ barrymore_msg = bot.berrymore_message(telegram_message)
89
+ bot.process_message(barrymore_msg)
90
+ end
91
+ end
92
+ ```
93
+
94
+ ## Development
95
+
96
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
97
+
98
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
99
+
100
+ ## Contributing
101
+
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Nondv/barrymore.
103
+
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
108
+
data/Rakefile ADDED
@@ -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
data/barrymore.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'barrymore/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'barrymore'
8
+ spec.version = Barrymore::VERSION
9
+ spec.authors = ['Dmitriy Non']
10
+ spec.email = ['non.dmitriy@gmail.com']
11
+
12
+ spec.summary = 'Barrymore - DSL for defining chat bot commands (like telegram bot commands)'
13
+ spec.homepage = 'https://github.com/Nondv/barrymore'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.13'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'rubocop', '0.46.0'
27
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'barrymore'
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
data/bin/setup ADDED
@@ -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
data/lib/barrymore.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'barrymore/version'
2
+ require 'barrymore/message'
3
+ require 'barrymore/class_methods'
4
+ require 'barrymore/instance_methods'
5
+
6
+ #
7
+ # Barrymore is a DSL for defining chat bots commands.
8
+ # Especially useful for Telegram bots.
9
+ #
10
+ module Barrymore
11
+ class UndefinedCommandError < StandardError; end
12
+ class NoCommandInProgressError < StandardError; end
13
+
14
+ # It is used for storing commands. You don't need it
15
+ class BotCommand
16
+ attr_reader :name, :execution
17
+ attr_accessor :continuation
18
+
19
+ def initialize(name, &block)
20
+ raise(ArgumentError, 'no block given') unless block
21
+ @name = name.freeze
22
+ @execution = block
23
+ end
24
+ end
25
+
26
+ def self.included(klass)
27
+ klass.include InstanceMethods
28
+ klass.extend ClassMethods
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ module Barrymore
2
+ #
3
+ # Class-level DSL
4
+ #
5
+ module ClassMethods
6
+ def define_command(name, &block)
7
+ raise(ArgumentError, 'no block given') unless block
8
+ barrymore_commands[name] = BotCommand.new(name, &block)
9
+ end
10
+
11
+ def define_command_continuation(name, &block)
12
+ raise(ArgumentError, 'no block given') unless block
13
+ raise(UndefinedCommandError, name) unless command_defined?(name)
14
+ get_barrymore_command(name).continuation = block
15
+ end
16
+
17
+ # arg [Message|String]
18
+ def command_defined?(arg)
19
+ name = arg.is_a?(String) ? arg : arg.text
20
+ barrymore_commands.key?(name)
21
+ end
22
+
23
+ #
24
+ # You don't need private methods.
25
+ # They are only for barrymore implementation
26
+ #
27
+
28
+ private
29
+
30
+ # arg [Message|String]
31
+ def get_barrymore_command(arg)
32
+ name = arg.is_a?(String) ? arg : arg.text
33
+ barrymore_commands[name]
34
+ end
35
+
36
+ def barrymore_commands
37
+ @barrymore_commands ||= {}
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,71 @@
1
+ module Barrymore
2
+ #
3
+ # Instance-level helpers
4
+ #
5
+ module InstanceMethods
6
+ # command should be specified in msg.text.
7
+ # chat should be specified in msg.chat.
8
+ def execute_command(msg)
9
+ raise(UndefinedCommandError, msg.text) unless command_defined?(msg)
10
+ instance_exec(msg, &get_barrymore_command(msg).execution)
11
+ end
12
+
13
+ def start_command_processing(msg)
14
+ # TODO: use chat_data ?
15
+ commands_in_progress[msg.chat] = msg.text
16
+ end
17
+
18
+ def stop_command_processing(msg)
19
+ commands_in_progress.delete(msg.chat)
20
+ end
21
+
22
+ # arg [Object] - arg.chat or arg itself will be used
23
+ def set_chat_data(arg, data)
24
+ chat = arg.respond_to?(:chat) ? arg.chat : arg
25
+ barrymore_chats_data[chat] = chat_data(arg).merge(data).freeze
26
+ end
27
+
28
+ # arg [Object] - arg.chat or arg itself will be used
29
+ def chat_data(arg)
30
+ chat = arg.respond_to?(:chat) ? arg.chat : arg
31
+ barrymore_chats_data[chat] ||= {}.freeze
32
+ end
33
+
34
+ def continue_command(msg)
35
+ raise(NoCommandInProgressError, msg.chat.to_s) unless command_in_progress?(msg)
36
+ proc = get_barrymore_command(commands_in_progress[msg.chat]).continuation
37
+ instance_exec(msg, &proc)
38
+ end
39
+
40
+ def command_defined?(*args)
41
+ self.class.command_defined?(*args)
42
+ end
43
+
44
+ def command_in_progress?(msg)
45
+ commands_in_progress.key?(msg.chat)
46
+ end
47
+
48
+ def commands_in_progress
49
+ @commands_in_progress ||= {}
50
+ end
51
+
52
+ #
53
+ # You don't need private methods.
54
+ # They are only for barrymore implementation
55
+ #
56
+
57
+ private
58
+
59
+ def barrymore_chats_data
60
+ @barrymore_chats_data ||= {}
61
+ end
62
+
63
+ def barrymore_commands
64
+ self.class.send :barrymore_commands
65
+ end
66
+
67
+ def get_barrymore_command(*args)
68
+ self.class.send(:get_barrymore_command, *args)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,17 @@
1
+ module Barrymore
2
+ # Barrymore implementation of message.
3
+ # You could use it or provide yours with the same behavior
4
+ class Message
5
+ attr_reader :chat, :text, :data
6
+
7
+ def initialize(chat:, text:, data: nil)
8
+ self.chat = chat.freeze
9
+ self.text = text.freeze
10
+ self.data = data.freeze if data
11
+ end
12
+
13
+ private
14
+
15
+ attr_writer :chat, :text, :data
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Barrymore
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barrymore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitriy Non
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-12 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.46.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.46.0
69
+ description:
70
+ email:
71
+ - non.dmitriy@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - barrymore.gemspec
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/barrymore.rb
87
+ - lib/barrymore/class_methods.rb
88
+ - lib/barrymore/instance_methods.rb
89
+ - lib/barrymore/message.rb
90
+ - lib/barrymore/version.rb
91
+ homepage: https://github.com/Nondv/barrymore
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.7
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Barrymore - DSL for defining chat bot commands (like telegram bot commands)
115
+ test_files: []