Skip to content

Commit

Permalink
Getting things started by rendering a flippin image.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Sep 11, 2012
1 parent af1ab44 commit d253359
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 25 deletions.
17 changes: 15 additions & 2 deletions Gemfile
@@ -1,4 +1,17 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in flipper-ui.gemspec
gemspec

gem 'rake'

group(:guard) do
gem 'guard'
gem 'guard-rspec'
gem 'guard-bundler'
gem 'terminal-notifier-guard'
gem 'rb-fsevent'
end

group(:test) do
gem 'rspec'
gem 'rack-test'
end
13 changes: 13 additions & 0 deletions Guardfile
@@ -0,0 +1,13 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'bundler' do
watch('Gemfile')
watch(/^.+\.gemspec/)
end

guard 'rspec' do
watch(%r{^spec/.+_spec\.rb$}) { "spec" }
watch(%r{^lib/(.+)\.rb$}) { "spec" }
watch('spec/helper.rb') { "spec" }
end
8 changes: 2 additions & 6 deletions README.md
@@ -1,6 +1,6 @@
# Flipper::Ui
# Flipper::UI

TODO: Write a gem description
UI for the Flipper gem.

## Installation

Expand All @@ -16,10 +16,6 @@ Or install it yourself as:

$ gem install flipper-ui

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
Expand Down
19 changes: 19 additions & 0 deletions examples/basic.ru
@@ -0,0 +1,19 @@
# Nothing to see here... move along.
# Sets up load path for examples and requires some stuff
require 'pp'
require 'pathname'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')
$:.unshift(lib_path)

require 'flipper-ui'
require 'flipper/adapters/memory'

adapter = Flipper::Adapters::Memory.new({})
flipper = Flipper.new(adapter)

use Flipper::UI::Middleware, flipper
run lambda { |env|
[200, {'Content-Type' => 'text/html'}, ['Go here for <a href="/flipper">Flipper!</a>']]
}
13 changes: 8 additions & 5 deletions flipper-ui.gemspec
@@ -1,17 +1,20 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/flipper-ui/version', __FILE__)
require File.expand_path('../lib/flipper/ui/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["John Nunemaker"]
gem.email = ["nunemaker@gmail.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""
gem.description = %q{UI for the Flipper gem}
gem.summary = %q{UI for the Flipper gem}
gem.homepage = "https://github.com/jnunemaker/flipper-ui"

gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "flipper-ui"
gem.require_paths = ["lib"]
gem.version = Flipper::Ui::VERSION
gem.version = Flipper::UI::VERSION

gem.add_dependency 'rack'
gem.add_dependency 'flipper'
end
8 changes: 1 addition & 7 deletions lib/flipper-ui.rb
@@ -1,7 +1 @@
require "flipper-ui/version"

module Flipper
module Ui
# Your code goes here...
end
end
require 'flipper/ui'
5 changes: 0 additions & 5 deletions lib/flipper-ui/version.rb

This file was deleted.

10 changes: 10 additions & 0 deletions lib/flipper/ui.rb
@@ -0,0 +1,10 @@
require 'flipper'
require 'flipper/ui/middleware'

module Flipper
module UI
def self.root
@root ||= Pathname(__FILE__).dirname.expand_path.join('ui')
end
end
end
97 changes: 97 additions & 0 deletions lib/flipper/ui/middleware.rb
@@ -0,0 +1,97 @@
require 'pathname'
require 'erb'
require 'rack'

module Flipper
module UI
class Middleware
def initialize(app, flipper)
@app = app
@flipper = flipper
end

class Action
def self.views_path
Flipper::UI.root.join('views')
end

def self.public_path
Flipper::UI.root.join('public')
end

attr_reader :request

def initialize(request)
@request = request
@code = 200
@headers = {'Content-Type' => 'text/html'}
end

def render(name)
body = render_with_layout do
render_without_layout name
end

Rack::Response.new(body)
end

def render_with_layout(&block)
render_template :layout, &block
end

def render_without_layout(name)
render_template name
end

def render_template(name)
path = self.class.views_path.join("#{name}.erb")
contents = path.read
compiled = ERB.new(contents)
compiled.result Proc.new {}.binding
end
end

class Index < Action
def get
render :index
end
end

class File < Action
def get
Rack::File.new(self.class.public_path).call(request.env)
end
end

class Route
def self.detect(request)
return unless request.path_info =~ /^\/flipper/

case request.path_info
when /\/flipper\/?$/
Index.new(request)
when /\/flipper\/images\/(.*)/
File.new(request)
when /\/flipper\/css\/(.*)/
File.new(request)
end
end
end

def call(env)
request = Rack::Request.new(env)

if action = Route.detect(request)
case request.request_method.downcase
when 'get'
action.get
else
raise "#{request.request_method} not supported at this time"
end
else
@app.call(env)
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/flipper/ui/public/flipper/css/application.css
@@ -0,0 +1,7 @@
#header h1 a {
display:block;
width:75px;
height:42px;
text-indent: -9999px;
background:url(/flipper/images/logo.png) 0 0 no-repeat;
}
Binary file added lib/flipper/ui/public/flipper/images/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions lib/flipper/ui/version.rb
@@ -0,0 +1,5 @@
module Flipper
module UI
VERSION = "0.1.0"
end
end
Empty file added lib/flipper/ui/views/index.erb
Empty file.
16 changes: 16 additions & 0 deletions lib/flipper/ui/views/layout.erb
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flipper</title>
<link rel="stylesheet" type="text/css" href="/flipper/css/application.css">
</head>
<body>
<div id="header">
<h1><a href="/">Flipper</a></h1>
</div>

<div id="content">
<%= yield %>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions log/test.log
@@ -0,0 +1 @@
# Logfile created on 2012-09-07 15:05:48 -0400 by logger.rb/31641
61 changes: 61 additions & 0 deletions spec/flipper-ui/middleware_spec.rb
@@ -0,0 +1,61 @@
require 'helper'
require 'rack/test'
require 'flipper'
require 'flipper/adapters/memory'

describe Flipper::UI::Middleware do
include Rack::Test::Methods

let(:source) { {} }
let(:adapter) { Flipper::Adapters::Memory.new(source) }
let(:flipper) { Flipper.new(adapter) }

def app
@app ||= begin
middleware = described_class
instance = flipper

Rack::Builder.new do
use middleware, instance

map "/" do
run lambda {|env| [404, {}, []] }
end
end.to_app
end
end

describe "GET /flipper" do
before do
get '/flipper'
end

it "responds with 200" do
last_response.status.should be(200)
end

it "renders view" do
last_response.body.should match(/Flipper/)
end
end

describe "GET /flipper/images/logo.png" do
before do
get '/flipper/images/logo.png'
end

it "responds with 200" do
last_response.status.should be(200)
end
end

describe "GET /flipper/css/application.css" do
before do
get '/flipper/css/application.css'
end

it "responds with 200" do
last_response.status.should be(200)
end
end
end
25 changes: 25 additions & 0 deletions spec/helper.rb
@@ -0,0 +1,25 @@
$:.unshift(File.expand_path('../../lib', __FILE__))

require 'pathname'
require 'logger'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')
log_path = root_path.join('log')
log_path.mkpath

require 'rubygems'
require 'bundler'

Bundler.require(:default, :test)

require 'flipper-ui'

Logger.new(log_path.join('test.log'))

RSpec.configure do |config|
config.filter_run :focused => true
config.alias_example_to :fit, :focused => true
config.alias_example_to :xit, :pending => true
config.run_all_when_everything_filtered = true
end

0 comments on commit d253359

Please sign in to comment.