Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit a72ae73

Browse files
committed
Updated preview command to display new documentation.
1 parent 8bf6f6f commit a72ae73

File tree

5 files changed

+109
-48
lines changed

5 files changed

+109
-48
lines changed

lib/apiary.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require "apiary/version"
33
require "apiary/cli"
44
require "apiary/common"
5-
Dir["#{File.dirname(__FILE__)}/apiary/command/*.rb"].each { |f| require(f) }
65

76
module Apiary
87
end

lib/apiary/command/preview.rb

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
require 'ostruct'
55
require 'json'
66
require 'tmpdir'
7+
require 'erb'
78

89
require "apiary/common"
10+
require "apiary/helpers/javascript_helper"
911

1012
module Apiary
1113
module Command
1214
# Display preview of local blueprint file
1315
class Preview
1416

17+
include Apiary::Helpers::JavascriptHelper
18+
19+
PREVIEW_TEMPLATE_PATH = "#{File.expand_path File.dirname(__FILE__)}/../file_templates/preview.erb"
20+
1521
BROWSERS = {
1622
:safari => "Safari",
1723
:chrome => "Google Chrome",
@@ -28,6 +34,8 @@ def initialize(opts)
2834
@options.port ||= 8080
2935
@options.proxy ||= ENV['http_proxy']
3036
@options.server ||= false
37+
38+
validate_apib_file()
3139
end
3240

3341
def execute
@@ -43,12 +51,12 @@ def server
4351
end
4452

4553
def show
46-
generate_static(@options.path)
54+
generate_static
4755
end
4856

49-
def validate_apib_file(apib_file)
57+
def validate_apib_file
5058
common = Apiary::Common.new
51-
common.validate_apib_file(apib_file)
59+
common.validate_apib_file(@options.path)
5260
end
5361

5462
def path
@@ -67,46 +75,12 @@ def rack_app(&block)
6775

6876
def run_server
6977
app = self.rack_app do
70-
self.query_apiary(@options.api_host, @options.path)
78+
self.generate
7179
end
7280

7381
Rack::Server.start(:Port => @options.port, :app => app)
7482
end
7583

76-
def preview_path(path)
77-
basename = File.basename(@options.path)
78-
temp = Dir.tmpdir()
79-
"#{temp}/#{basename}-preview.html"
80-
end
81-
82-
def query_apiary(host, path)
83-
url = "https://#{host}/blueprint/generate"
84-
if validate_apib_file(path)
85-
begin
86-
data = File.read(path)
87-
rescue
88-
abort "File #{path} not found."
89-
end
90-
91-
RestClient.proxy = @options.proxy
92-
93-
begin
94-
RestClient.post(url, data, @options.headers)
95-
rescue RestClient::BadRequest => e
96-
err = JSON.parse e.response
97-
if err.has_key? 'parserError'
98-
abort "#{err['message']}: #{err['parserError']} (Line: #{err['line']}, Column: #{err['column']})"
99-
else
100-
abort "Apiary service responded with an error: #{err['message']}"
101-
end
102-
rescue RestClient::Exception => e
103-
abort "Apiary service responded with an error: #{e.message}"
104-
end
105-
else
106-
abort "Sorry, Apiary can't display invalid blueprint."
107-
end
108-
end
109-
11084
# TODO: add linux and windows systems
11185
def open_generated_page(path)
11286
exec "open #{browser_options} #{path}"
@@ -116,17 +90,49 @@ def write_generated_path(path, outfile)
11690
File.write(outfile, File.read(path))
11791
end
11892

119-
def generate_static(path)
120-
File.open(preview_path(path), "w") do |file|
121-
file.write(query_apiary(@options.api_host, path))
93+
def generate
94+
template = load_preview_template()
95+
96+
data = {
97+
title: File.basename(@options.path, ".*"),
98+
blueprint: load_blueprint()
99+
}
100+
101+
template.result(binding)
102+
end
103+
104+
def generate_static
105+
preview_string = generate
106+
107+
File.open(preview_path, "w") do |file|
108+
file.write preview_string
109+
file.flush
122110
@options.output ? write_generated_path(file.path, @options.output) : open_generated_page(file.path)
123111
end
124112
end
125113

114+
def load_blueprint
115+
file = File.open @options.path, "r"
116+
file.read
117+
end
118+
119+
def preview_path
120+
basename = File.basename(@options.path, ".*")
121+
temp = Dir.tmpdir()
122+
"#{temp}/#{basename}-preview.html"
123+
end
124+
125+
def load_preview_template
126+
file = File.open(PREVIEW_TEMPLATE_PATH, "r")
127+
template_string = file.read()
128+
ERB.new(template_string)
129+
end
130+
126131
private
127-
def browser_options
128-
"-a #{BROWSERS[@options.browser.to_sym]}" if @options.browser
129-
end
132+
133+
def browser_options
134+
"-a #{BROWSERS[@options.browser.to_sym]}" if @options.browser
135+
end
130136
end
131137
end
132138
end

lib/apiary/file_templates/preview.erb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title><%= data[:title] %></title>
6+
</head>
7+
<body>
8+
<script src="https://api.apiary.io/seeds/embed.js"></script>
9+
<script>
10+
var embed = new Apiary.Embed({
11+
apiBlueprint: "<%= escape_javascript data[:blueprint] %>"
12+
});
13+
</script>
14+
</body>
15+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Apiary
2+
module Helpers
3+
module JavascriptHelper
4+
JS_ESCAPE_MAP = {
5+
'\\' => '\\\\',
6+
'</' => '<\/',
7+
"\r\n" => '\n',
8+
"\n" => '\n',
9+
"\r" => '\n',
10+
'"' => '\\"',
11+
"'" => "\\'"
12+
}
13+
14+
JS_ESCAPE_MAP["\342\200\250".force_encoding(Encoding::UTF_8).encode!] = '&#x2028;'
15+
JS_ESCAPE_MAP["\342\200\251".force_encoding(Encoding::UTF_8).encode!] = '&#x2029;'
16+
17+
def escape_javascript(javascript)
18+
if javascript
19+
javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] }
20+
else
21+
''
22+
end
23+
end
24+
25+
alias_method :j, :escape_javascript
26+
end
27+
end
28+
end

spec/command/preview_spec.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,23 @@
22

33
describe Apiary::Command::Preview do
44

5+
let(:command) do
6+
opts = {
7+
path: "#{File.expand_path File.dirname(__FILE__)}/../../features/fixtures/apiary.apib"
8+
}
9+
Apiary::Command::Preview.new(opts)
10+
end
11+
512
it 'check tmp path if contains filename' do
6-
opts = {}
7-
command = Apiary::Command::Preview.new(opts)
8-
expect(command.preview_path('apiary.apib')).to end_with('apiary.apib-preview.html')
13+
expect(command.preview_path()).to end_with('apiary-preview.html')
14+
end
15+
16+
it 'shoud contain html5 doctype' do
17+
expect(command.generate()).to include('<!DOCTYPE html>')
18+
end
19+
20+
it 'should contain embed javascript' do
21+
expect(command.generate()).to include('https://api.apiary.io/seeds/embed.js')
922
end
1023

1124
end

0 commit comments

Comments
 (0)