Skip to content

Commit

Permalink
Using this syntax is more fault-tolerant, easier to reuse without all…
Browse files Browse the repository at this point in the history
… the dependencies. Not that big deal in this case but still.
  • Loading branch information
botanicus committed Jul 6, 2012
1 parent 4841827 commit 5c05e76
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 55 deletions.
1 change: 0 additions & 1 deletion lib/apiary.rb
@@ -1 +0,0 @@
module Apiary; end
13 changes: 7 additions & 6 deletions lib/apiary/cli.rb
@@ -1,10 +1,11 @@
require "apiary"
require "apiary/cmd"

class Apiary::CLI
def self.start(*args)
command = args.shift.strip rescue "help"
Apiary::Command.load
Apiary::Command.run(command, args)
module Apiary
class CLI
def self.start(*args)
command = args.shift.strip rescue "help"
Apiary::Command.load
Apiary::Command.run(command, args)
end
end
end
5 changes: 3 additions & 2 deletions lib/apiary/cmd.rb
Expand Up @@ -5,6 +5,7 @@
module Apiary
module Command
class CommandFailed < RuntimeError; end

def self.commands
@@commands ||= {}
end
Expand Down Expand Up @@ -37,15 +38,15 @@ def self.prepare_run(cmd, args=[])
end

# add args, opts
[ command[:klass].new(), command[:method] ]
[command[:klass].new(), command[:method]]
end

def self.register_command(command)
commands[command[:command]] = command
end


def self.run(cmd, arguments=[])
def self.run(cmd, arguments = [])
object, method = prepare_run(cmd, arguments.dup)
object.send(method)
rescue CommandFailed => e
Expand Down
66 changes: 35 additions & 31 deletions lib/apiary/commands/base.rb
@@ -1,39 +1,43 @@
class Apiary::Command::Base
attr_reader :args
attr_reader :options
module Apiary
module Command
class Base
attr_reader :args
attr_reader :options

def initialize(args = [], options = {})
@args = args
@options = options
end
def initialize(args = [], options = {})
@args = args
@options = options
end

def self.namespace
self.to_s.split("::").last.downcase
end
def self.namespace
self.to_s.split("::").last.downcase
end

attr_reader :args
attr_reader :options
attr_reader :args
attr_reader :options

def self.method_added(method)
return if self == Apiary::Command::Base
return if private_method_defined?(method)
return if protected_method_defined?(method)
def self.method_added(method)
return if self == Apiary::Command::Base
return if private_method_defined?(method)
return if protected_method_defined?(method)

# help = extract_help_from_caller(caller.first)
resolved_method = (method.to_s == "index") ? nil : method.to_s
command = [self.namespace, resolved_method].compact.join(":")
# banner = extract_banner(help) || command
# help = extract_help_from_caller(caller.first)
resolved_method = (method.to_s == "index") ? nil : method.to_s
command = [self.namespace, resolved_method].compact.join(":")
# banner = extract_banner(help) || command

Apiary::Command.register_command(
:klass => self,
:method => method,
:namespace => self.namespace,
:command => command
# :banner => banner.strip,
# :help => help.join("\n"),
# :summary => extract_summary(help),
# :description => extract_description(help),
# :options => extract_options(help)
)
Apiary::Command.register_command(
:klass => self,
:method => method,
:namespace => self.namespace,
:command => command
# :banner => banner.strip,
# :help => help.join("\n"),
# :summary => extract_summary(help),
# :description => extract_description(help),
# :options => extract_options(help)
)
end
end
end
end
33 changes: 18 additions & 15 deletions lib/apiary/commands/preview.rb
@@ -1,24 +1,27 @@
require "apiary/commands/base"

# Display preview of local blueprint file
class Apiary::Command::Preview < Apiary::Command::Base
module Apiary
module Command
class Preview < Apiary::Command::Base
# Preview.
#
# Launch web browser and display preview of local blueprint file
def index
api_server = ENV.fetch("APIARY_API_HOST") { "api.apiary.io" }

# preview
#
# Launch web browser and display preview of local blueprint file
def index
api_server = ENV.fetch("APIARY_API_HOST") { "api.apiary.io" }
require "launchy"
require "rest_client"

require "launchy"
require "rest_client"
headers = {:accept => "text/html", :content_type => "text/plain"}
response = RestClient.post "https://#{api_server}/blueprint/generate", IO.read("apiary.apib"), headers

headers = {:accept => "text/html", :content_type => "text/plain"}
response = RestClient.post "https://#{api_server}/blueprint/generate", IO.read("apiary.apib"), headers
file = File.new("/tmp/apiarypreview.html", "w")
file.write(response)
file.close

file = File.new("/tmp/apiarypreview.html", "w")
file.write(response)
file.close

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

0 comments on commit 5c05e76

Please sign in to comment.