Class: Honey::Command::Preview

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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)
  @options = OpenStruct.new(opts)
  @options.path         ||= "apiary.apib"
  @options.api_host     ||= "api.apiary.io"
  @options.headers      ||= {:accept => "text/html", :content_type => "text/plain"}
  @options.port         ||= 8080
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.


17
18
19
# File 'lib/honey/command/preview.rb', line 17

def options
  @options
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

#browserObject


50
51
52
# File 'lib/honey/command/preview.rb', line 50

def browser
  BROWSERS[@options.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(@options.api_host, path))
    @options.output ? write_generated_path(file.path, @options.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

#pathObject


46
47
48
# File 'lib/honey/command/preview.rb', line 46

def path
  @options.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(@options.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, @options.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_serverObject


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(@options.api_host, @options.path)
  end

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

#serverObject


32
33
34
# File 'lib/honey/command/preview.rb', line 32

def server
  run_server
end

#showObject


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