Permalink
Browse files

Using this syntax is more fault-tolerant, easier to reuse without all…

… the dependencies. Not that big deal in this case but still.
  • Loading branch information...
botanicus committed Jul 6, 2012
1 parent 4841827 commit 5c05e768566e3ff9683ade4d7af8aff04fb9545b
Showing with 63 additions and 55 deletions.
  1. +0 −1 lib/apiary.rb
  2. +7 −6 lib/apiary/cli.rb
  3. +3 −2 lib/apiary/cmd.rb
  4. +35 −31 lib/apiary/commands/base.rb
  5. +18 −15 lib/apiary/commands/preview.rb
View
@@ -1 +0,0 @@
-module Apiary; end
View
@@ -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
View
@@ -5,6 +5,7 @@
module Apiary
module Command
class CommandFailed < RuntimeError; end
+
def self.commands
@@commands ||= {}
end
@@ -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
@@ -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
@@ -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.