Class: Honey::Command::Preview
- Inherits:
-
Object
- Object
- Honey::Command::Preview
- Defined in:
- lib/honey/command/preview.rb
Overview
Display preview of local blueprint file
Constant Summary collapse
- BROWSERS =
{ :safari => "Safari", :chrome => "Google Chrome", :firefox => "Firefox" }
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #browser ⇒ Object
- #generate_static(path) ⇒ Object
-
#initialize(opts) ⇒ Preview
constructor
TODO: use OpenStruct to store @options.
-
#open_generated_page(path) ⇒ Object
TODO: add linux and windows systems.
- #path ⇒ Object
- #preview_path(path) ⇒ Object
- #query_apiary(host, path) ⇒ Object
- #rack_app(&block) ⇒ Object
- #run_server ⇒ Object
- #server ⇒ Object
- #show ⇒ Object
- #validate_apib_file(apib_file) ⇒ Object
- #write_generated_path(path, outfile) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Preview
TODO: use OpenStruct to store @options
20 21 22 23 24 25 26 |
# File 'lib/honey/command/preview.rb', line 20 def initialize(opts) = OpenStruct.new(opts) .path ||= "apiary.apib" .api_host ||= "api.apiary.io" .headers ||= {:accept => "text/html", :content_type => "text/plain"} .port ||= 8080 end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/honey/command/preview.rb', line 17 def end |
Class Method Details
.execute(args) ⇒ Object
28 29 30 |
# File 'lib/honey/command/preview.rb', line 28 def self.execute(args) args[:server] ? new(args).server : new(args).show end |
Instance Method Details
#browser ⇒ Object
50 51 52 |
# File 'lib/honey/command/preview.rb', line 50 def browser BROWSERS[.browser] || nil end |
#generate_static(path) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/honey/command/preview.rb', line 90 def generate_static(path) File.open(preview_path(path), "w") do |file| file.write(query_apiary(.api_host, path)) .output ? write_generated_path(file.path, .output) : open_generated_page(file.path) end end |
#open_generated_page(path) ⇒ Object
TODO: add linux and windows systems
80 81 82 |
# File 'lib/honey/command/preview.rb', line 80 def open_generated_page(path) exec "open #{browser_options} #{path}" end |
#path ⇒ Object
46 47 48 |
# File 'lib/honey/command/preview.rb', line 46 def path .path || "#{File.basename(Dir.pwd)}.apib" end |
#preview_path(path) ⇒ Object
68 69 70 71 |
# File 'lib/honey/command/preview.rb', line 68 def preview_path(path) basename = File.basename(.path) "/tmp/#{basename}-preview.html" end |
#query_apiary(host, path) ⇒ Object
73 74 75 76 77 |
# File 'lib/honey/command/preview.rb', line 73 def query_apiary(host, path) url = "https://#{host}/blueprint/generate" data = File.read(path) RestClient.post(url, data, .headers) end |
#rack_app(&block) ⇒ Object
54 55 56 57 58 |
# File 'lib/honey/command/preview.rb', line 54 def rack_app(&block) Rack::Builder.new do run lambda { |env| [200, Hash.new, [block.call]] } end end |
#run_server ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/honey/command/preview.rb', line 60 def run_server app = self.rack_app do self.query_apiary(.api_host, .path) end Rack::Server.start(:Port => .port, :app => app) end |
#server ⇒ Object
32 33 34 |
# File 'lib/honey/command/preview.rb', line 32 def server run_server end |
#show ⇒ Object
36 37 38 |
# File 'lib/honey/command/preview.rb', line 36 def show generate_static(path) end |
#validate_apib_file(apib_file) ⇒ Object
40 41 42 43 44 |
# File 'lib/honey/command/preview.rb', line 40 def validate_apib_file(apib_file) unless File.exist?(apib_file) abort "Apiary definition file hasn't been found: #{apib_file.inspect}" end end |
#write_generated_path(path, outfile) ⇒ Object
84 85 86 87 88 |
# File 'lib/honey/command/preview.rb', line 84 def write_generated_path(path, outfile) File.open(outfile, 'w') do |file| file.write(File.open(path, 'r').read) end end |