Class: Apiary::Command::Preview

Inherits:
Object
  • Object
show all
Includes:
Helpers, Helpers::JavascriptHelper
Defined in:
lib/apiary/command/preview.rb

Overview

Display preview of local blueprint file

Constant Summary collapse

PREVIEW_TEMPLATE_PATH =
"#{File.expand_path File.dirname(__FILE__)}/../file_templates/preview.erb".freeze

Constants included from Helpers::JavascriptHelper

Helpers::JavascriptHelper::JS_ESCAPE_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::JavascriptHelper

#escape_javascript

Methods included from Helpers

#api_description_source, #api_description_source_path, #convert_from_json

Constructor Details

#initialize(opts) ⇒ Preview

Returns a new instance of Preview.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/apiary/command/preview.rb', line 25

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.json         ||= false
  @options.watch        ||= false
  @options.interval     ||= 1000
  @options.host         ||= '127.0.0.1'
  @options.headers      ||= {
    accept: 'text/html',
    content_type: 'text/plain',
    user_agent: Apiary.user_agent
  }

  @changed = timestamp

  begin
    @source_path = api_description_source_path(@options.path)
  rescue StandardError => e
    abort "#{e.message}"
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/apiary/command/preview.rb', line 23

def options
  @options
end

Instance Method Details

#executeObject



51
52
53
54
55
56
57
58
# File 'lib/apiary/command/preview.rb', line 51

def execute
  if @options.server || @options.watch
    watch
    server
  else
    show
  end
end

#generateObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/apiary/command/preview.rb', line 124

def generate
  template = load_preview_template
  source = api_description_source(@source_path)

  return if source.nil?

  begin
    JSON.parse(source)
    abort('Did you forget the --json flag') unless @options.json
  rescue; end
  source = convert_from_json(source) if @options.json

  data = {
    title: File.basename(@source_path, '.*'),
    source: source,
    interval: @options.interval,
    watch: @options.watch
  }

  template.result(binding)
end

#get_app(path: '/', options: {}) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/apiary/command/preview.rb', line 102

def get_app(path: '/', options: {})
  Rack::Builder.new do
    map path do
      run ->(env) { [200, options, [yield]] }
    end
  end
end

#load_preview_templateObject



152
153
154
155
156
# File 'lib/apiary/command/preview.rb', line 152

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



110
111
112
113
114
115
116
117
118
# File 'lib/apiary/command/preview.rb', line 110

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_pathObject



146
147
148
149
150
# File 'lib/apiary/command/preview.rb', line 146

def preview_path
  basename = File.basename(@source_path, '.*')
  temp = Dir.tmpdir
  "#{temp}/#{basename}-preview.html"
end

#serverObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/apiary/command/preview.rb', line 72

def server
  generate_app = get_app(path: '/') do
    generate
  end

  change_app = get_app(path: '/changed', options: { 'Content-Type' => 'text/plain' }) do
    @changed
  end

  source_app = get_app(path: '/source', options: { 'Content-Type' => 'text/plain' }) do
    api_description_source(@source_path)
  end

  app = Rack::Builder.new do
    run Rack::Cascade.new([source_app, change_app, generate_app])
  end

  Rack::Server.start(Port: @options.port, Host: @options.host, app: app)
end

#showObject



92
93
94
95
96
97
98
99
100
# File 'lib/apiary/command/preview.rb', line 92

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

#timestampObject



68
69
70
# File 'lib/apiary/command/preview.rb', line 68

def timestamp
  Time.now.getutc.to_i.to_s
end

#watchObject



60
61
62
63
64
65
66
# File 'lib/apiary/command/preview.rb', line 60

def watch
  return unless @options.watch
  listener = Listen.to(File.dirname(@source_path), only: /#{File.basename(@source_path)}/) do |modified|
    @changed = timestamp
  end
  listener.start
end

#write_generated_path(path, outfile) ⇒ Object



120
121
122
# File 'lib/apiary/command/preview.rb', line 120

def write_generated_path(path, outfile)
  File.write(outfile, File.read(path))
end