Class: Apiary::Command::Preview

Inherits:
Object
  • Object
show all
Defined in:
lib/apiary/command/preview.rb

Overview

Display preview of local blueprint file

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Preview) initialize(args)

A new instance of Preview



18
19
20
21
22
23
24
25
# File 'lib/apiary/command/preview.rb', line 18

def initialize(args)
  @options = {}
  @options[:apib_path]   = "apiary.apib"
  @options[:api_host]    = "api.apiary.io"
  @options[:headers]     = {:accept => "text/html", :content_type => "text/plain"}
  @options[:port]        = 8080
  @options.merge! args
end

Instance Attribute Details

- (Object) options (readonly)

Returns the value of attribute options



16
17
18
# File 'lib/apiary/command/preview.rb', line 16

def options
  @options
end

Class Method Details

+ (Object) execute(args)



27
28
29
# File 'lib/apiary/command/preview.rb', line 27

def self.execute(args)
  args[:server] ? new(args).server : new(args).show
end

Instance Method Details

- (Object) api_host



53
54
55
# File 'lib/apiary/command/preview.rb', line 53

def api_host
  @options[:api_host]
end

- (Object) apib_path



45
46
47
# File 'lib/apiary/command/preview.rb', line 45

def apib_path
  @options[:apib_path] || "#{File.basename(Dir.pwd)}.apib"
end

- (Object) browser



49
50
51
# File 'lib/apiary/command/preview.rb', line 49

def browser
  BROWSERS[@options[:browser]]  || nil
end

- (Object) generate_static(apib_path)



91
92
93
94
95
96
# File 'lib/apiary/command/preview.rb', line 91

def generate_static(apib_path)
  File.open(preview_path(apib_path), "w") do |file|
    file.write(query_apiary(api_host, apib_path))
    open_generated_page(file.path)
  end
end

- (Object) open_generated_page(path)

TODO: add linux and windows systems



87
88
89
# File 'lib/apiary/command/preview.rb', line 87

def open_generated_page(path)
  exec "open #{browser_options} #{path}"
end

- (Object) port



57
58
59
# File 'lib/apiary/command/preview.rb', line 57

def port
  @options[:port]
end

- (Object) preview_path(apib_path)



75
76
77
78
# File 'lib/apiary/command/preview.rb', line 75

def preview_path(apib_path)
  basename = File.basename(apib_path)
  "/tmp/#{basename}-preview.html"
end

- (Object) query_apiary(host, apib_path)



80
81
82
83
84
# File 'lib/apiary/command/preview.rb', line 80

def query_apiary(host, apib_path)
  url  = "https://#{host}/blueprint/generate"
  data = File.read(apib_path)
  RestClient.post(url, data, @options[:headers])
end

- (Object) rack_app(&block)



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

def rack_app(&block)
  Rack::Builder.new do
    run lambda { |env| [200, Hash.new, [block.call]] }
  end
end

- (Object) run_server



67
68
69
70
71
72
73
# File 'lib/apiary/command/preview.rb', line 67

def run_server
  app = self.rack_app do
    self.query_apiary(api_host, apib_path)
  end

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

- (Object) server



31
32
33
# File 'lib/apiary/command/preview.rb', line 31

def server
  run_server
end

- (Object) show



35
36
37
# File 'lib/apiary/command/preview.rb', line 35

def show
  generate_static(apib_path)
end

- (Object) validate_apib_file(apib_file)



39
40
41
42
43
# File 'lib/apiary/command/preview.rb', line 39

def validate_apib_file(apib_file)
  unless File.exist?(apib_file)
    abort "Apiary definition file hasn't been found: #{apib_file.inspect}"
  end
end