Class: Apiary::Command::Preview
Overview
Display preview of local blueprint file
Constant Summary
collapse
- PREVIEW_TEMPLATE_PATH =
"#{File.expand_path File.dirname(__FILE__)}/../file_templates/preview.erb".freeze
Helpers::JavascriptHelper::JS_ESCAPE_MAP
Instance Attribute Summary collapse
Instance Method Summary
collapse
#escape_javascript
Methods included from Helpers
#api_description_source, #api_description_source_path
Constructor Details
#initialize(opts) ⇒ Preview
Returns a new instance of Preview.
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/apiary/command/preview.rb', line 24
def initialize(opts)
@options = OpenStruct.new(opts)
@options.path ||= '.'
@options.api_host ||= 'api.apiary.io'
@options.port ||= 8080
@options.proxy ||= ENV['http_proxy']
@options.server ||= false
@options.host ||= '127.0.0.1'
@options. ||= {
accept: 'text/html',
content_type: 'text/plain',
user_agent: Apiary.user_agent
}
begin
@source_path = api_description_source_path(@options.path)
rescue StandardError => e
abort "#{e.message}"
end
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
22
23
24
|
# File 'lib/apiary/command/preview.rb', line 22
def options
@options
end
|
Instance Method Details
#execute ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/apiary/command/preview.rb', line 45
def execute
if @options.server
server
else
show
end
end
|
#generate ⇒ Object
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/apiary/command/preview.rb', line 91
def generate
template = load_preview_template
data = {
title: File.basename(@source_path, '.*'),
source: api_description_source(@source_path)
}
template.result(binding)
end
|
#load_preview_template ⇒ Object
108
109
110
111
112
|
# File 'lib/apiary/command/preview.rb', line 108
def load_preview_template
file = File.open(PREVIEW_TEMPLATE_PATH, 'r')
template_string = file.read
ERB.new(template_string)
end
|
#open_generated_page(path) ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/apiary/command/preview.rb', line 77
def open_generated_page(path)
def_browser = ENV['BROWSER']
ENV['BROWSER'] = @options.browser
Launchy.open(path) do |e|
puts "Attempted to open `#{path}` and failed because #{e}"
end
ENV['BROWSER'] = def_browser
end
|
#preview_path ⇒ Object
102
103
104
105
106
|
# File 'lib/apiary/command/preview.rb', line 102
def preview_path
basename = File.basename(@source_path, '.*')
temp = Dir.tmpdir
"#{temp}/#{basename}-preview.html"
end
|
#rack_app ⇒ Object
71
72
73
74
75
|
# File 'lib/apiary/command/preview.rb', line 71
def rack_app
Rack::Builder.new do
run ->(env) { [200, {}, [yield]] }
end
end
|
#server ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/apiary/command/preview.rb', line 53
def server
app = rack_app do
generate
end
Rack::Server.start(Port: @options.port, Host: @options.host, app: app)
end
|
#show ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/apiary/command/preview.rb', line 61
def show
preview_string = generate
File.open(preview_path, 'w') do |file|
file.write preview_string
file.flush
@options.output ? write_generated_path(file.path, @options.output) : open_generated_page(file.path)
end
end
|
#write_generated_path(path, outfile) ⇒ Object
87
88
89
|
# File 'lib/apiary/command/preview.rb', line 87
def write_generated_path(path, outfile)
File.write(outfile, File.read(path))
end
|