Navigation Menu

Skip to content

Commit

Permalink
Introduced Cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmorales committed Mar 4, 2017
1 parent 039f6e1 commit 924b424
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 10 deletions.
3 changes: 3 additions & 0 deletions config-sample.yml
@@ -0,0 +1,3 @@
---
:port: 9292
:url: http://localhost:9292
3 changes: 3 additions & 0 deletions config.yml
@@ -0,0 +1,3 @@
---
:port: 9292
:url: http://localhost:9292
13 changes: 13 additions & 0 deletions 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
6 changes: 5 additions & 1 deletion lib/todo/box.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions 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
9 changes: 7 additions & 2 deletions lib/todo/cli.rb
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/todo/router.rb
@@ -0,0 +1,4 @@
module Todo
class Router
end
end
4 changes: 2 additions & 2 deletions lib/todo/todos_api_serializer.rb
Expand Up @@ -2,15 +2,15 @@ module Todo
class TodosApiSerializer
include LittleBoxes::Configurable

dependency :todo_path
dependency :todo_url

def call(todo)
{
id: todo.id,
title: todo.title,
completed: todo.completed,
order: todo.order,
url: todo_path.(todo),
url: todo_url.(todo),
}
end
end
Expand Down
10 changes: 8 additions & 2 deletions spec/functional/api_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions 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
4 changes: 4 additions & 0 deletions spec/todo/app_spec.rb
@@ -0,0 +1,4 @@
require_relative '../helper'

RSpec.describe 'App' do
end
6 changes: 3 additions & 3 deletions 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(
Expand All @@ -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

0 comments on commit 924b424

Please sign in to comment.