Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Getting things started by rendering a flippin image.

  • Loading branch information...
commit d253359ee543c726fc89072e1b8c8d159ef2726e 1 parent af1ab44
@jnunemaker authored
View
17 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
View
13 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
View
8 README.md
@@ -1,6 +1,6 @@
-# Flipper::Ui
+# Flipper::UI
-TODO: Write a gem description
+UI for the Flipper gem.
## Installation
@@ -16,10 +16,6 @@ Or install it yourself as:
$ gem install flipper-ui
-## Usage
-
-TODO: Write usage instructions here
-
## Contributing
1. Fork it
View
19 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>']]
+}
View
13 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
View
8 lib/flipper-ui.rb
@@ -1,7 +1 @@
-require "flipper-ui/version"
-
-module Flipper
- module Ui
- # Your code goes here...
- end
-end
+require 'flipper/ui'
View
5 lib/flipper-ui/version.rb
@@ -1,5 +0,0 @@
-module Flipper
- module Ui
- VERSION = "0.0.1"
- end
-end
View
10 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
View
97 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
View
7 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;
+}
View
BIN  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.
View
5 lib/flipper/ui/version.rb
@@ -0,0 +1,5 @@
+module Flipper
+ module UI
+ VERSION = "0.1.0"
+ end
+end
View
0  lib/flipper/ui/views/index.erb
No changes.
View
16 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>
View
1  log/test.log
@@ -0,0 +1 @@
+# Logfile created on 2012-09-07 15:05:48 -0400 by logger.rb/31641
View
61 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
View
25 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
Please sign in to comment.
Something went wrong with that request. Please try again.