From 924b4248c1e8aafb900a3bf617855a4f53dadfb2 Mon Sep 17 00:00:00 2001 From: Manuel Morales Date: Sat, 4 Mar 2017 20:15:05 +0000 Subject: [PATCH] Introduced Cfg --- config-sample.yml | 3 +++ config.yml | 3 +++ lib/todo/app.rb | 13 +++++++++++++ lib/todo/box.rb | 6 +++++- lib/todo/cfg.rb | 26 +++++++++++++++++++++++++ lib/todo/cli.rb | 9 +++++++-- lib/todo/router.rb | 4 ++++ lib/todo/todos_api_serializer.rb | 4 ++-- spec/functional/api_spec.rb | 10 ++++++++-- spec/functional/app_spec.rb | 27 ++++++++++++++++++++++++++ spec/todo/app_spec.rb | 4 ++++ spec/todo/todos_api_serializer_spec.rb | 6 +++--- 12 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 config-sample.yml create mode 100644 config.yml create mode 100644 lib/todo/app.rb create mode 100644 lib/todo/cfg.rb create mode 100644 lib/todo/router.rb create mode 100644 spec/functional/app_spec.rb create mode 100644 spec/todo/app_spec.rb diff --git a/config-sample.yml b/config-sample.yml new file mode 100644 index 0000000..2e16da0 --- /dev/null +++ b/config-sample.yml @@ -0,0 +1,3 @@ +--- +:port: 9292 +:url: http://localhost:9292 diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..2e16da0 --- /dev/null +++ b/config.yml @@ -0,0 +1,3 @@ +--- +:port: 9292 +:url: http://localhost:9292 diff --git a/lib/todo/app.rb b/lib/todo/app.rb new file mode 100644 index 0000000..d66f068 --- /dev/null +++ b/lib/todo/app.rb @@ -0,0 +1,13 @@ +module Todo + class App + include LittleBoxes::Configurable + + dependency :cfg + + def start + require 'puma/cli' + args = ['-p', cfg[:port].to_s] + Puma::CLI.new(args).run + end + end +end diff --git a/lib/todo/box.rb b/lib/todo/box.rb index bb1d550..5ea11e3 100644 --- a/lib/todo/box.rb +++ b/lib/todo/box.rb @@ -4,6 +4,10 @@ module Todo class Box include LittleBoxes::Box + letc(:app) { App.new } + + let(:cfg) { Cfg.load } + let(:rack_app) { |box| box.todos.rack_app } box(:todos) do @@ -28,7 +32,7 @@ class Box letc(:delete) { DeleteTodoEndpoint.new } letc(:delete_all) { DeleteAllTodosEndpoint.new } letc(:serialize) { TodosApiSerializer.new } - let(:todo_path) { -> (todo) { "/todos/#{todo.id}" } } + let(:todo_url) { |box| -> (todo) { "#{box.cfg[:url]}/todos/#{todo.id}" } } end end end diff --git a/lib/todo/cfg.rb b/lib/todo/cfg.rb new file mode 100644 index 0000000..2c53d49 --- /dev/null +++ b/lib/todo/cfg.rb @@ -0,0 +1,26 @@ +module Cfg + extend self + + def load + defaults.merge(options) + end + + private + + def defaults + { port: 9292, url: 'http://localhost:9292' } + end + + def options + if File.exist? path + require 'yaml' + YAML.load(File.read path) + else + {} + end + end + + def path + File.expand_path '../../../config.yml', __FILE__ + end +end diff --git a/lib/todo/cli.rb b/lib/todo/cli.rb index 084c725..4b09a09 100644 --- a/lib/todo/cli.rb +++ b/lib/todo/cli.rb @@ -11,8 +11,13 @@ def test(*args) desc 'start', 'Starts the Puma and any other required thread' def start(*args) - require 'puma/cli' - Puma::CLI.new(args).run + box.app.start + end + + private + + def box + @box ||= Box.new end end end diff --git a/lib/todo/router.rb b/lib/todo/router.rb new file mode 100644 index 0000000..7c919b1 --- /dev/null +++ b/lib/todo/router.rb @@ -0,0 +1,4 @@ +module Todo + class Router + end +end diff --git a/lib/todo/todos_api_serializer.rb b/lib/todo/todos_api_serializer.rb index b820922..7d569dc 100644 --- a/lib/todo/todos_api_serializer.rb +++ b/lib/todo/todos_api_serializer.rb @@ -2,7 +2,7 @@ module Todo class TodosApiSerializer include LittleBoxes::Configurable - dependency :todo_path + dependency :todo_url def call(todo) { @@ -10,7 +10,7 @@ def call(todo) title: todo.title, completed: todo.completed, order: todo.order, - url: todo_path.(todo), + url: todo_url.(todo), } end end diff --git a/spec/functional/api_spec.rb b/spec/functional/api_spec.rb index 5d89dd8..aa03068 100644 --- a/spec/functional/api_spec.rb +++ b/spec/functional/api_spec.rb @@ -4,7 +4,13 @@ RSpec.describe 'API' do include Rack::Test::Methods - let(:box) { Box.new } + + let(:box) do + Box.new.tap do |box| + box.cfg.merge!({ port: 9292, url: 'http://example.com' }) + end + end + let(:app) { box.rack_app } def response_body @@ -70,7 +76,7 @@ def create_todo get "/todos/#{id}" expect(last_response.status).to be 200 expect(response_body).to include('title' => 'laundry') - expect(response_body['url']).to match(/^\/todos\/.*/) + expect(response_body['url']).to start_with('http://example.com/todos') end end diff --git a/spec/functional/app_spec.rb b/spec/functional/app_spec.rb new file mode 100644 index 0000000..c508e7c --- /dev/null +++ b/spec/functional/app_spec.rb @@ -0,0 +1,27 @@ +require_relative '../helper' + +RSpec.describe 'box.app' do + describe '#start' do + let(:box) { Box.new } + let(:app) { box.app } + + it 'runs puma on the given port' do + puma = double(:puma, run: true) + puma_class = double(:puma_cli_class, new: puma) + + require 'puma/cli' # Has to be loaded before stubbing + stub_const('Puma::CLI', puma_class) + + box.cfg[:port] = 80 + + expect(puma_class). + to receive(:new). + with(['-p', '80']). + and_return(puma) + + expect(puma).to receive(:run) + + app.start + end + end +end diff --git a/spec/todo/app_spec.rb b/spec/todo/app_spec.rb new file mode 100644 index 0000000..ec0d1f3 --- /dev/null +++ b/spec/todo/app_spec.rb @@ -0,0 +1,4 @@ +require_relative '../helper' + +RSpec.describe 'App' do +end diff --git a/spec/todo/todos_api_serializer_spec.rb b/spec/todo/todos_api_serializer_spec.rb index 5407c2b..0280045 100644 --- a/spec/todo/todos_api_serializer_spec.rb +++ b/spec/todo/todos_api_serializer_spec.rb @@ -1,8 +1,8 @@ require_relative '../helper' RSpec.describe 'TodosApiSerializer' do - let(:serializer) { TodosApiSerializer.new todo_path: todo_path } - let(:todo_path) { -> (todo) { '/the.url' } } + let(:serializer) { TodosApiSerializer.new todo_url: todo_url } + let(:todo_url) { -> (todo) { 'http://example.com/the.url' } } let(:todo) do TodoEntity.new( @@ -23,6 +23,6 @@ end it 'has a url' do - expect(serializer.(todo)).to include(url: '/the.url') + expect(serializer.(todo)).to include(url: 'http://example.com/the.url') end end