From 5c05e768566e3ff9683ade4d7af8aff04fb9545b Mon Sep 17 00:00:00 2001 From: botanicus Date: Fri, 6 Jul 2012 11:20:57 +0100 Subject: [PATCH] Using this syntax is more fault-tolerant, easier to reuse without all the dependencies. Not that big deal in this case but still. --- lib/apiary.rb | 1 - lib/apiary/cli.rb | 13 +++---- lib/apiary/cmd.rb | 5 +-- lib/apiary/commands/base.rb | 66 ++++++++++++++++++---------------- lib/apiary/commands/preview.rb | 33 +++++++++-------- 5 files changed, 63 insertions(+), 55 deletions(-) diff --git a/lib/apiary.rb b/lib/apiary.rb index ee1b2bc..e69de29 100644 --- a/lib/apiary.rb +++ b/lib/apiary.rb @@ -1 +0,0 @@ -module Apiary; end diff --git a/lib/apiary/cli.rb b/lib/apiary/cli.rb index 70ff335..e103c25 100644 --- a/lib/apiary/cli.rb +++ b/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 diff --git a/lib/apiary/cmd.rb b/lib/apiary/cmd.rb index 17bfa5a..dc0012e 100644 --- a/lib/apiary/cmd.rb +++ b/lib/apiary/cmd.rb @@ -5,6 +5,7 @@ module Apiary module Command class CommandFailed < RuntimeError; end + def self.commands @@commands ||= {} end @@ -37,7 +38,7 @@ 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) @@ -45,7 +46,7 @@ def self.register_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 diff --git a/lib/apiary/commands/base.rb b/lib/apiary/commands/base.rb index e2c1b59..0a5efab 100644 --- a/lib/apiary/commands/base.rb +++ b/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 diff --git a/lib/apiary/commands/preview.rb b/lib/apiary/commands/preview.rb index 0f11815..3a6fd6f 100644 --- a/lib/apiary/commands/preview.rb +++ b/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