stacko 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: b62a1d92c1a77f285fe53695f11d533580c7044b
4
+ data.tar.gz: 974017ed573402b6cfed18e058f230a14a284d40
5
+ SHA512:
6
+ metadata.gz: c35b59995fe7decf5e81e2e41aa52eb347e42487c10445ad18a22fdc707fc98741afeeca0bcbb1adff104a48de8b4598f0e5d20efb92c8bde3543d43f6b5ff58
7
+ data.tar.gz: 2b72e21c8a1791d4ca7a45017e857229a042a10ad2f47cf9c7654039521a7ea23eda5c165ef82da27a42113c4252027cde5a2cd42047ca274a44dbe7d546e010
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stacko.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :minitest, :all_on_start => false do
5
+ # with Minitest::Spec
6
+ watch(%r{^spec/(.*)_spec\.rb})
7
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
8
+ watch(%r{^spec/spec_helper\.rb}) { 'spec' }
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Erik Lott
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,29 @@
1
+ # Stacko
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'stacko'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install stacko
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,38 @@
1
+ module Stacko
2
+ class Application
3
+ def self.builder
4
+ @builder ||= Builder.new
5
+ end
6
+
7
+ def self.map(method_name, &block)
8
+ builder.map(method_name, &block)
9
+ class_eval <<-EOT, __FILE__, __LINE__ + 1
10
+ def #{method_name}(*args) # def get_user(*args)
11
+ execute(:#{method_name}, *args) # execute(:get_user, *args)
12
+ end # end
13
+ EOT
14
+ end
15
+
16
+ def self.run(app)
17
+ builder.run(app)
18
+ end
19
+
20
+ def self.use(middleware, *args, &block)
21
+ builder.use(middleware, *args, &block)
22
+ end
23
+
24
+ def call(*args)
25
+ execute(:call, *args)
26
+ end
27
+
28
+ private
29
+
30
+ def execute(method_name, *args)
31
+ env = {}
32
+ env['METHOD_NAME'] = method_name
33
+ env['ARGUMENTS'] = *args
34
+
35
+ self.class.builder.call(env)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,47 @@
1
+ module Stacko
2
+ class Builder
3
+ def initialize(default_app = nil, &block)
4
+ @run = default_app
5
+ @use = []
6
+ @map = nil
7
+ instance_eval(&block) if block_given?
8
+ end
9
+
10
+ def use(middleware, *args, &block)
11
+ if @map
12
+ mapping = @map
13
+ @map = nil
14
+ @use << proc { |app| generate_map(app, mapping) }
15
+ end
16
+ @use << proc { |app| middleware.new(app, *args, &block) }
17
+ end
18
+
19
+ def run(app)
20
+ @run = app
21
+ end
22
+
23
+ def map(method_name, &block)
24
+ @map ||= {}
25
+ @map[method_name] = block
26
+ end
27
+
28
+ def to_app
29
+ app = @map ? generate_map(@run, @map) : @run
30
+ fail "missing run or map statement" unless app
31
+ app = @use.reverse.inject(app) { |a,e| e[a] }
32
+ app
33
+ end
34
+
35
+ def call(env)
36
+ to_app.call(env)
37
+ end
38
+
39
+ private
40
+
41
+ def generate_map(default_app, mapping)
42
+ mapped = default_app ? {:call => default_app} : {}
43
+ mapping.each { |r,b| mapped[r] = self.class.new(default_app, &b) }
44
+ MethodMap.new(mapped)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,4 @@
1
+ module Stacko
2
+ class StackoError < StandardError; end
3
+ class NoMethodError < StackoError; end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Stacko
2
+ class MethodMap
3
+ def initialize(map = {})
4
+ @map = map
5
+ end
6
+
7
+ def call(env)
8
+ meth = env['METHOD_NAME']
9
+ raise(::Stacko::NoMethodError) unless @map.key?(meth)
10
+ @map[meth].call(env)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module Stacko
2
+ class Request
3
+ def initialize(env)
4
+ @env = env
5
+ end
6
+
7
+ def method_name
8
+ @env['METHOD_NAME']
9
+ end
10
+
11
+ def arguments
12
+ @env['ARGUMENTS']
13
+ end
14
+ end
15
+ end
16
+
17
+
@@ -0,0 +1,3 @@
1
+ module Stacko
2
+ VERSION = "0.0.1"
3
+ end
data/lib/stacko.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Stacko
2
+ end
3
+
4
+ require 'stacko/request'
5
+ require 'stacko/version'
6
+ require 'stacko/request'
7
+ require 'stacko/application'
8
+ require 'stacko/builder'
9
+ require 'stacko/exceptions'
10
+ require 'stacko/method_map'
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Stacko::Application do
4
+ def new_application
5
+ klass = Class.new(Stacko::Application) do
6
+ use BeaverMiddleware
7
+
8
+ map(:method_name_1) do
9
+ use DogMiddleware
10
+ run lambda { |env| env['ARGUMENTS'].push(:meth1); env['ARGUMENTS'] }
11
+ end
12
+
13
+ map(:method_name_2) do
14
+ use HorseMiddleware
15
+ run lambda { |env| env['ARGUMENTS'].push(:meth2); env['ARGUMENTS'] }
16
+ end
17
+
18
+ run lambda { |env| env['ARGUMENTS'].push(:defaultapp); env['ARGUMENTS'] }
19
+ end
20
+ klass.new
21
+ end
22
+
23
+ it 'defined methods for each mapping' do
24
+ app = new_application
25
+ app.respond_to?(:method_name_1)
26
+ app.respond_to?(:method_name_2)
27
+ end
28
+
29
+ it 'responds to call' do
30
+ app = new_application
31
+ app.respond_to?(:call)
32
+ end
33
+
34
+ it 'correctly chains apps' do
35
+ app = new_application
36
+ app.call.must_equal([:beaver_middleware, :defaultapp])
37
+ app.method_name_1.must_equal([:beaver_middleware, :dog_middleware, :meth1])
38
+ app.method_name_2.must_equal([:beaver_middleware, :horse_middleware, :meth2])
39
+ end
40
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ describe Stacko::Builder do
4
+ it 'has implicit #to_app' do
5
+ app = builder do
6
+ run lambda { |env| {:blah => 'blah'} }
7
+ end
8
+
9
+ app.call({}).must_equal({:blah=>"blah"})
10
+ end
11
+
12
+ it 'has explicit #to_app' do
13
+ app = builder do
14
+ run lambda { |env| {:blah => 'blah'} }
15
+ end
16
+
17
+ app.to_app.call({}).must_equal({:blah=>"blah"})
18
+ end
19
+
20
+ it 'chains app by default' do
21
+ app = builder do
22
+ use DogMiddleware
23
+ run lambda { |env| env['ARGUMENTS'].push(:app); env['ARGUMENTS'] }
24
+ end
25
+
26
+ e = new_env(:blah, [])
27
+ app.call(e).must_equal([:dog_middleware, :app])
28
+ end
29
+
30
+ it 'supports method mapping' do
31
+ app = builder do
32
+ map(:method_name_1) do
33
+ run lambda { |env| 'method_name_1_was_called' }
34
+ end
35
+
36
+ map(:method_name_2) do
37
+ run lambda { |env| 'method_name_2_was_called' }
38
+ end
39
+ end
40
+
41
+ env = new_env(:method_name_1)
42
+ app.call(env).must_equal('method_name_1_was_called')
43
+
44
+ env = new_env(:method_name_2)
45
+ app.call(env).must_equal('method_name_2_was_called')
46
+ end
47
+
48
+ it 'responds to call when mappings exist' do
49
+ app = builder do
50
+ map(:method_name_1) do
51
+ run lambda { |env| 'method_name_1_was_called' }
52
+ end
53
+
54
+ run lambda { |env| 'default app called' }
55
+ end
56
+
57
+ env = new_env(:call)
58
+ app.call(env).must_equal('default app called')
59
+ end
60
+
61
+ it 'correctly chains app in mappings' do
62
+ app = builder do
63
+ use BeaverMiddleware
64
+
65
+ map(:method_name_1) do
66
+ use DogMiddleware
67
+ run lambda { |env| env['ARGUMENTS'].push(:meth1); env['ARGUMENTS'] }
68
+ end
69
+
70
+ map(:method_name_2) do
71
+ use HorseMiddleware
72
+ run lambda { |env| env['ARGUMENTS'].push(:meth2); env['ARGUMENTS'] }
73
+ end
74
+
75
+ run lambda { |env| env['ARGUMENTS'].push(:defaultapp); env['ARGUMENTS'] }
76
+ end
77
+
78
+ e = new_env(:call, [])
79
+ app.call(e).must_equal([:beaver_middleware, :defaultapp])
80
+
81
+ e = new_env(:method_name_1, [])
82
+ app.call(e).must_equal([:beaver_middleware, :dog_middleware, :meth1])
83
+
84
+ e = new_env(:method_name_2, [])
85
+ app.call(e).must_equal([:beaver_middleware, :horse_middleware, :meth2])
86
+ end
87
+
88
+ it 'fails if run is missing' do
89
+ app = builder do
90
+ # no run statement
91
+ end
92
+ proc { app.call({}) } .must_raise(RuntimeError)
93
+ end
94
+
95
+ def builder(&block)
96
+ Stacko::Builder.new(&block)
97
+ end
98
+
99
+ def new_env(method_name, arguments=nil)
100
+ {
101
+ 'METHOD_NAME' => method_name,
102
+ 'ARGUMENTS' => arguments
103
+ }
104
+ end
105
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Stacko::MethodMap do
4
+ it 'dispatches map correctly' do
5
+ map = Stacko::MethodMap.new(
6
+ :method_1 => Proc.new{ |env| 'method_1 called' },
7
+ :method_2 => Proc.new{ |env| 'method_2 called' }
8
+ )
9
+
10
+ env = new_env(:method_1)
11
+ map.call(env).must_equal('method_1 called')
12
+
13
+ env = new_env(:method_2)
14
+ map.call(env).must_equal('method_2 called')
15
+ end
16
+
17
+ it 'complains when map not found' do
18
+ map = Stacko::MethodMap.new(
19
+ :method_1 => Proc.new{ |env| 'method_1 called' }
20
+ )
21
+
22
+ env = new_env(:non_existant_method)
23
+ proc { map.call(env) }.must_raise(Stacko::NoMethodError)
24
+ end
25
+
26
+ def new_env(method_name)
27
+ {'METHOD_NAME' => method_name}
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Stacko::Request do
4
+ def request
5
+ Stacko::Request.new(
6
+ 'METHOD_NAME' => :login,
7
+ 'ARGUMENTS' => {:blah => 'blah'}
8
+ )
9
+ end
10
+
11
+ it 'returns arguments' do
12
+ request.arguments.must_equal({:blah => 'blah'})
13
+ end
14
+
15
+ it 'returns method name' do
16
+ request.method_name.must_equal(:login)
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha/setup'
3
+ require 'stacko'
4
+
5
+ def context *args, &block
6
+ describe *args, &block
7
+ end
8
+
9
+ class DogMiddleware
10
+ def initialize(app)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ env['ARGUMENTS'].push(:dog_middleware)
16
+ @app.call(env)
17
+ end
18
+ end
19
+
20
+ class HorseMiddleware
21
+ def initialize(app)
22
+ @app = app
23
+ end
24
+
25
+ def call(env)
26
+ env['ARGUMENTS'].push(:horse_middleware)
27
+ @app.call(env)
28
+ end
29
+ end
30
+
31
+ class BeaverMiddleware
32
+ def initialize(app)
33
+ @app = app
34
+ end
35
+
36
+ def call(env)
37
+ env['ARGUMENTS'].push(:beaver_middleware)
38
+ @app.call(env)
39
+ end
40
+ end
data/stacko.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stacko/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stacko"
8
+ spec.version = Stacko::VERSION
9
+ spec.authors = ["Erik Lott"]
10
+ spec.email = ["erik.lott@kodio.com"]
11
+ spec.description = %q{Stackable message interface for ruby}
12
+ spec.summary = %q{Stackable message interface for ruby}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "mocha", "~> 0.14.0"
24
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stacko
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Erik Lott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-18 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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
+ - !ruby/object:Gem::Dependency
42
+ name: mocha
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.14.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.14.0
55
+ description: Stackable message interface for ruby
56
+ email:
57
+ - erik.lott@kodio.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - Guardfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/stacko.rb
69
+ - lib/stacko/application.rb
70
+ - lib/stacko/builder.rb
71
+ - lib/stacko/exceptions.rb
72
+ - lib/stacko/method_map.rb
73
+ - lib/stacko/request.rb
74
+ - lib/stacko/version.rb
75
+ - spec/application_spec.rb
76
+ - spec/builder_spec.rb
77
+ - spec/method_map_spec.rb
78
+ - spec/request_spec.rb
79
+ - spec/spec_helper.rb
80
+ - stacko.gemspec
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.7
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Stackable message interface for ruby
105
+ test_files:
106
+ - spec/application_spec.rb
107
+ - spec/builder_spec.rb
108
+ - spec/method_map_spec.rb
109
+ - spec/request_spec.rb
110
+ - spec/spec_helper.rb