Skip to content

Commit e14e48a

Browse files
committed
initial
0 parents  commit e14e48a

17 files changed

+305
-0
lines changed

.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/.bundle
2+
3+
/.yardoc
4+
/_yardoc/
5+
/coverage/
6+
/pkg/
7+
8+
# Ignore all logfiles and tempfiles.
9+
/log/*
10+
/tmp/*
11+
!/log/.keep
12+
!/tmp/.keep
13+
mkmf.log
14+
15+
nbproject
16+
.idea
17+
*~
18+
19+
*.swp
20+
*.swap
21+
*.bundle
22+
*.so
23+
*.o
24+
*.a
25+
26+
.vagrant
27+
.ruby-env
28+
.DS_Store
29+
Vagrantfile
30+
Gemfile.lock

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper

Gemfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'activesupport', '>= 5.0.0'
4+
5+
group :development, :test do
6+
gem 'rspec', '~> 3.5.0'
7+
gem 'spring'
8+
gem 'factory_girl'
9+
end
10+
11+
group :test do
12+
gem 'webmock'
13+
gem 'vcr'
14+
end

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2016 dizer
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.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Eureka Bot gem

eureka-bot.gemspec

+23
Original file line numberDiff line numberDiff line change
@@ -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 'eureka_bot/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "eureka-bot"
8+
spec.version = EurekaBot::VERSION
9+
spec.authors = ["dizer"]
10+
spec.email = ["[email protected]"]
11+
spec.summary = %q{Run your messenger bots}
12+
spec.homepage = ""
13+
spec.license = "MIT"
14+
15+
spec.files = `git ls-files -z`.split("\x0")
16+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18+
spec.require_paths = ["lib"]
19+
20+
spec.add_development_dependency "bundler", "~> 1.7"
21+
spec.add_development_dependency "rake", "> 10.0"
22+
spec.add_development_dependency "rspec", "~> 3.5.0"
23+
end

lib/eureka-bot.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
$:.unshift File.dirname(__FILE__)
2+
3+
require 'active_support'
4+
require 'active_support/core_ext'
5+
6+
module EurekaBot
7+
extend ActiveSupport::Autoload
8+
9+
autoload :Version
10+
autoload :Controller
11+
end

lib/eureka_bot/controller.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class EurekaBot::Controller
2+
extend ActiveSupport::Autoload
3+
4+
autoload :Response
5+
autoload :Resolver
6+
7+
attr_reader :params, :message, :logger, :response
8+
9+
def initialize(params: {}, message: nil, response: nil, logger: Logger.new(STDOUT))
10+
@params = params
11+
@message = message
12+
@logger = logger
13+
@response = response || response_class.new(logger: logger)
14+
@response.controller = self
15+
end
16+
17+
def execute(action)
18+
if respond_to?(action, include_all: false)
19+
public_send(action)
20+
else
21+
raise UnknownAction.new("Action #{action} is not defined in #{self.class}")
22+
end
23+
self
24+
end
25+
26+
def answer(params={})
27+
response << params
28+
end
29+
30+
def response_class
31+
EurekaBot::Controller::Response
32+
end
33+
34+
class UnknownAction < StandardError; end
35+
36+
end

lib/eureka_bot/controller/resolver.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class EurekaBot::Controller::Resolver
2+
3+
attr_reader :logger, :message, :response
4+
5+
def initialize(message:, response: nil, logger: Logger.new(STDOUT))
6+
@logger = logger
7+
@message = message
8+
@response = response
9+
end
10+
11+
def resolve
12+
nil
13+
end
14+
15+
def execute
16+
resolved = resolve
17+
raise ActionNotFound.new("Cant resolve path for #{message}") unless resolved
18+
controller = resolved[:controller].new(
19+
params: {},
20+
message: message,
21+
logger: logger,
22+
response: response
23+
)
24+
controller.execute(resolved[:action])
25+
end
26+
27+
class ActionNotFound < StandardError;
28+
end
29+
30+
end

lib/eureka_bot/controller/response.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class EurekaBot::Controller::Response
2+
include ActiveSupport::Callbacks
3+
define_callbacks :add
4+
5+
attr_reader :logger, :data
6+
attr_accessor :controller
7+
8+
def initialize(logger: Logger.new(STDOUT))
9+
@logger = logger
10+
@data = []
11+
end
12+
13+
def add(params={})
14+
run_callbacks :add do
15+
@data << params
16+
end
17+
end
18+
19+
def <<(params={})
20+
add(params)
21+
end
22+
23+
def to_a
24+
data
25+
end
26+
27+
end

lib/eureka_bot/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module EurekaBot
2+
VERSION = '1.0.0'
3+
end

log/.keep

Whitespace-only changes.

spec/eureka-bot/controller_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe EurekaBot::Controller do
4+
5+
context 'response' do
6+
let(:controller) { EurekaBot::Controller.new }
7+
8+
it 'is EurekaBot::Controller::Response subclass' do
9+
expect(controller.response).to be_a_kind_of(EurekaBot::Controller::Response)
10+
end
11+
12+
context '#answer' do
13+
let(:controller) do
14+
Class.new(EurekaBot::Controller) do
15+
def some_action
16+
answer(some_action: 'finished')
17+
end
18+
end.new
19+
end
20+
21+
before do
22+
controller.some_action
23+
end
24+
25+
it do
26+
expect(controller.response.to_a).to include(include(some_action: 'finished'))
27+
end
28+
end
29+
end
30+
end

spec/spec_helper.rb

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
ENV['NEW_RELIC_ENV'] = 'test'
2+
3+
require 'bundler'
4+
Bundler.require
5+
require './lib/eureka-bot'
6+
require 'webmock/rspec'
7+
require 'vcr'
8+
require 'factory_girl'
9+
10+
$:.unshift File.dirname(__FILE__) + '/..'
11+
Dir['spec/support/**/*.rb'].each { |f| require f }
12+
13+
RSpec.configure do |config|
14+
config.include WebRequestHelper
15+
config.include SettingsHelper
16+
config.include FactoryGirl::Syntax::Methods
17+
18+
config.before(:suite) do
19+
FactoryGirl.find_definitions
20+
lint_time = Benchmark.realtime do
21+
factories_for_lint = FactoryGirl.factories.select{|f| f.send(:class_name) != Hash}
22+
FactoryGirl.lint(factories_for_lint)
23+
end
24+
# puts "=== FactoryGirl.lint complete in #{lint_time} seconds ==="
25+
end
26+
27+
config.expect_with :rspec do |expectations|
28+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29+
end
30+
31+
config.mock_with :rspec do |mocks|
32+
mocks.verify_partial_doubles = true
33+
end
34+
35+
config.disable_monkey_patching!
36+
# config.warnings = true
37+
38+
config.profile_examples = 10
39+
config.order = :random
40+
41+
# tag to disable VCR and WebMock
42+
# it 'Real web request', :novcr { ... }
43+
config.around do |example|
44+
if example.metadata[:novcr]
45+
with_web_request do
46+
example.run
47+
end
48+
else
49+
example.run
50+
end
51+
end
52+
53+
Kernel.srand config.seed
54+
end
55+
56+
VCR.configure do |c|
57+
c.cassette_library_dir = 'spec/cassettes'
58+
c.hook_into :webmock
59+
c.configure_rspec_metadata!
60+
end

spec/support/settings_helper.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module SettingsHelper
2+
def settings
3+
@settings ||= YAML.load_file('config/config.yml')['test']
4+
end
5+
end

spec/support/web_request_helper.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module WebRequestHelper
2+
def with_web_request(&block)
3+
previous_allowed = WebMock.net_connect_allowed?
4+
begin
5+
WebMock.allow_net_connect!
6+
VCR.turned_off(&block)
7+
ensure
8+
WebMock.disable_net_connect! unless previous_allowed
9+
end
10+
end
11+
end

tmp/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)