apical 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +24 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +94 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/apical.gemspec +122 -0
- data/bin/apical +7 -0
- data/examples/taco_truck/Gemfile +7 -0
- data/examples/taco_truck/Gemfile.lock +50 -0
- data/examples/taco_truck/app.rb +13 -0
- data/examples/taco_truck/taco_truck.apical +18 -0
- data/lib/apical.rb +30 -0
- data/lib/apical/adapter.rb +21 -0
- data/lib/apical/adapters/http_adapter.rb +30 -0
- data/lib/apical/adapters/rack_adapter.rb +16 -0
- data/lib/apical/cli.rb +36 -0
- data/lib/apical/content_types.rb +62 -0
- data/lib/apical/resource.rb +138 -0
- data/lib/apical/resource_types.rb +36 -0
- data/lib/apical/runner.rb +104 -0
- data/lib/apical/writers/console_writer.rb +26 -0
- data/lib/apical/writers/html_writer.rb +37 -0
- data/spec/apical/adapters/http_adapter_spec.rb +24 -0
- data/spec/apical/cli_spec.rb +81 -0
- data/spec/apical/content_types_spec.rb +9 -0
- data/spec/apical/rack_adapter_spec.rb +12 -0
- data/spec/apical_spec.rb +360 -0
- data/spec/before_and_after_spec.rb +211 -0
- data/spec/fixtures/cli_example_1.rb +7 -0
- data/spec/fixtures/cli_example_2.rb +8 -0
- data/spec/fixtures/example_require.rb +3 -0
- data/spec/fixtures/load_paths_example.apical +1 -0
- data/spec/html_writer_spec.rb +117 -0
- data/spec/http_apical_spec.rb +23 -0
- data/spec/load_paths_spec.rb +12 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/support/test_apps.rb +34 -0
- data/templates/apical_helper.rb +11 -0
- data/templates/layout.mustache +180 -0
- data/templates/resource.mustache +14 -0
- metadata +248 -0
@@ -0,0 +1,211 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "before and after blocks" do
|
4
|
+
|
5
|
+
describe "when specifying a global before block" do
|
6
|
+
before do
|
7
|
+
$var = 0
|
8
|
+
@doc = Apical.new do
|
9
|
+
adapter :rack, app: TestApp
|
10
|
+
before do
|
11
|
+
$var += 1
|
12
|
+
end
|
13
|
+
|
14
|
+
get '/tacos.json' do
|
15
|
+
desc "Get all tacos as JSON"
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/tacos.xml' do
|
19
|
+
desc "Get all tacos as XML"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
@doc.run
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should have run the before block twice, once for each resource" do
|
27
|
+
$var.should == 2
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "when specifying a global before each block" do
|
32
|
+
before do
|
33
|
+
$var = 0
|
34
|
+
@doc = Apical.new do
|
35
|
+
adapter :rack, app: TestApp
|
36
|
+
before :each do
|
37
|
+
$var += 1
|
38
|
+
end
|
39
|
+
|
40
|
+
get '/tacos.json' do
|
41
|
+
desc "Get all tacos as JSON"
|
42
|
+
end
|
43
|
+
|
44
|
+
get '/tacos.xml' do
|
45
|
+
desc "Get all tacos as XML"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
@doc.run
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should have run the before block twice, once for each resource" do
|
53
|
+
$var.should == 2
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "when specifying a global before all block" do
|
58
|
+
before do
|
59
|
+
$var = 0
|
60
|
+
@doc = Apical.new do
|
61
|
+
adapter :rack, app: TestApp
|
62
|
+
before :all do
|
63
|
+
$var += 1
|
64
|
+
end
|
65
|
+
|
66
|
+
get '/tacos.json' do
|
67
|
+
desc "Get all tacos as JSON"
|
68
|
+
end
|
69
|
+
|
70
|
+
get '/tacos.xml' do
|
71
|
+
desc "Get all tacos as XML"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
@doc.run
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should have run the before block once and only once" do
|
79
|
+
$var.should == 1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "when specifying a global after block" do
|
84
|
+
before do
|
85
|
+
$var = 0
|
86
|
+
@doc = Apical.new do
|
87
|
+
adapter :rack, app: TestApp
|
88
|
+
after do
|
89
|
+
$var += 1
|
90
|
+
end
|
91
|
+
|
92
|
+
get '/tacos.json' do
|
93
|
+
desc "Get all tacos as JSON"
|
94
|
+
end
|
95
|
+
|
96
|
+
get '/tacos.xml' do
|
97
|
+
desc "Get all tacos as XML"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
@doc.run
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should have run the after block twice, once for each resource" do
|
105
|
+
$var.should == 2
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "when specifying a global after each block" do
|
110
|
+
before do
|
111
|
+
$var = 0
|
112
|
+
@doc = Apical.new do
|
113
|
+
adapter :rack, app: TestApp
|
114
|
+
after :each do
|
115
|
+
$var += 1
|
116
|
+
end
|
117
|
+
|
118
|
+
get '/tacos.json' do
|
119
|
+
desc "Get all tacos as JSON"
|
120
|
+
end
|
121
|
+
|
122
|
+
get '/tacos.xml' do
|
123
|
+
desc "Get all tacos as XML"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
@doc.run
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should have run the after block twice, once for each resource" do
|
131
|
+
$var.should == 2
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe "when specifying a global after all block" do
|
136
|
+
before do
|
137
|
+
$var = 0
|
138
|
+
@doc = Apical.new do
|
139
|
+
adapter :rack, app: TestApp
|
140
|
+
after :all do
|
141
|
+
$var += 1
|
142
|
+
end
|
143
|
+
|
144
|
+
get '/tacos.json' do
|
145
|
+
desc "Get all tacos as JSON"
|
146
|
+
end
|
147
|
+
|
148
|
+
get '/tacos.xml' do
|
149
|
+
desc "Get all tacos as XML"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
@doc.run
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should have run the after block once and only once" do
|
157
|
+
$var.should == 1
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe "when specifying a resource-specific before block" do
|
162
|
+
before do
|
163
|
+
$var = 0
|
164
|
+
@doc = Apical.new do
|
165
|
+
adapter :rack, app: TestApp
|
166
|
+
get '/tacos.json' do
|
167
|
+
desc "Get all tacos as JSON"
|
168
|
+
before do
|
169
|
+
$var += 1
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
get '/tacos.xml' do
|
174
|
+
desc "Get all tacos as XML"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
@doc.run
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should only run the before block for its resource" do
|
182
|
+
$var.should == 1
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe "when specifying a resource-specific after block" do
|
187
|
+
before do
|
188
|
+
$var = 0
|
189
|
+
@doc = Apical.new do
|
190
|
+
adapter :rack, app: TestApp
|
191
|
+
get '/tacos.json' do
|
192
|
+
desc "Get all tacos as JSON"
|
193
|
+
after do
|
194
|
+
$var += 1
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
get '/tacos.xml' do
|
199
|
+
desc "Get all tacos as XML"
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
@doc.run
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should only run the before block for its resource" do
|
207
|
+
$var.should == 1
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'example_require'
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Apical::HtmlWriter do
|
4
|
+
before do
|
5
|
+
@doc = Apical.new do
|
6
|
+
adapter :rack, app: TestApp
|
7
|
+
get '/tacos.json' do
|
8
|
+
desc "Get all tacos"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "when writing to HTML" do
|
14
|
+
subject do
|
15
|
+
stream = StringIO.new
|
16
|
+
Apical::HtmlWriter.new(@doc).write(stream)
|
17
|
+
stream.read
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should write the request method" do
|
21
|
+
subject.should include('GET')
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should write the request path" do
|
25
|
+
subject.should include("/tacos.json")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should write the response body" do
|
29
|
+
subject.should include('meat')
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "when accepting JSON as input" do
|
33
|
+
before do
|
34
|
+
@doc = Apical.new do
|
35
|
+
adapter :rack, app: TestApp
|
36
|
+
post '/tacos.json' do
|
37
|
+
desc "Make a new delicious taco"
|
38
|
+
accept :json
|
39
|
+
params do
|
40
|
+
{ meat: 'beef', lettuce: true }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
@doc.run
|
46
|
+
end
|
47
|
+
|
48
|
+
subject do
|
49
|
+
stream = StringIO.new
|
50
|
+
Apical::HtmlWriter.new(@doc).write(stream)
|
51
|
+
stream.read
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should pretty-print the JSON data in the IN section" do
|
55
|
+
subject.should include("{\n "meat": "beef",\n "lettuce": true\n}")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "when setting the default accept and content type" do
|
60
|
+
before do
|
61
|
+
@doc = Apical.new do
|
62
|
+
adapter :rack, app: TestApp
|
63
|
+
accept :json
|
64
|
+
content_type :json
|
65
|
+
|
66
|
+
post '/tacos.json' do
|
67
|
+
desc "Make a new delicious taco"
|
68
|
+
params do
|
69
|
+
{ meat: 'beef', lettuce: true }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
@doc.run
|
75
|
+
end
|
76
|
+
|
77
|
+
subject { @doc }
|
78
|
+
|
79
|
+
it "should propagate the accept type to its resources" do
|
80
|
+
@doc.resources.first.accept.should be_a(Apical::JsonContentType)
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should propagate the content type to its resources" do
|
84
|
+
@doc.resources.first.content_type.should be_a(Apical::JsonContentType)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "when providing JSON as output" do
|
89
|
+
before do
|
90
|
+
@doc = Apical.new do
|
91
|
+
adapter :rack, app: TestApp
|
92
|
+
post '/tacos.json' do
|
93
|
+
desc "Make a new delicious taco"
|
94
|
+
accept :json
|
95
|
+
content_type :json
|
96
|
+
params do
|
97
|
+
{ meat: 'beef', lettuce: true }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
@doc.run
|
103
|
+
end
|
104
|
+
|
105
|
+
subject do
|
106
|
+
stream = StringIO.new
|
107
|
+
Apical::HtmlWriter.new(@doc).write(stream)
|
108
|
+
stream.read
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should pretty-print the JSON data in the OUT section" do
|
112
|
+
subject.should include("{\n "meat": "beef",\n "lettuce": true\n}")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "creating an apical from a real running HTTP server" do
|
4
|
+
before do
|
5
|
+
stub_request(:get, "http://mocksite.com/resource.json").
|
6
|
+
to_return(:status => 200, :body => "this is a resource", :headers => {})
|
7
|
+
|
8
|
+
@doc = Apical.new do
|
9
|
+
adapter :http, base_uri: "http://mocksite.com"
|
10
|
+
|
11
|
+
get "/resource.json" do
|
12
|
+
desc "A test GET resource"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
subject { @doc.run }
|
18
|
+
|
19
|
+
it "should have run the request" do
|
20
|
+
subject.resources.first.response_body.should == "this is a resource"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "an apical file with a relative require line" do
|
4
|
+
before do
|
5
|
+
@cli = Apical::CLI.new
|
6
|
+
@cli.compile File.dirname(__FILE__) + '/fixtures/load_paths_example.apical'
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should load the file from within the same directory as the apical file" do
|
10
|
+
$required.should == true
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'apical'
|
5
|
+
|
6
|
+
require 'webmock/rspec'
|
7
|
+
|
8
|
+
# Requires supporting files with custom matchers and macros, etc,
|
9
|
+
# in ./support/ and its subdirectories.
|
10
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
11
|
+
|
12
|
+
ROOT = Pathname.new(File.dirname(__FILE__) + '/../')
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
|
16
|
+
config.before(:all) do
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
class TestApp < Sinatra::Base
|
4
|
+
get '/tacos.json' do
|
5
|
+
JSON.generate [ { meat: 'beef' }, { meat: 'chicken' } ]
|
6
|
+
end
|
7
|
+
|
8
|
+
post '/tacos.json' do
|
9
|
+
JSON.generate( { body: request.body.read } )
|
10
|
+
end
|
11
|
+
|
12
|
+
get '/tacos/:id.json' do
|
13
|
+
JSON.generate({ meat: 'beef', id: params['id'] })
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class RequestMethodsApp < Sinatra::Base
|
18
|
+
get '/get.json' do
|
19
|
+
JSON.generate({ method: 'GET' })
|
20
|
+
end
|
21
|
+
post '/post.json' do
|
22
|
+
JSON.generate({ method: 'POST' })
|
23
|
+
end
|
24
|
+
put '/put.json' do
|
25
|
+
JSON.generate({ method: 'PUT' })
|
26
|
+
end
|
27
|
+
delete '/delete.json' do
|
28
|
+
JSON.generate({ method: 'DELETE' })
|
29
|
+
end
|
30
|
+
options '/options.json' do
|
31
|
+
JSON.generate({ method: 'OPTIONS' })
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|