moko 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/mokoup +53 -0
- data/lib/defaults.yml +5 -0
- data/lib/moko.rb +89 -0
- data/lib/template/resource.erb +71 -0
- metadata +82 -0
data/bin/mokoup
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
require 'moko'
|
7
|
+
|
8
|
+
class MokoUp < Thor
|
9
|
+
include Thor::Actions
|
10
|
+
|
11
|
+
no_commands do
|
12
|
+
def download_moco_if_needed
|
13
|
+
moco_dir = File.join(File.dirname(File.expand_path(__FILE__)), 'moco')
|
14
|
+
FileUtils.mkdir_p moco_dir
|
15
|
+
|
16
|
+
moco_standalone = File.join(File.dirname(File.expand_path(__FILE__)), "moco/moco-runner-standalone.jar")
|
17
|
+
unless File.exist? moco_standalone then
|
18
|
+
puts "Downloading moco-runner..."
|
19
|
+
Net::HTTP.start("repo1.maven.org") do |http|
|
20
|
+
resp = http.get("/maven2/com/github/dreamhead/moco-runner/0.9.1/moco-runner-0.9.1-standalone.jar")
|
21
|
+
open(moco_standalone, "wb") do |file|
|
22
|
+
file.write(resp.body)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
puts "moco-runner is up-to-date"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def prepare_env
|
30
|
+
FileUtils.mkdir_p 'resources'
|
31
|
+
FileUtils.mkdir_p 'conf'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "server", "startup the underlying moco server"
|
36
|
+
method_option :server, :aliases => '-s'
|
37
|
+
def server
|
38
|
+
download_moco_if_needed
|
39
|
+
moco = File.join(File.dirname(File.expand_path(__FILE__)), "moco/moco-runner-standalone.jar")
|
40
|
+
run "java -jar #{moco} start -p 12306 -c conf/moko.conf.json"
|
41
|
+
end
|
42
|
+
|
43
|
+
desc "generate", "generate moco configuration and restful resources"
|
44
|
+
method_option :generate, :aliases => '-g'
|
45
|
+
def generate config="moko.up"
|
46
|
+
prepare_env
|
47
|
+
cfg = File.read(config)
|
48
|
+
eval "Moko::Server.draw do\n" + cfg + "\nend", TOPLEVEL_BINDING, config, 0
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
MokoUp.start
|
data/lib/defaults.yml
ADDED
data/lib/moko.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'json'
|
3
|
+
require 'yaml'
|
4
|
+
require 'active_support/inflector'
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
module Moko
|
8
|
+
class Dual
|
9
|
+
def initialize
|
10
|
+
@fields = {}
|
11
|
+
f = File.join(File.dirname(File.expand_path(__FILE__)), 'defaults.yml')
|
12
|
+
@defaults = YAML.load_file(f)['defaults']
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(method, *args, &block)
|
16
|
+
attr = method.to_s
|
17
|
+
key = args[0]
|
18
|
+
|
19
|
+
if @defaults.key?(attr)
|
20
|
+
@fields[key] = @defaults[attr]
|
21
|
+
else
|
22
|
+
super.method_missing(method, args, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def render
|
27
|
+
JSON.pretty_generate(@fields)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Item
|
32
|
+
attr_accessor :item
|
33
|
+
def initialize(item)
|
34
|
+
@item = item
|
35
|
+
end
|
36
|
+
def get_binding
|
37
|
+
binding
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Server
|
42
|
+
attr_reader :items
|
43
|
+
|
44
|
+
def self.draw &block
|
45
|
+
server = Server.new
|
46
|
+
server.instance_eval(&block)
|
47
|
+
server.render
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def touch_resources
|
52
|
+
@items.each do |item|
|
53
|
+
FileUtils.touch "resources/#{item}.json"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def render
|
58
|
+
@items = @items.uniq
|
59
|
+
touch_resources
|
60
|
+
|
61
|
+
result = @items.reduce([]) do |result, item|
|
62
|
+
result << ERB.new(@template).result(Item.new(item).get_binding)
|
63
|
+
end
|
64
|
+
resources = "[#{result.join(',')}]"
|
65
|
+
|
66
|
+
File.open("conf/moko.conf.json", "w") { |f| f.write(resources) }
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
def initialize
|
71
|
+
f = File.join(File.dirname(File.expand_path(__FILE__)), 'template/resource.erb')
|
72
|
+
@template = File.read(f)
|
73
|
+
@items = []
|
74
|
+
end
|
75
|
+
|
76
|
+
def resources *arguments
|
77
|
+
arguments.each do |item|
|
78
|
+
@items << item
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def resource res, &block
|
83
|
+
obj = Moko::Dual.new
|
84
|
+
obj.instance_eval(&block) if block_given?
|
85
|
+
resources res.to_s.pluralize.to_sym
|
86
|
+
File.open("resources/#{res.to_s.pluralize}.json", "w") { |io| io.write(obj.render) }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
{
|
2
|
+
"request": {
|
3
|
+
"method": "get",
|
4
|
+
"uri": "/<%= item %>"
|
5
|
+
},
|
6
|
+
"response": {
|
7
|
+
"status": 200,
|
8
|
+
"headers" : {
|
9
|
+
"content-type" : "application/json"
|
10
|
+
},
|
11
|
+
"file": "resources/<%= item %>.json"
|
12
|
+
}
|
13
|
+
},
|
14
|
+
{
|
15
|
+
"request": {
|
16
|
+
"method": "get",
|
17
|
+
"uri": {
|
18
|
+
"match": "/<%= item %>/\\d+"
|
19
|
+
}
|
20
|
+
},
|
21
|
+
"response": {
|
22
|
+
"status": 200,
|
23
|
+
"headers" : {
|
24
|
+
"content-type" : "application/json"
|
25
|
+
},
|
26
|
+
"file": "resources/<%= item %>.json"
|
27
|
+
}
|
28
|
+
},
|
29
|
+
{
|
30
|
+
"request": {
|
31
|
+
"method": "post",
|
32
|
+
"uri": "/<%= item %>"
|
33
|
+
},
|
34
|
+
"response": {
|
35
|
+
"status": 201,
|
36
|
+
"headers" : {
|
37
|
+
"content-type" : "application/json"
|
38
|
+
},
|
39
|
+
"file": "resources/<%= item %>.json"
|
40
|
+
}
|
41
|
+
},
|
42
|
+
{
|
43
|
+
"request": {
|
44
|
+
"method": "put",
|
45
|
+
"uri": {
|
46
|
+
"match": "/<%= item %>/\\d+"
|
47
|
+
}
|
48
|
+
},
|
49
|
+
"response": {
|
50
|
+
"status": 200,
|
51
|
+
"headers" : {
|
52
|
+
"content-type" : "application/json"
|
53
|
+
},
|
54
|
+
"file": "resources/<%= item %>.json"
|
55
|
+
}
|
56
|
+
},
|
57
|
+
{
|
58
|
+
"request": {
|
59
|
+
"method": "delete",
|
60
|
+
"uri": {
|
61
|
+
"match": "/<%= item %>/\\d+"
|
62
|
+
}
|
63
|
+
},
|
64
|
+
"response": {
|
65
|
+
"status": 200,
|
66
|
+
"headers" : {
|
67
|
+
"content-type" : "application/json"
|
68
|
+
},
|
69
|
+
"file": "resources/<%= item %>.json"
|
70
|
+
}
|
71
|
+
}
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moko
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Juntao Qiu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-02-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.0.2
|
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: 4.0.2
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: thor
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.18.1
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.18.1
|
46
|
+
description: moko is used for build restful API in secs, for testing puropse
|
47
|
+
email: juntao.qiu@gmail.com
|
48
|
+
executables:
|
49
|
+
- mokoup
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/moko.rb
|
54
|
+
- lib/template/resource.erb
|
55
|
+
- lib/defaults.yml
|
56
|
+
- bin/mokoup
|
57
|
+
homepage: https://github.com/abruzzi/moko
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.28
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: moko is a helper for build restful API in secs
|
82
|
+
test_files: []
|