thor_hammer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thor_hammer.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem "rails", "3.2.11"
8
+ gem "test-unit-rails"
9
+ gem "sample_thor_cli", git: "https://github.com/monochromegane/sample_thor_cli.git"
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 monochromegane
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # ThorHammer
2
+
3
+ ThorHammer provides web api for your Thor CLI.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'thor_hammer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install thor_hammer
18
+
19
+ ## Usage
20
+
21
+ 1. Install Thor CLIs in your project.
22
+ 2. Generate an api like the following.
23
+ `rails generate thor_hammer:api thor_cli::class_name api_path`
24
+ 3. Access the api.
25
+ `curl http://hostname/api_path/subcommnad/args1,args2`
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ Description:
2
+ This generates a controller class with URLs like `/:thor(/:subcommand(/:args))`.
3
+
4
+ Example:
5
+ `rails generate thor_hammer:api thor_cli::class_name api_path`
6
+
7
+ This will create:
8
+ Thor api controller with URLs like /api_path(/:subcommand(/:args))
@@ -0,0 +1,19 @@
1
+ module ThorHammer
2
+ class ApiGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def create_api
6
+ thor = @args.shift.underscore
7
+ api = @args.shift || thor
8
+ route api_routing(thor, api)
9
+ template "app/controllers/thor_hammer_controller.rb"
10
+ end
11
+
12
+ private
13
+
14
+ def api_routing(thor, api)
15
+ %Q(match "#{api}(/:subcommand(/:args))" => "thor_hammer#start", :defaults => {:thor => "#{thor}"})
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ require "thor_hammer/runner"
2
+ class ThorHammerController < ApplicationController
3
+ def start
4
+ render text: ThorHammer::Runner.start(params[:thor], params[:subcommand], params[:args])
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ require "thor_hammer/version"
2
+
3
+ if defined?(Rails::Railtie)
4
+ require "thor_hammer/railtie"
5
+ end
@@ -0,0 +1,7 @@
1
+ module ThorHammer
2
+ class Railtie < Rails::Railtie
3
+ generators do
4
+ require "generators/thor_hammer/api_generator"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ module ThorHammer
2
+ class Runner
3
+
4
+ def self.start(thor, subcommand, args = "")
5
+ output = capture(:stdout, :stderr) do
6
+ thor.classify.constantize.start([subcommand] + args.split(","))
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def self.capture(*streams)
13
+ streams.map!{|stream|stream.to_s}
14
+ begin
15
+ result = StringIO.new
16
+ streams.each{|stream|eval("$#{stream} = result")}
17
+ yield
18
+ ensure
19
+ streams.each{|stream|eval("$#{stream} = #{stream.upcase}")}
20
+ end
21
+ result.string
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module ThorHammer
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ require "rails"
2
+ require "test/unit"
3
+ require "rails/generators"
4
+ require "generators/thor_hammer/api_generator"
5
+
6
+ class ApiGeneratorTest < Rails::Generators::TestCase
7
+ tests ThorHammer::ApiGenerator
8
+ destination File.expand_path("../tmp", File.dirname(__FILE__))
9
+ setup :prepare_destination, :initialize_routes
10
+
11
+ def initialize_routes
12
+ Dir::mkdir("tmp/config")
13
+ f = File.open("tmp/config/routes.rb", "w")
14
+ f.print <<-EOH
15
+ Sample::Application.routes.draw do
16
+ end
17
+ EOH
18
+ f.close
19
+ end
20
+
21
+ test "Assert thor hammer controller file created" do
22
+ run_generator(["ThorTask::ClassName", "api_path"])
23
+
24
+ assert_file "app/controllers/thor_hammer_controller.rb", /class ThorHammerController < ApplicationController/
25
+ assert_file "app/controllers/thor_hammer_controller.rb", /def start/
26
+
27
+ assert_file "config/routes.rb", /match \"api_path/
28
+ end
29
+
30
+ test "Assert routes added if specified api_path" do
31
+ run_generator(["ThorTask::ClassName", "api_path"])
32
+ assert_file "config/routes.rb", /match \"api_path/
33
+ end
34
+
35
+ test "Assert routes added if not specified api_path" do
36
+ run_generator(["ThorTask::ClassName"])
37
+ assert_file "config/routes.rb", /match \"thor_task\/class_name/
38
+ end
39
+
40
+ end
@@ -0,0 +1,24 @@
1
+ require "rails"
2
+ require "test/unit"
3
+ require "thor_hammer/runner"
4
+ require "sample_thor_cli/runner"
5
+
6
+ class RunnerTest < Test::Unit::TestCase
7
+
8
+ test "Assert thor hammer runs thor cli with subcommand" do
9
+ output = ThorHammer::Runner.start("sample_thor_cli/runner", "echo")
10
+ assert_equal("", output.chomp)
11
+ end
12
+
13
+ test "Assert thor hammer runs thor cli with subcommand and a argument" do
14
+ output = ThorHammer::Runner.start("sample_thor_cli/runner", "echo", "hoge")
15
+ assert_equal("hoge", output.chomp)
16
+ end
17
+
18
+ test "Assert thor hammer runs thor cli with subcommand and arguments" do
19
+ output = ThorHammer::Runner.start("sample_thor_cli/runner", "echo", "hoge,fuga")
20
+ assert_equal("hoge fuga", output.chomp)
21
+ end
22
+
23
+ end
24
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thor_hammer/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "thor_hammer"
8
+ gem.version = ThorHammer::VERSION
9
+ gem.authors = ["monochromegane"]
10
+ gem.email = ["dev.kuro.obi@gmail.com"]
11
+ gem.description = %q{ThorHammer provides web api for your Thor CLI.}
12
+ gem.summary = %q{ThorHammer provides web api for your Thor CLI.}
13
+ gem.homepage = "https://github.com/monochromegane/thor_hammer"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "thor"
21
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thor_hammer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - monochromegane
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ThorHammer provides web api for your Thor CLI.
31
+ email:
32
+ - dev.kuro.obi@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/generators/thor_hammer/USAGE
43
+ - lib/generators/thor_hammer/api_generator.rb
44
+ - lib/generators/thor_hammer/templates/app/controllers/thor_hammer_controller.rb
45
+ - lib/thor_hammer.rb
46
+ - lib/thor_hammer/railtie.rb
47
+ - lib/thor_hammer/runner.rb
48
+ - lib/thor_hammer/version.rb
49
+ - test/api_generator_test.rb
50
+ - test/runner_test.rb
51
+ - thor_hammer.gemspec
52
+ homepage: https://github.com/monochromegane/thor_hammer
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.23
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: ThorHammer provides web api for your Thor CLI.
76
+ test_files:
77
+ - test/api_generator_test.rb
78
+ - test/runner_test.rb