Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Preview works.
  • Loading branch information
botanicus committed Jul 12, 2012
1 parent 2fd37ca commit d2a9343
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
1 change: 0 additions & 1 deletion apiary.gemspec
Expand Up @@ -23,7 +23,6 @@ Gem::Specification.new do |gem|

gem.add_dependency "nake"
gem.add_dependency "rest-client", "~> 1.6.1"
gem.add_dependency "launchy", ">= 0.3.2"

gem.post_install_message = "This gem is a client for http://apiary.io. Apiary is in closed beta version now, you need an invitation. If you don't have one, visit http://apiary.us2.list-manage.com/subscribe?u=b89934a238dcec9533f4a834a&id=08f2bdde55 to get on the waiting list!"
end
56 changes: 49 additions & 7 deletions lib/apiary/commands/preview.rb
@@ -1,22 +1,64 @@
# encoding: utf-8

# Usage:
# apiary preview
# apiary preview my_apib_file.apib
# apiary preview [my_apib_file.apib] --api_host=api.apiary.io
# apiary preview [my_apib_file.apib] --browser=Safari

# Display preview of local blueprint file
Task.new(:preview) do |task|
task.description = "Display preview of local blueprint file"

# Configuration.
task.config[:apib_file] = "#{File.basename(Dir.pwd)}.apib"
task.config[:api_host] = "api.apiary.io"
task.config[:headers] = {accept: "text/html", content_type: "text/plain"}
task.config[:browser] = "Google Chrome"

task.boot do
require "launchy"
require "rest_client"
end

task.define do |api_server = "api.apiary.io"|
headers = {:accept => "text/html", :content_type => "text/plain"}
response = RestClient.post "https://#{api_server}/blueprint/generate", IO.read("apiary.apib"), headers
# Singleton methods.
def task.validate_apib_file(apib_file)
unless File.exist?(apib_file)
abort "Apiary definition file hasn't been found: #{apib_file.inspect}"
end
end

def browser(options)
options[:browser] || self.config[:browser]
end

def api_host(options)
options[:api_host] || self.config[:api_host]
end

File.new("/tmp/apiarypreview.html", "w") do |file|
def resolve_apib_file(apib_file)
apib_file = apib_file || self.config[:apib_file]
end

# The actual definition.
task.define do |apib_file, options|
apib_file = self.resolve_apib_file(apib_file)

self.validate_apib_file(apib_file)

url = "https://#{self.api_host(options)}/blueprint/generate"
apib = IO.read(apib_file)
response = RestClient.post(url, apib, self.config[:headers])

basename = File.basename(apib_file)
File.open("/tmp/#{basename}-preview.html", "w") do |file|
file.write(response)
# TODO: Support non OS X systems again. Could be done
# through launchy or similar projects, but launchy seemed
# to be tad buggy to me (ignored application I wanted to use),
# so for now it's done through the open command.
sh "open -a '#{self.browser(options)}' '#{file.path}'"
end

Launchy.open("file:///tmp/apiarypreview.html")
end
end

# Task.tasks.default_proc = lambda { |*| Task[:preview] }

0 comments on commit d2a9343

Please sign in to comment.