├── .ruby-version ├── Contributors.md ├── Gemfile ├── Gemfile.lock ├── LICENCE ├── README.md ├── config.ru ├── src ├── html_from_template_file.rb ├── rack_http_adapter.rb ├── raters.rb ├── smaller_web_hexagon.rb └── views │ └── result_view.erb └── test ├── file_rater.txt └── test_smaller_web_hexagon.rb /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.1.2 2 | -------------------------------------------------------------------------------- /Contributors.md: -------------------------------------------------------------------------------- 1 | * [Alistair Cockburn](http://alistair.cockburn.us/) 2 | * [Amitai Schlair](http://www.schmonz.com) 3 | * Dave Alvarez Navarro 4 | * [Jen Diamond](https://github.com/jendiamond) 5 | * [Pat Maddox](https://patmaddox.com) 6 | * [Felipe Dornelas](http://felipedornelas.com) 7 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'http://rubygems.org' 2 | 3 | gem 'rack' 4 | gem 'erubis' 5 | 6 | group :test do 7 | gem 'rack-test' 8 | gem 'rspec-expectations' 9 | gem 'test-unit' 10 | end 11 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: http://rubygems.org/ 3 | specs: 4 | diff-lcs (1.2.4) 5 | erubis (2.7.0) 6 | rack (1.5.2) 7 | rack-test (0.6.2) 8 | rack (>= 1.0) 9 | rspec-expectations (2.14.0) 10 | diff-lcs (>= 1.1.3, < 2.0) 11 | test-unit (2.5.5) 12 | 13 | PLATFORMS 14 | java 15 | ruby 16 | x86-mingw32 17 | 18 | DEPENDENCIES 19 | erubis 20 | rack 21 | rack-test 22 | rspec-expectations 23 | test-unit 24 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Alistair Cockburn 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Smaller Web Hexagon 2 | ========== 3 | 4 | Illustration of a simple hexagon with one user (left) port and one database (right) port. 5 | 6 | It simply calculates the formula: 7 | 8 | result = input * rate 9 | 10 | e.g. 11 | 12 | result = 100 * 1.1 = 110 13 | 14 | 15 | The user port is connected to either the web or to a test harness, with or without the server adapter. 16 | 17 | The database port looks up the rate in a database, either in-the-code database, or from a file. 18 | 19 | The startup decides how the wiring goes. 20 | 21 | ## Running the Application 22 | 23 | Run `rackup config.ru` to get the web UI on port 9292 24 | 25 | ## Running Tests 26 | 27 | - Enter `test` folder 28 | - Run `ruby test_smaller_web_hexagon.rb` 29 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # run the Smaller Web Hexagon from a browser 2 | 3 | require './src/smaller_web_hexagon' 4 | require './src/rack_http_adapter' 5 | require './src/raters' 6 | 7 | hex = SmallerWebHexagon.new(InCodeRater.new) 8 | app = RackHttpAdapter.new(hex,"./src/views/") 9 | 10 | run app 11 | -------------------------------------------------------------------------------- /src/html_from_template_file.rb: -------------------------------------------------------------------------------- 1 | require 'erb' 2 | require 'erubis' 3 | 4 | 5 | def html_from_template_file(template_path, binding) 6 | page_template = Erubis::Eruby.new(File.open(template_path, 'r').read) 7 | page_template.result(binding) 8 | end 9 | -------------------------------------------------------------------------------- /src/rack_http_adapter.rb: -------------------------------------------------------------------------------- 1 | require 'rack' 2 | require 'pathname' 3 | require_relative '../src/html_from_template_file' 4 | 5 | 6 | # Primary adapter to SmallerWebHexagon, using Rack for web-type I/O 7 | class RackHttpAdapter 8 | 9 | def initialize(hex_app, views_folder) 10 | @app = hex_app 11 | @views_folder = views_folder 12 | end 13 | 14 | def call(env) # hooks into the Rack Request chain 15 | request = Rack::Request.new(env) 16 | value = path_as_number(request) 17 | 18 | rate, result = @app.rate_and_result value 19 | 20 | out = { 21 | out_action: 'result_view', 22 | value: value, 23 | rate: rate, 24 | result: result 25 | } 26 | 27 | template_path = Pathname.new(@views_folder).join(out[:out_action]).sub_ext('.erb') 28 | page = html_from_template_file(template_path , binding) 29 | 30 | response = Rack::Response.new 31 | response.write(page) 32 | response.finish 33 | end 34 | 35 | private 36 | 37 | # ==== utilities for reading a Rack Request ==== 38 | 39 | def path_as_number(request) 40 | number_or_zero(path_contents(request)) 41 | end 42 | 43 | def path_contents(request) 44 | request.path[ 1..request.path.size ] 45 | end 46 | 47 | # converts string to a number, zero if not a number 48 | def number_or_zero(s) 49 | i= s.to_i 50 | i.to_s == s ? i : 0 51 | end 52 | 53 | end 54 | -------------------------------------------------------------------------------- /src/raters.rb: -------------------------------------------------------------------------------- 1 | # a Rater produces a multiplier ("rate") given a value 2 | # here are two kinds of raters: 3 | # - a variable in-code one that can be used when the db is down 4 | # - one w the table stored in a file (or db, but I only know files so far) 5 | # note: I'm making them give different rates, so that mistakes show up easier 6 | 7 | 8 | class InCodeRater 9 | 10 | def rate value 11 | case 12 | when value <= 100 13 | 1.01 14 | when value > 100 15 | 1.5 16 | end 17 | end 18 | 19 | end 20 | 21 | 22 | class FileRater 23 | 24 | def initialize fn 25 | @rates = [] 26 | File.open(fn) do |f| 27 | f.each_line do |line| 28 | @rates << line.split.map(&:to_f) 29 | end 30 | end 31 | end 32 | 33 | def rate value # ugly code but I only need to know it works 34 | case 35 | when value >= @rates[0][0] && value < @rates[1][0] 36 | rate = @rates[0][1] 37 | when value >= @rates[0][0] 38 | rate = @rates[1][1] 39 | end 40 | end 41 | 42 | end 43 | -------------------------------------------------------------------------------- /src/smaller_web_hexagon.rb: -------------------------------------------------------------------------------- 1 | # Welcome to Smallerwebhexagon, the simple web hexagon implementation 2 | # Alistair Cockburn and a couple of really nice friends 3 | 4 | # this hexagon has one primary port and one secondary 5 | # the user/test/web side is the primary 6 | # the rates "database" mechanism is the secondary 7 | # the app itself just returns value * rate(as a function of value) 8 | 9 | 10 | class SmallerWebHexagon 11 | 12 | def initialize rater 13 | @rater = rater # the database port needs configuring 14 | end 15 | 16 | def rate_and_result value 17 | rate = @rater.rate(value) 18 | result = value * rate 19 | return rate, result 20 | end 21 | 22 | end 23 | -------------------------------------------------------------------------------- /src/views/result_view.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Smaller Web Hexagon 7 | 8 | 9 | 10 | 11 |

12 | value = <%= out[:value] %>:
13 | rate = <%= out[:rate] %>:
14 | result = <%= out[:result] %>:
15 |

16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /test/file_rater.txt: -------------------------------------------------------------------------------- 1 | 0 1.0 2 | 100 2.0 3 | -------------------------------------------------------------------------------- /test/test_smaller_web_hexagon.rb: -------------------------------------------------------------------------------- 1 | require_relative '../src/smaller_web_hexagon' 2 | require_relative '../src/rack_http_adapter' 3 | require_relative '../src/raters' 4 | require 'rack/test' 5 | require 'rspec/expectations' 6 | require 'test/unit' 7 | 8 | # The first 2 tests check the primary adapter swaps, using direct API access for the left 9 | # The last test checks the secondary adapter swap, using Rack input. 10 | # The config.ru file runs the real server stuff, for the final usage test. 11 | 12 | # note about the tests, I made all the raters give different answers, 13 | # so that I can see if they are hooked up wrong 14 | 15 | 16 | class TestRequests < Test::Unit::TestCase 17 | attr_accessor :app 18 | 19 | def test_it_works_with_in_code_rater 20 | p __method__ 21 | 22 | @app = SmallerWebHexagon.new(InCodeRater.new) 23 | 24 | value_should_produce_rate 100, 1.01 25 | value_should_produce_rate 200, 1.5 26 | end 27 | 28 | 29 | def test_it_works_with_file_rater 30 | p __method__ 31 | 32 | @app = SmallerWebHexagon.new(FileRater.new('file_rater.txt')) 33 | 34 | value_should_produce_rate 10, 1.00 35 | value_should_produce_rate 100, 2.0 36 | end 37 | 38 | 39 | def test_runs_via_rack_adapter 40 | p __method__ 41 | 42 | views_folder = '../src/views/' 43 | hex = SmallerWebHexagon.new (InCodeRater.new) 44 | app = RackHttpAdapter.new(hex, views_folder) 45 | 46 | request = Rack::MockRequest.new(app) 47 | response = request.request('GET', '/100') # sends the req through the Rack call(env) chain 48 | 49 | out = { # expected values 50 | out_action: 'result_view', 51 | value: 100, 52 | rate: 1.01, 53 | result: (100)*(1.01) 54 | } 55 | response.body.should == html_from_template_file(views_folder + 'result_view.erb' , binding) 56 | end 57 | 58 | 59 | def value_should_produce_rate value, exp_rate 60 | rate, result = @app.rate_and_result value 61 | 62 | rate.should == exp_rate 63 | result.should == value * exp_rate 64 | end 65 | 66 | end 67 | --------------------------------------------------------------------------------