grape_bootstrap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ M2U4NGQ2NjcyODMzYmFkM2MxNzE3Yzc4YjlhMmM1MDIxM2RkZWQxYg==
5
+ data.tar.gz: !binary |-
6
+ MGY1MzlkMWU3YzlhOTQ1YjE4NzA5NTEyMjIzM2Q5ZGQ0YjFjNDQ5OA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZTk3Y2U1YjI3MjZjZTRmM2NkMWE1YTM2ZDIyZWQ0NTM4NzdjM2U2MjZmZDkw
10
+ N2U4MGQ3Yjc0YmNkZjQ3MWE1NDkxZmZmMmI0ZGIwMGU1NjdjZjNkZmU0ODU4
11
+ YzI3YWVmMjc0NjdiM2Y5MmU0NTkxZDI3NjFlMmNiZjQ5MWMzZWY=
12
+ data.tar.gz: !binary |-
13
+ YjE4OGIzNmJkZWMwMTU2NjRmYjdhODJmNzIxYzc3ODhjYTM1MDE4NTkyOWUy
14
+ OWY1NmQ3N2E0MmRmZjRmM2MwYjEyMzkwY2Q4N2JmNzI4MjUxZjczYTRhZGMw
15
+ ODI1OTliNjE2ODFlY2FmYjQwMGViNGNiNzg2Yjk4MzhkMDI2OTg=
data/bin/grape ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "grape_bootstrap/cli"
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/grape_bootstrap/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Mate Marjai"]
6
+ gem.email = ["mate.marjai@gmail.com"]
7
+ gem.description = %q{This library contains an executable to bootstrap and scaffold the minimum for a grape REST API.}
8
+ gem.summary = %q{Grape REST API bootstrap.}
9
+ gem.homepage = "https://github.com/marjaimate/grape-bootstrap"
10
+ gem.license = "MIT"
11
+
12
+ gem.files = [
13
+ Dir.glob("lib/**/*.rb"),
14
+ Dir.glob("lib/**/*.erb"),
15
+ Dir.glob("bin/*"),
16
+ ['grape-bootstrap.gemspec']
17
+ ].inject(:+)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.name = "grape_bootstrap"
21
+ gem.require_paths = ["lib"]
22
+ gem.version = GrapeBootstrap::VERSION
23
+
24
+ # Declare the gem dependecies
25
+ gem.add_dependency('grape', '~> 0')
26
+ gem.add_dependency('rake', '~> 0')
27
+ gem.add_dependency('activesupport', '~> 3.0')
28
+ end
@@ -0,0 +1,2 @@
1
+ module GrapeBootstrap
2
+ end
@@ -0,0 +1,6 @@
1
+ if [nil, "-h", "--help"].include?(ARGV.first)
2
+ ARGV.shift
3
+ require 'grape_bootstrap/commands/help'
4
+ else
5
+ require 'grape_bootstrap/commands/application'
6
+ end
@@ -0,0 +1,72 @@
1
+ require 'grape_bootstrap/commands/base'
2
+ require 'fileutils'
3
+ require 'erb'
4
+
5
+ module GrapeBootstrap
6
+ module Commands
7
+ class AppInit < Base
8
+ def initialize path, options=[]
9
+ super path, options
10
+ @dry = (options & ["--dry"]).length > 0
11
+
12
+ setup_dirs
13
+ setup_base_files
14
+ end
15
+
16
+ private
17
+ def setup_dirs
18
+ {
19
+ "app/api" => ["base.rb", "status.rb"],
20
+ "app/helpers" => [],
21
+ "app/models" => [],
22
+ "config" => ["database.yml"],
23
+ "config/environments" => ["development.rb"],
24
+ "db/migrate" => [],
25
+ "lib" => [],
26
+ "docs" => [],
27
+ "public" => ["index.html"]
28
+ }.each do |dir, files|
29
+ puts "Creating #{dir}"
30
+ FileUtils.mkdir_p("#{@path}/#{dir}") unless @dry
31
+
32
+ files.each do |f|
33
+ puts "Creating #{dir}/#{f}"
34
+ file_from_template(f, dir) unless @dry
35
+ end
36
+ end
37
+ end
38
+
39
+ def setup_base_files
40
+ [
41
+ "Gemfile",
42
+ "README.md",
43
+ "application.rb",
44
+ "environment.rb",
45
+ "config.ru",
46
+ "Rakefile"
47
+ ].each do |filename|
48
+ puts "Creating #{filename}"
49
+ file_from_template(filename) unless @dry
50
+ end
51
+ end
52
+
53
+ def file_from_template template_name, sub_path=nil
54
+ erb_binding = -> {
55
+ app_name = "APP"
56
+ binding
57
+ }
58
+
59
+ template_name = "#{sub_path}/#{template_name}" if sub_path
60
+ tpl = ERB.new(
61
+ File.read(
62
+ "#{File.dirname(__FILE__)}/../templates/#{template_name}.erb"
63
+ )
64
+ )
65
+
66
+ File.open("#{@path}/#{template_name}", 'w') do |file|
67
+ file << tpl.result(erb_binding.call)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,15 @@
1
+ require "grape_bootstrap/commands/app_init.rb"
2
+ require "grape_bootstrap/commands/generate.rb"
3
+
4
+ main_cmd = ARGV.shift.downcase
5
+ path = %x(pwd).gsub(/\n/, '')
6
+
7
+ case main_cmd
8
+ when 'init'
9
+ GrapeBootstrap::Commands::AppInit.new(path, ARGV)
10
+ when 'generate'
11
+ GrapeBootstrap::Commands::Generate.new(path, ARGV.shift, ARGV)
12
+ else
13
+ puts "Unknown command"
14
+ exit 1
15
+ end
@@ -0,0 +1,14 @@
1
+ module GrapeBootstrap
2
+ module Commands
3
+ class Base
4
+ attr_reader :path
5
+
6
+ def initialize path, options=[]
7
+ if path.nil?
8
+ throw "Path not specified"
9
+ end
10
+ @path = path
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,50 @@
1
+ require 'grape_bootstrap/commands/base'
2
+ require 'active_support/core_ext'
3
+
4
+ module GrapeBootstrap
5
+ module Commands
6
+ class Generate < Base
7
+ def initialize path, type, options=[]
8
+ super path, options
9
+ if type.nil?
10
+ throw "Type not specified"
11
+ end
12
+ @type = type
13
+
14
+ self.send type.downcase, options.shift
15
+ end
16
+
17
+ def model name
18
+ if name.nil?
19
+ throw "Model needs a name to be generated"
20
+ end
21
+
22
+ render_template name
23
+ end
24
+
25
+ private
26
+ def render_template name
27
+ template_type = @type
28
+ erb_binding = -> {
29
+ name = name.camelize
30
+ binding
31
+ }
32
+
33
+ template_path = [
34
+ File.dirname(__FILE__),
35
+ "..",
36
+ "templates",
37
+ "app",
38
+ template_type.pluralize,
39
+ "new.rb.erb"
40
+ ].join('/')
41
+ tpl = ERB.new(File.read(template_path))
42
+ target_path = "app/#{template_type.pluralize}/#{name.underscore}.rb"
43
+
44
+ File.open("#{@path}/#{target_path}", 'w') do |file|
45
+ file << tpl.result(erb_binding.call)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,6 @@
1
+ puts <<HELP
2
+
3
+ Grape Boostrap - helping you init a Grape REST API from scratch.
4
+
5
+ HELP
6
+
@@ -0,0 +1,11 @@
1
+ # A sample Gemfile
2
+ source 'https://rubygems.org'
3
+
4
+ gem 'rack'
5
+ gem 'grape'
6
+ gem 'activerecord', '~>3.2.13', require: 'active_record'
7
+ gem 'sqlite3', group: [:development, :test]
8
+ gem 'pg', group: :production
9
+ gem 'rake'
10
+ gem 'encode_with_alphabet'
11
+ gem 'racksh'
@@ -0,0 +1 @@
1
+ README skeleton
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env rake
2
+ require File.expand_path('../environment', __FILE__)
3
+
4
+ task :rails_env do
5
+ end
6
+
7
+ task :environment do
8
+ end
9
+
10
+ module Rails
11
+ def self.application
12
+ Struct.new(:config, :paths) do
13
+ def load_seed
14
+ require File.expand_path('../application', __FILE__)
15
+ require File.expand_path('../db/seeds', __FILE__)
16
+ end
17
+ end.new(config, paths)
18
+ end
19
+
20
+ def self.config
21
+ require 'erb'
22
+ db_config = YAML.load(ERB.new(File.read("config/database.yml")).result)
23
+ Struct.new(:database_configuration).new(db_config)
24
+ end
25
+
26
+ def self.paths
27
+ { 'db/migrate' => ["#{root}/db/migrate"] }
28
+ end
29
+
30
+ def self.env
31
+ env = ENV['RACK_ENV'] || "development"
32
+ ActiveSupport::StringInquirer.new(env)
33
+ end
34
+
35
+ def self.root
36
+ File.dirname(__FILE__)
37
+ end
38
+ end
39
+
40
+ namespace :g do
41
+ desc "Generate migration. Specify name in the NAME variable"
42
+ task :migration => :environment do
43
+ name = ENV['NAME'] || raise("Specify name: rake g:migration NAME=create_users")
44
+ timestamp = Time.now.strftime("%Y%m%d%H%M%S")
45
+
46
+ path = File.expand_path("../db/migrate/#{timestamp}_#{name}.rb", __FILE__)
47
+ migration_class = name.split("_").map(&:capitalize).join
48
+
49
+ File.open(path, 'w') do |file|
50
+ file.write <<-EOF.strip_heredoc
51
+ class #{migration_class} < ActiveRecord::Migration
52
+ def self.up
53
+ end
54
+
55
+ def self.down
56
+ end
57
+ end
58
+ EOF
59
+ end
60
+
61
+ puts "DONE"
62
+ puts path
63
+ end
64
+ end
65
+
66
+ Rake.load_rakefile "active_record/railties/databases.rake"
@@ -0,0 +1,30 @@
1
+ class API::Base < Grape::API
2
+ def self.inherited(subclass)
3
+ super
4
+ subclass.instance_eval do
5
+ version 'v1', :using => :path
6
+ format :json
7
+
8
+ rescue_from ActiveRecord::RecordNotFound do |e|
9
+ message = e.message.gsub(/\s*\[.*\Z/, '')
10
+ Rack::Response.new(
11
+ [{ status: 404, status_code: "not_found", error: message }.to_json],
12
+ 404,
13
+ { 'Content-Type' => 'application/json' }
14
+ )
15
+ end
16
+
17
+ rescue_from ActiveRecord::RecordInvalid do |e|
18
+ record = e.record
19
+ message = e.message.downcase.capitalize
20
+ Rack::Response.new(
21
+ [{ status: 403, status_code: "record_invalid", error: message }.to_json],
22
+ 403,
23
+ { 'Content-Type' => 'application/json' }
24
+ )
25
+ end
26
+ end
27
+ end
28
+
29
+ default_format :json
30
+ end
@@ -0,0 +1,6 @@
1
+ class API::Status < API::Base
2
+ desc "Returns the status of the API"
3
+ get '/status' do
4
+ { status: 'ok' }
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class <%= name %> < ActiveRecord::Base
2
+ def initialize
3
+ # Do stuff
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path('../environment', __FILE__)
2
+
3
+ module API
4
+ end
5
+
6
+ require 'app/api/base'
7
+
8
+ Dir["#{File.dirname(__FILE__)}/app/models/extensions/**/*.rb"].each {|f| require f}
9
+ Dir["#{File.dirname(__FILE__)}/app/models/**/*.rb"].each {|f| require f}
10
+ Dir["#{File.dirname(__FILE__)}/app/**/*.rb"].each {|f| require f}
11
+
12
+ ActiveRecord::Base.instance_eval do
13
+ include ActiveModel::MassAssignmentSecurity
14
+ attr_accessible []
15
+ end
16
+
17
+
18
+ class API::Root < Grape::API
19
+ format :json
20
+
21
+ before do
22
+ header['Access-Control-Allow-Origin'] = '*'
23
+ header['Access-Control-Request-Method'] = '*'
24
+ end
25
+
26
+ mount API::Base
27
+ mount API::Status
28
+ end
29
+
30
+ ApplicationServer = Rack::Builder.new {
31
+ use Rack::Static, :urls => [
32
+ "/css",
33
+ "/images",
34
+ "/lib"
35
+ ], :root => "public", index: 'index.html'
36
+
37
+ map "/" do
38
+ run API::Root
39
+ end
40
+ }
@@ -0,0 +1,3 @@
1
+ require File.expand_path('../application', __FILE__)
2
+
3
+ run ApplicationServer
@@ -0,0 +1,15 @@
1
+ development:
2
+ adapter: postgresql
3
+ encoding: utf-8
4
+ database: grape_api_development
5
+ pool: 5
6
+ username: grape
7
+ password: password
8
+
9
+ test: &test
10
+ adapter: postgresql
11
+ encoding: utf-8
12
+ database: grape_api_test
13
+ pool: 5
14
+ username: grape
15
+ password: password
@@ -0,0 +1,3 @@
1
+ puts "Loading development environment"
2
+
3
+ Application.config.base_path = "http://localhost:9292"
@@ -0,0 +1,24 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ env = (ENV['RACK_ENV'] || :development)
3
+
4
+ require 'bundler'
5
+ Bundler.require :default, env.to_sym
6
+ require 'erb'
7
+
8
+ module Application
9
+ include ActiveSupport::Configurable
10
+ end
11
+
12
+ Application.configure do |config|
13
+ config.root = File.dirname(__FILE__)
14
+ config.env = ActiveSupport::StringInquirer.new(env.to_s)
15
+ end
16
+
17
+ db_config = YAML.load(ERB.new(File.read("config/database.yml")).result)[Application.config.env]
18
+ ActiveRecord::Base.default_timezone = :utc
19
+ ActiveRecord::Base.establish_connection(db_config)
20
+
21
+ specific_environment = "config/environments/#{Application.config.env}.rb"
22
+ require specific_environment if File.exists? specific_environment
23
+ Dir["config/initializers/**/*.rb"].each {|f| require f}
24
+
@@ -0,0 +1,9 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Grape API Example</title>
5
+ </head>
6
+ <body>
7
+ GRAPE API BOOTSTRAPPED
8
+ </body>
9
+ </html>
@@ -0,0 +1,3 @@
1
+ module GrapeBootstrap
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape_bootstrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mate Marjai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: This library contains an executable to bootstrap and scaffold the minimum
56
+ for a grape REST API.
57
+ email:
58
+ - mate.marjai@gmail.com
59
+ executables:
60
+ - grape
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - bin/grape
65
+ - grape-bootstrap.gemspec
66
+ - lib/grape_bootstrap.rb
67
+ - lib/grape_bootstrap/cli.rb
68
+ - lib/grape_bootstrap/commands/app_init.rb
69
+ - lib/grape_bootstrap/commands/application.rb
70
+ - lib/grape_bootstrap/commands/base.rb
71
+ - lib/grape_bootstrap/commands/generate.rb
72
+ - lib/grape_bootstrap/commands/help.rb
73
+ - lib/grape_bootstrap/templates/Gemfile.erb
74
+ - lib/grape_bootstrap/templates/README.md.erb
75
+ - lib/grape_bootstrap/templates/Rakefile.erb
76
+ - lib/grape_bootstrap/templates/app/api/base.rb.erb
77
+ - lib/grape_bootstrap/templates/app/api/status.rb.erb
78
+ - lib/grape_bootstrap/templates/app/models/new.rb.erb
79
+ - lib/grape_bootstrap/templates/application.rb.erb
80
+ - lib/grape_bootstrap/templates/config.ru.erb
81
+ - lib/grape_bootstrap/templates/config/database.yml.erb
82
+ - lib/grape_bootstrap/templates/config/environments/development.rb.erb
83
+ - lib/grape_bootstrap/templates/environment.rb.erb
84
+ - lib/grape_bootstrap/templates/public/index.html.erb
85
+ - lib/grape_bootstrap/version.rb
86
+ homepage: https://github.com/marjaimate/grape-bootstrap
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Grape REST API bootstrap.
110
+ test_files: []