hooksler 0.1.4

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: 3e166e86f144cba6b9944841ca0afe92846a4a52
4
+ data.tar.gz: c1a06f4b4f21c65fae1ed52463285fa6d7edbf10
5
+ SHA512:
6
+ metadata.gz: caa0b4970e4854907b9486516811a6221ff43e5516cdcd67c889ece8ebcc314b274bb6e48f4aa721f71b102683798a6a81cb4e60793e0b33c9fa1b06b90191ab
7
+ data.tar.gz: d1ab21581d025131e2725550b978f44e2f45bb82b6359c2f28142612cceb39d05566c09e4e0570e39ee19270a2a0d2203075dd6c5bbfd2f612cd3a3996de8052
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ cache: bundler
3
+ bundler_args: "--binstubs --without documentation --path ../bundle --retry=3 --jobs=3"
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - ruby-head
8
+
9
+ script: bundle exec rspec spec --require spec_helper
10
+
11
+ addons:
12
+ code_climate:
13
+ repo_token: 8742c58d209f105d62e1a8c727f2cca1d147376f940ca1506538a3d8098effd3
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hooksler.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ end
10
+ gem "codeclimate-test-reporter", group: :test, require: nil
@@ -0,0 +1,105 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hooksler (0.1.4)
5
+ hashie
6
+ multi_json
7
+ rack
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ celluloid (0.16.0)
13
+ timers (~> 4.0.0)
14
+ codeclimate-test-reporter (0.4.7)
15
+ simplecov (>= 0.7.1, < 1.0.0)
16
+ coderay (1.1.0)
17
+ diff-lcs (1.2.5)
18
+ docile (1.1.5)
19
+ ffi (1.9.8)
20
+ ffi (1.9.8-x64-mingw32)
21
+ formatador (0.2.5)
22
+ guard (2.12.5)
23
+ formatador (>= 0.2.4)
24
+ listen (~> 2.7)
25
+ lumberjack (~> 1.0)
26
+ nenv (~> 0.1)
27
+ notiffany (~> 0.0)
28
+ pry (>= 0.9.12)
29
+ shellany (~> 0.0)
30
+ thor (>= 0.18.1)
31
+ guard-compat (1.2.1)
32
+ guard-rspec (4.5.1)
33
+ guard (~> 2.1)
34
+ guard-compat (~> 1.1)
35
+ rspec (>= 2.99.0, < 4.0)
36
+ hashie (3.4.2)
37
+ hitimes (1.2.2)
38
+ json (1.8.2)
39
+ listen (2.10.0)
40
+ celluloid (~> 0.16.0)
41
+ rb-fsevent (>= 0.9.3)
42
+ rb-inotify (>= 0.9)
43
+ lumberjack (1.0.9)
44
+ method_source (0.8.2)
45
+ multi_json (1.11.1)
46
+ nenv (0.2.0)
47
+ notiffany (0.0.6)
48
+ nenv (~> 0.1)
49
+ shellany (~> 0.0)
50
+ pry (0.10.1)
51
+ coderay (~> 1.1.0)
52
+ method_source (~> 0.8.1)
53
+ slop (~> 3.4)
54
+ pry (0.10.1-x64-mingw32)
55
+ coderay (~> 1.1.0)
56
+ method_source (~> 0.8.1)
57
+ slop (~> 3.4)
58
+ win32console (~> 1.3)
59
+ rack (1.6.1)
60
+ rack-test (0.6.3)
61
+ rack (>= 1.0)
62
+ rake (10.4.2)
63
+ rb-fsevent (0.9.5)
64
+ rb-inotify (0.9.5)
65
+ ffi (>= 0.5.0)
66
+ rspec (3.2.0)
67
+ rspec-core (~> 3.2.0)
68
+ rspec-expectations (~> 3.2.0)
69
+ rspec-mocks (~> 3.2.0)
70
+ rspec-core (3.2.3)
71
+ rspec-support (~> 3.2.0)
72
+ rspec-expectations (3.2.1)
73
+ diff-lcs (>= 1.2.0, < 2.0)
74
+ rspec-support (~> 3.2.0)
75
+ rspec-mocks (3.2.1)
76
+ diff-lcs (>= 1.2.0, < 2.0)
77
+ rspec-support (~> 3.2.0)
78
+ rspec-support (3.2.2)
79
+ shellany (0.0.1)
80
+ simplecov (0.10.0)
81
+ docile (~> 1.1.0)
82
+ json (~> 1.8)
83
+ simplecov-html (~> 0.10.0)
84
+ simplecov-html (0.10.0)
85
+ slop (3.6.0)
86
+ thor (0.19.1)
87
+ timers (4.0.1)
88
+ hitimes
89
+ win32console (1.3.2)
90
+
91
+ PLATFORMS
92
+ ruby
93
+ x64-mingw32
94
+
95
+ DEPENDENCIES
96
+ bundler (~> 1.7)
97
+ codeclimate-test-reporter
98
+ guard
99
+ guard-rspec
100
+ hooksler!
101
+ pry
102
+ rack-test
103
+ rake (~> 10.0)
104
+ rspec
105
+ simplecov
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 ajieks@vmp.ru
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,66 @@
1
+ [![Code Climate](https://codeclimate.com/github/hooksler/hooksler/badges/gpa.svg)](https://codeclimate.com/github/hooksler/hooksler)
2
+ [![Test Coverage](https://codeclimate.com/github/hooksler/hooksler/badges/coverage.svg)](https://codeclimate.com/github/hooksler/hooksler/coverage)
3
+ [![Build Status](https://travis-ci.org/hooksler/hooksler.svg?branch=master)](https://travis-ci.org/hooksler/hooksler)
4
+ [![Gem Version](https://badge.fury.io/rb/hooksler.svg)](http://badge.fury.io/rb/hooksler)
5
+
6
+ # Hooksler
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'hooksler'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install hooksler
23
+
24
+ ## Usage
25
+
26
+
27
+ routing.rb
28
+
29
+
30
+ ```ruby
31
+ Hooksler::Router.config do
32
+ secret_code 'very_secret_code'
33
+ host_name 'http://example.com'
34
+
35
+ endpoints do
36
+ input 'simple', type: :simple
37
+ input 'newrelic', type: :newrelic
38
+ input 'trello', type: :trello,
39
+ create: false,
40
+ public_key: ENV['TRELLO_KEY'],
41
+ member_token: ENV['TRELLO_TOKEN'],
42
+ board_id: ENV['TRELLO_ID1']
43
+
44
+ output 'black_hole', type: :dummy
45
+ output 'slack_out', type: :slack, url: ENV['SLACK_WEBHOOK_URL'], channel: '#test'
46
+ end
47
+
48
+ route 'simple' => 'slack_out'
49
+ route 'trello' => ['black_hole', 'slack_out']
50
+ route 'newrelic' => ['black_hole', 'slack_out']
51
+ end
52
+ ```
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( https://github.com/hooksler/hooksler/fork )
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hooksler"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hooksler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hooksler"
8
+ spec.version = Hooksler::VERSION
9
+ spec.authors = ["schalexey@gmail.com"]
10
+ spec.email = ["schalexey@gmail.com"]
11
+
12
+ spec.summary = %q{WebHooks multiplexer.}
13
+ spec.description = %q{WebHooks multiplexer, for send notify from One-to-One, Many-to-One, Many-To-Many with preprocessing etc.}
14
+ spec.homepage = "https://github.com/fuCtor/hooksler"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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_dependency 'rack'
24
+ spec.add_dependency 'multi_json'
25
+ spec.add_dependency 'hashie'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.7'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'rspec'
31
+ spec.add_development_dependency 'rack-test', ['>= 0']
32
+ spec.add_development_dependency 'simplecov'
33
+ end
@@ -0,0 +1,19 @@
1
+ require 'hooksler/version'
2
+
3
+ module Hooksler
4
+ # Your code goes here...
5
+
6
+ class Error < StandardError; end
7
+
8
+ autoload :Router, 'hooksler/router'
9
+ autoload :Route, 'hooksler/route'
10
+ autoload :Channel, 'hooksler/channel'
11
+ autoload :Endpoints, 'hooksler/endpoints'
12
+
13
+ autoload :Message, 'hooksler/message'
14
+
15
+ autoload :Application, 'hooksler/application'
16
+ end
17
+
18
+ require 'hooksler/simple_input'
19
+
@@ -0,0 +1,44 @@
1
+ module Hooksler
2
+ class Application
3
+ def self.run
4
+ self.new
5
+ end
6
+
7
+ def initialize
8
+ @root = File.dirname ENV['BUNDLE_GEMFILE']
9
+
10
+ Dir.glob(File.join(@root, 'inputs/*.rb')).each do |file|
11
+ require file
12
+ end
13
+ Dir.glob(File.join(@root, 'outputs/*.rb')).each do |file|
14
+ require file
15
+ end
16
+
17
+ require File.join(@root, 'config', 'routing.rb')
18
+ end
19
+
20
+ def call(env)
21
+ req = Rack::Request.new(env)
22
+ if req.path =~ /\/_endpoints_$/
23
+ ['200', {'Content-Type' => 'application/json'}, [MultiJson.dump(Hooksler::Router.info)]]
24
+ else
25
+ from_instance, routes = Hooksler::Router.resolve_path req.fullpath
26
+ return ['410', {'Content-Type' => 'text/html'}, ['Gone']] unless from_instance
27
+
28
+ messages = [*from_instance.load(req)].compact
29
+
30
+ routes.each do |route|
31
+ messages.each do |message|
32
+ route.process(message)
33
+ end
34
+ end
35
+
36
+ ['200', {'Content-Type' => 'text/plain'}, ['']]
37
+ end
38
+ rescue => e
39
+ puts e
40
+ puts e.backtrace
41
+ ['503', {'Content-Type' => 'text/html'}, [e.to_s]]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,60 @@
1
+ require 'hooksler'
2
+
3
+ module Hooksler
4
+
5
+ module Channel
6
+
7
+ def self.extended(base)
8
+ base.send :include, InstanceMethods
9
+ end
10
+
11
+ def build(params = {})
12
+ self.new(params)
13
+ end
14
+
15
+ def register_channel(type, name)
16
+ fail "name is #{name.class} but must be a Symbol" unless name.is_a?(Symbol)
17
+ @inbound_channel_name = name
18
+ Hooksler::Router.register type, name, self
19
+ end
20
+
21
+ def channel_name
22
+ @inbound_channel_name
23
+ end
24
+
25
+ module InstanceMethods
26
+ def build_message(raw, params = {}, &block)
27
+ Hooksler::Message.new(self.class.channel_name, raw, params, &block)
28
+ end
29
+
30
+ def route_defined(_path)
31
+
32
+ end
33
+ end
34
+
35
+ module Input
36
+ def self.extended(base)
37
+ base.send :extend, Channel
38
+ end
39
+
40
+ def register(name)
41
+ @inbound_channel_name = name
42
+ register_channel :input, name
43
+ end
44
+
45
+ end
46
+
47
+ module Output
48
+ def self.extended(base)
49
+ base.send :extend, Channel
50
+ end
51
+
52
+ def register(name)
53
+ register_channel :output, name
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+
60
+ end
@@ -0,0 +1,57 @@
1
+ require 'digest/sha1'
2
+
3
+ module Hooksler
4
+ class Endpoints
5
+
6
+ def initialize(secret_code)
7
+ @secret_code = secret_code
8
+ @instances = {input: {}, output: {}}
9
+ end
10
+
11
+ def input(name, params = {})
12
+ fail 'endpoint type must be set' unless params.key? :type
13
+
14
+ type = params.delete(:type)
15
+ klass = Hooksler::Router.inbounds[type.to_sym]
16
+ fail "unknown type #{type}" unless klass
17
+
18
+ instance = klass.build(params)
19
+
20
+ @instances[:input][encode_name(name)] = [instance, type, name.to_s]
21
+
22
+ instance.route_defined(path(name)) if instance
23
+
24
+ instance
25
+ end
26
+
27
+ def output(name, params = {})
28
+ fail 'endpoint type must be set' unless params.key? :type
29
+
30
+ type = params.delete(:type)
31
+
32
+ klass = Hooksler::Router.outbounds[type.to_sym]
33
+ fail "unknown output type #{type}" unless klass
34
+ instance = klass.build(params)
35
+
36
+ @instances[:output][name.to_s] = instance
37
+ instance
38
+ end
39
+
40
+ def path(name)
41
+ _k, (_instance, type, in_name) = @instances[:input].detect { |_k, (_instance, type, in_name)| name.to_s == in_name }
42
+
43
+ "/#{type}/#{in_name}/#{encode_name(in_name)}" if _k
44
+ end
45
+
46
+ def resolve(type, key)
47
+ fail 'unknown type #{type}, allowed :in, :out' unless [:input, :output].include?(type)
48
+
49
+ @instances.fetch(type).fetch(key.to_s)
50
+ end
51
+
52
+ def encode_name(name)
53
+ Digest::SHA1.hexdigest "#{name}::#{@secret_code}"
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,34 @@
1
+ require 'hooksler'
2
+ require 'hashie'
3
+
4
+ module Hooksler
5
+ class Message
6
+
7
+ MESSAGE_LEVEL = [ :debug, :info, :warning, :error, :critical ].freeze
8
+
9
+ attr_accessor :user, :title, :message, :params, :level, :url
10
+ attr_reader :raw, :source
11
+
12
+ def initialize(source, payload, opts = {})
13
+ @source = source
14
+ @raw = payload
15
+ @level = :info
16
+ @message = ''
17
+ @title = ''
18
+
19
+ params = Hashie::Mash.new opts
20
+
21
+ [ :user, :title, :message, :level, :url ].each do |s|
22
+ next unless v = params.delete(s)
23
+ send "#{s}=", v
24
+ end
25
+
26
+ yield self if block_given?
27
+ end
28
+
29
+ def level=(val)
30
+ fail "Wrong message level #{val}, allow #{MESSAGE_LEVEL.join(', ')} " unless MESSAGE_LEVEL.include?(val.to_sym)
31
+ @level = val.to_sym
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,39 @@
1
+ require 'hooksler'
2
+
3
+ module Hooksler
4
+ class Route
5
+ attr_reader :from
6
+ attr_reader :to
7
+ attr_reader :name
8
+
9
+ def initialize(name, to, params = {})
10
+ fail "TO must be Hooksler::Channel::Output" unless to.class.is_a? Hooksler::Channel::Output
11
+
12
+ @name = name
13
+ @to = to
14
+ @filters = [*params.delete(:filter)]
15
+ @params = params
16
+ end
17
+
18
+ def process(message)
19
+ return unless @to
20
+ return unless @to.respond_to? :dump
21
+
22
+ message = @filters.inject(message) do |msg, filter|
23
+ next unless validate_message! msg
24
+ filter.call(msg, @params)
25
+ end
26
+
27
+ @to.dump message if validate_message! message
28
+ end
29
+
30
+ private
31
+
32
+ def validate_message!(message)
33
+ return false unless message
34
+ fail 'message object must be inherited from Hooksler::Message' unless message.is_a? Hooksler::Message
35
+ true
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,131 @@
1
+ require 'hooksler'
2
+
3
+ module Hooksler
4
+ class Router
5
+
6
+ class RouteNotFound < Hooksler::Error;
7
+ end
8
+
9
+ @endpoints = nil
10
+ @channels = {input: {}, output: {}}
11
+ VALID_TYPES = [:input, :output].freeze
12
+
13
+ attr_reader :routes
14
+
15
+ def self.register(type, name, klass)
16
+ fail "Unknown type #{type} allow #{VALID_TYPES.join(', ')}" unless VALID_TYPES.include? type
17
+
18
+ case type
19
+ when :input
20
+ fail 'Instance must be extended by Hooksler::Inbound' unless klass.is_a? Hooksler::Channel::Input
21
+ when :output
22
+ fail 'Instance must be extended by Hooksler::Outbound' unless klass.is_a? Hooksler::Channel::Output
23
+ end
24
+
25
+ @channels[type][name.to_sym] = klass
26
+ end
27
+
28
+ def self.resolve_path(*args)
29
+ @instance.resolve_path(*args) if @instance
30
+ end
31
+
32
+ def self.config(&block)
33
+ @instance ||= self.new
34
+ @instance.instance_exec &block if block
35
+ @instance
36
+ end
37
+
38
+ def self.inbounds
39
+ @channels[:input]
40
+ end
41
+
42
+ def self.outbounds
43
+ @channels[:output]
44
+ end
45
+
46
+ def self.info
47
+ info = {}
48
+ @instance.routes.each do |from, to_list|
49
+ path = host_name + @instance.endpoints.path(from)
50
+ info[path] = {from => to_list.map(&:name) }
51
+ end
52
+ info
53
+ end
54
+
55
+ def self.host_name(host=nil)
56
+ if defined? @host_name
57
+ @host_name
58
+ else
59
+ return ENV['HOST_NAME'] if host.nil?
60
+ @host_name = host
61
+ end
62
+ end
63
+
64
+ def host_name(host=nil)
65
+ self.class.host_name(host)
66
+ end
67
+
68
+ def endpoints(&block)
69
+ fail 'secret code not set' unless defined? @secret_code
70
+
71
+ unless @endpoints
72
+ @endpoints = Hooksler::Endpoints.new(@secret_code)
73
+ @endpoints.instance_exec &block if block
74
+ end
75
+ @endpoints
76
+ end
77
+
78
+ def secret_code(code)
79
+ if defined? @secret_code
80
+ @secret_code
81
+ else
82
+ return nil if code.nil?
83
+ @secret_code = code
84
+ end
85
+ end
86
+
87
+
88
+ def route(params)
89
+ fail 'route must be a Hash' unless params.is_a? Hash
90
+
91
+ @routes ||= {}
92
+
93
+ from, to = params.first
94
+
95
+ params.delete from
96
+
97
+ [*from].each do |i|
98
+ fail 'from must be string or symbol' unless i.is_a?(String) || i.is_a?(Symbol)
99
+
100
+ @routes[i.to_sym] ||= []
101
+ @routes[i.to_sym] += [*to].map do |it|
102
+
103
+ fail 'to must be string or symbol' unless it.is_a?(String) || it.is_a?(Symbol)
104
+ output = @endpoints.resolve :output, it
105
+
106
+ fail 'unknown out endpoint' unless output
107
+
108
+ Hooksler::Route.new it, output, params
109
+ end
110
+ end
111
+ @routes
112
+ end
113
+
114
+ def resolve_path(path)
115
+ return if !@endpoints || path.to_s.empty?
116
+
117
+ type, _name, key = path.split('/').select { |s| !(s.nil? || s.empty?) }
118
+
119
+ return unless type && _name && key
120
+
121
+ fail "unknown type #{type}" unless self.class.inbounds.key? type.to_sym
122
+ from_instance, _type, name = @endpoints.resolve :input, key
123
+
124
+ fail RouteNotFound.new "route for #{name} not found" unless @routes.key? name.to_sym
125
+
126
+ [from_instance, @routes[name.to_sym]]
127
+ rescue KeyError
128
+ nil
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,18 @@
1
+ require 'hooksler'
2
+ require 'multi_json'
3
+
4
+ module Hooksler
5
+ class SimpleInput
6
+ extend Hooksler::Channel::Input
7
+ register :simple
8
+
9
+ def initialize(params)
10
+ @params = params
11
+ end
12
+
13
+ def load(request)
14
+ payload = MultiJson.load(request.body.read)
15
+ build_message(payload, payload)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module Hooksler
2
+ module Test
3
+ autoload :Request, 'hooksler/test/request'
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+ require 'rack/request'
2
+ require 'rack/mock'
3
+
4
+ module Hooksler
5
+ module Test
6
+ class Request < Rack::Request
7
+ def self.build(data, opts = {})
8
+ opts[:input] = data if data
9
+ env = Rack::MockRequest.env_for('/', opts.merge(method: 'POST'))
10
+ self.new env
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ if defined? RSpec
17
+
18
+ RSpec.shared_examples 'wrong input' do
19
+
20
+ let(:message) { input.load request }
21
+
22
+ it do
23
+ expect { message }.to_not raise_error
24
+ end
25
+
26
+ it do
27
+ expect( message ).to be_nil
28
+ end
29
+ end
30
+
31
+ RSpec.shared_examples 'correct input' do
32
+
33
+ let(:message) { input.load request }
34
+
35
+ it do
36
+ expect { message }.to_not raise_error
37
+ end
38
+
39
+ it do
40
+ expect( message ).to be_instance_of Hooksler::Message
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Hooksler
2
+ VERSION = "0.1.4"
3
+ end
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hooksler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - schalexey@gmail.com
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
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: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rack-test
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
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: WebHooks multiplexer, for send notify from One-to-One, Many-to-One, Many-To-Many
140
+ with preprocessing etc.
141
+ email:
142
+ - schalexey@gmail.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - Guardfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - bin/console
156
+ - bin/setup
157
+ - hooksler.gemspec
158
+ - lib/hooksler.rb
159
+ - lib/hooksler/application.rb
160
+ - lib/hooksler/channel.rb
161
+ - lib/hooksler/endpoints.rb
162
+ - lib/hooksler/message.rb
163
+ - lib/hooksler/route.rb
164
+ - lib/hooksler/router.rb
165
+ - lib/hooksler/simple_input.rb
166
+ - lib/hooksler/test.rb
167
+ - lib/hooksler/test/request.rb
168
+ - lib/hooksler/version.rb
169
+ homepage: https://github.com/fuCtor/hooksler
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubyforge_project:
189
+ rubygems_version: 2.4.7
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: WebHooks multiplexer.
193
+ test_files: []