Permalink
Browse files

initial version of the gem + generator

  • Loading branch information...
1 parent b4bea30 commit a2008e61bf2509603bc880d19912c599cd0a3a59 @mattetti committed Apr 27, 2012
Showing with 13,673 additions and 9 deletions.
  1. +5 −6 .gitignore
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +29 −3 README.md
  5. +2 −0 Rakefile
  6. +47 −0 bin/wd_sinatra
  7. +7 −0 lib/wd_sinatra.rb
  8. +129 −0 lib/wd_sinatra/app_loader.rb
  9. +112 −0 lib/wd_sinatra/sinatra_ext.rb
  10. +204 −0 lib/wd_sinatra/test_helpers.rb
  11. +5 −0 lib/wd_sinatra/version.rb
  12. +24 −0 templates/Gemfile
  13. +13 −0 templates/Guardfile
  14. +38 −0 templates/Rakefile
  15. +29 −0 templates/api/hello_world.rb
  16. +7 −0 templates/bin/console
  17. +5 −0 templates/config.ru
  18. +1 −0 templates/config/app.rb
  19. +3 −0 templates/config/environments/default.rb
  20. +2 −0 templates/config/environments/production.rb
  21. +3 −0 templates/config/environments/test.rb
  22. 0 templates/config/middleware.rb
  23. +34 −0 templates/lib/auth_helpers.rb
  24. +41 −0 templates/lib/tasks/doc.rake
  25. +4 −0 templates/lib/tasks/doc_generator/bootstrap/.gitignore
  26. +176 −0 templates/lib/tasks/doc_generator/bootstrap/LICENSE
  27. +47 −0 templates/lib/tasks/doc_generator/bootstrap/Makefile
  28. +105 −0 templates/lib/tasks/doc_generator/bootstrap/README.md
  29. +2,467 −0 templates/lib/tasks/doc_generator/bootstrap/bootstrap.css
  30. +356 −0 templates/lib/tasks/doc_generator/bootstrap/bootstrap.min.css
  31. +317 −0 templates/lib/tasks/doc_generator/bootstrap/docs/assets/css/docs.css
  32. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/ico/bootstrap-apple-114x114.png
  33. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/ico/bootstrap-apple-57x57.png
  34. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/ico/bootstrap-apple-72x72.png
  35. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/ico/favicon.ico
  36. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/img/bird.png
  37. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/img/browsers.png
  38. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/img/example-diagram-01.png
  39. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/img/example-diagram-02.png
  40. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/img/example-diagram-03.png
  41. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/img/grid-18px.png
  42. BIN templates/lib/tasks/doc_generator/bootstrap/docs/assets/img/twitter-logo-no-bird.png
  43. +52 −0 templates/lib/tasks/doc_generator/bootstrap/docs/assets/js/application.js
  44. +94 −0 templates/lib/tasks/doc_generator/bootstrap/docs/assets/js/google-code-prettify/prettify.css
  45. +28 −0 templates/lib/tasks/doc_generator/bootstrap/docs/assets/js/google-code-prettify/prettify.js
  46. +2,037 −0 templates/lib/tasks/doc_generator/bootstrap/docs/index.html
  47. +798 −0 templates/lib/tasks/doc_generator/bootstrap/docs/javascript.html
  48. +119 −0 templates/lib/tasks/doc_generator/bootstrap/examples/container-app.html
  49. +122 −0 templates/lib/tasks/doc_generator/bootstrap/examples/fluid.html
  50. +79 −0 templates/lib/tasks/doc_generator/bootstrap/examples/hero.html
  51. +124 −0 templates/lib/tasks/doc_generator/bootstrap/js/bootstrap-alerts.js
  52. +64 −0 templates/lib/tasks/doc_generator/bootstrap/js/bootstrap-buttons.js
  53. +55 −0 templates/lib/tasks/doc_generator/bootstrap/js/bootstrap-dropdown.js
  54. +260 −0 templates/lib/tasks/doc_generator/bootstrap/js/bootstrap-modal.js
  55. +90 −0 templates/lib/tasks/doc_generator/bootstrap/js/bootstrap-popover.js
  56. +107 −0 templates/lib/tasks/doc_generator/bootstrap/js/bootstrap-scrollspy.js
  57. +80 −0 templates/lib/tasks/doc_generator/bootstrap/js/bootstrap-tabs.js
  58. +321 −0 templates/lib/tasks/doc_generator/bootstrap/js/bootstrap-twipsy.js
  59. +40 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/index.html
  60. +41 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/unit/bootstrap-alerts.js
  61. +42 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/unit/bootstrap-buttons.js
  62. +52 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/unit/bootstrap-dropdown.js
  63. +151 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/unit/bootstrap-modal.js
  64. +76 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/unit/bootstrap-popover.js
  65. +31 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/unit/bootstrap-scrollspy.js
  66. +77 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/unit/bootstrap-tabs.js
  67. +81 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/unit/bootstrap-twipsy.js
  68. +232 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/vendor/qunit.css
  69. +1,510 −0 templates/lib/tasks/doc_generator/bootstrap/js/tests/vendor/qunit.js
  70. +26 −0 templates/lib/tasks/doc_generator/bootstrap/lib/bootstrap.less
  71. +479 −0 templates/lib/tasks/doc_generator/bootstrap/lib/forms.less
  72. +222 −0 templates/lib/tasks/doc_generator/bootstrap/lib/mixins.less
  73. +1,060 −0 templates/lib/tasks/doc_generator/bootstrap/lib/patterns.less
  74. +141 −0 templates/lib/tasks/doc_generator/bootstrap/lib/reset.less
  75. +139 −0 templates/lib/tasks/doc_generator/bootstrap/lib/scaffolding.less
  76. +224 −0 templates/lib/tasks/doc_generator/bootstrap/lib/tables.less
  77. +187 −0 templates/lib/tasks/doc_generator/bootstrap/lib/type.less
  78. +60 −0 templates/lib/tasks/doc_generator/bootstrap/lib/variables.less
  79. +117 −0 templates/lib/tasks/doc_generator/template.erb
  80. BIN templates/public/favicon.ico
  81. +17 −0 wd-sinatra.gemspec
View
@@ -2,17 +2,16 @@
*.rbc
.bundle
.config
-coverage
+.yardoc
+Gemfile.lock
InstalledFiles
+_yardoc
+coverage
+doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
-
-# YARD artifacts
-.yardoc
-_yardoc
-doc/
View
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in wd-sinatra.gemspec
+gemspec
View
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Matt Aimonetti
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -1,4 +1,30 @@
-wd-sinatra
-==========
+# Weasel Diesel Sinatra
-Weasel-Diesel Sinatra app gem, allowing you to generate/update sinatra apps using the Weasel Diesel DSL
+Weasel-Diesel Sinatra app gem, allowing you to generate/update sinatra apps using the Weasel Diesel DSL
+
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'wd-sinatra'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install wd-sinatra
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
View
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
View
@@ -0,0 +1,47 @@
+#!/usr/bin/env ruby
+require "rubygems"
+require "thor/group"
+
+class WdSinatra < Thor::Group
+ include Thor::Actions
+
+ # Define arguments and options
+ # argument :type, :type => :string, :desc => "The type to generate, app by default"
+ argument :name, :type => :string, :desc => "The name of the app to generate"
+ class_option :test_framework, :default => :test_unit
+
+ def self.source_root
+ File.expand_path(File.join('..', 'templates'), File.dirname(__FILE__))
+ end
+
+ def create_lib_directory
+ directory "lib", "#{name}/lib"
+ end
+
+ def create_config_directory
+ directory "config", "#{name}/lib"
+ end
+
+ def create_bin_directory
+ directory "bin", "#{name}/bin"
+ chmod "#{name}/bin/console", 0755
+ end
+
+ def create_public_directory
+ directory "public", "#{name}/public"
+ end
+
+ def create_api_directory
+ directory "api", "#{name}/api"
+ end
+
+ def create_files
+ copy_file "Rakefile", "#{name}/Rakefile"
+ copy_file "Gemfile", "#{name}/Gemfile"
+ copy_file "config.ru", "#{name}/config.ru"
+ copy_file "Guardfile", "#{name}/Guardfile"
+ end
+
+end
+
+WdSinatra.start
View
@@ -0,0 +1,7 @@
+require "wd_sinatra/version"
+
+module WD
+ module Sinatra
+ # Your code goes here...
+ end
+end
@@ -0,0 +1,129 @@
+if RUBY_VERSION =~ /1.8/
+ require 'rubygems'
+ require 'backports'
+end
+require 'bundler'
+Bundler.setup
+require 'logger'
+require 'weasel_diesel'
+
+module WDSinatra
+ module AppLoader
+ module_function
+
+ # Boot in server mode
+ def server(root_path)
+ @root = root_path
+ unless @booted
+ console
+ load_apis
+ load_middleware
+ set_sinatra_routes
+ set_sinatra_settings
+ end
+ end
+
+ # Boot in console mode
+ def console(root_path)
+ @root = root_path
+ unless @booted
+ set_env
+ load_environment
+ set_loadpath
+ load_lib_dependencies
+ #TODO: datastore_setup
+ load_models
+ @booted = true
+ end
+ end
+
+ def root_path
+ @root
+ end
+
+ # PRIVATE
+
+ # Sets the environment (RACK_ENV) based on some env variables.
+ def set_env
+ if !Object.const_defined?(:RACK_ENV)
+ ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] || 'development'
+ Object.const_set(:RACK_ENV, ENV['RACK_ENV'])
+ end
+ puts "Running in #{RACK_ENV} mode" if RACK_ENV == 'development'
+ end
+
+
+ # Loads an environment specific config if available, the config file is where the logger should be set
+ # if it was not, we are using stdout.
+ def load_environment(env=RACK_ENV)
+ # Load the default which can be overwritten or extended by specific
+ # env config files.
+ require File.join(root_path, 'config', 'environments', 'default.rb')
+ env_file = File.join(root_path, "config", "environments", "#{env}.rb")
+ if File.exist?(env_file)
+ require env_file
+ else
+ debug_msg = "Environment file: #{env_file} couldn't be found, using only the default environment config instead." unless env == 'development'
+ end
+ # making sure we have a LOGGER constant defined.
+ unless Object.const_defined?(:LOGGER)
+ Object.const_set(:LOGGER, Logger.new($stdout))
+ end
+ LOGGER.debug(debug_msg) if debug_msg
+ require File.join(root_path, 'config', 'app')
+ end
+
+ def set_loadpath
+ $: << root_path
+ $: << File.join(root_path, 'lib')
+ $: << File.join(root_path, 'models')
+ end
+
+ def load_lib_dependencies
+ # WeaselDiesel is the web service DSL gem used to define services.
+ require 'weasel_diesel'
+ require 'sinatra'
+ require 'wd_sinatra/sinatra_ext'
+ # TODO: hook to custom app dependencies
+ end
+
+ def load_models
+ Dir.glob(File.join(root_path, "models", "**", "*.rb")).each do |model|
+ require model
+ end
+ end
+
+ # DSL routes are located in the api folder
+ def load_apis
+ Dir.glob(File.join(root_path, "api", "**", "*.rb")).each do |api|
+ require api
+ end
+ end
+
+ def set_sinatra_routes
+ WSList.all.sort.each{|api| api.load_sinatra_route }
+ end
+
+ def load_middleware
+ require File.join(root_path, 'config', 'middleware')
+ end
+
+ def set_sinatra_settings
+ # Using the :production env would wrap errors instead of displaying them
+ # like in dev mode
+ set :environment, RACK_ENV
+ set :root, ROOT
+ set :app_file, __FILE__
+ set :public_folder, File.join(root_path, "public")
+ # Checks on static files before dispatching calls
+ enable :static
+ # enable rack session
+ enable :session
+ set :raise_errors, false
+ # enable that option to run by calling this file automatically (without using the config.ru file)
+ # enable :run
+ use Rack::ContentLength
+ end
+
+ end
+end
@@ -0,0 +1,112 @@
+require 'forwardable'
+require 'params_verification'
+require 'json'
+
+class WeaselDiesel
+
+ class RequestHandler
+ extend Forwardable
+
+ # @return [WeaselDiesel] The service served by this controller
+ # @api public
+ attr_reader :service
+
+ # @return [Sinatra::Application]
+ # @api public
+ attr_reader :app
+
+ # @return [Hash]
+ # @api public
+ attr_reader :env
+
+ # @return [Sinatra::Request]
+ # @see http://rubydoc.info/github/sinatra/sinatra/Sinatra/Request
+ # @api public
+ attr_reader :request
+
+ # @return [Sinatra::Response]
+ # @see http://rubydoc.info/github/sinatra/sinatra/Sinatra/Response
+ # @api public
+ attr_reader :response
+
+ # @return [Hash]
+ # @api public
+ attr_accessor :params
+
+ attr_accessor :current_user
+
+ # The service controller might be loaded outside of a Sinatra App
+ # in this case, we don't need to load the helpers
+ if Object.const_defined?(:Sinatra)
+ include Sinatra::Helpers
+ end
+
+ def initialize(service, &block)
+ @service = service
+ @implementation = block
+ end
+
+ def dispatch(app)
+ @app = app
+ @env = app.env
+ @request = app.request
+ @response = app.response
+ @service = service
+
+ begin
+ # raises an exception if the params are not valid
+ # otherwise update the app params with potentially new params (using default values)
+ # note that if a type is mentioned for a params, the object will be cast to this object type
+ #
+ # removing the fake sinatra params since v1.3 added this. (should be eventually removed)
+ if app.params['splat']
+ processed_params = app.params.dup
+ processed_params.delete('splat')
+ processed_params.delete('captures')
+ end
+ @params = ParamsVerification.validate!((processed_params || app.params), service.defined_params)
+ rescue Exception => e
+ LOGGER.error e.message
+ LOGGER.error "passed params: #{app.params.inspect}"
+ halt 400, {:error => e.message}.to_json
+ end
+
+ # Define WeaselDiesel::RequestHandler#authorization_check in your app if
+ # you want to use an auth check.
+ pre_dispatch_hook if self.respond_to?(:pre_dispatch_hook)
+ service_dispatch
+ end
+
+ # Forwarding some methods to the underlying app object
+ def_delegators :app, :settings, :halt, :compile_template, :session
+
+ private ##################################################
+
+ end # of RequestHandler
+
+ attr_reader :handler
+
+ def implementation(&block)
+ if block_given?
+ @handler = RequestHandler.new(self, &block)
+ @handler.define_singleton_method(:service_dispatch, block)
+ end
+ @handler
+ end
+
+ def load_sinatra_route
+ service = self
+ upcase_verb = service.verb.to_s.upcase
+ LOGGER.info "Available endpoint: #{self.http_verb.upcase} /#{self.url}" unless ENV['NO_ROUTE_PRINT']
+ raise "DSL is missing the implementation block" unless self.handler && self.handler.respond_to?(:service_dispatch)
+
+ # Define the route directly to save some object allocations on the critical path
+ # Note that we are using a private API to define the route and that unlike sinatra usual DSL
+ # we do NOT define a HEAD route for every GET route.
+ Sinatra::Base.send(:route, upcase_verb, "/#{self.url}") do
+ service.handler.dispatch(self)
+ end
+
+ end
+
+end
Oops, something went wrong.

0 comments on commit a2008e6

Please sign in to comment.