Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add initial, heroku-based skeleton

  • Loading branch information...
commit aa0e495043d6e84245a1ececbf57d562b97631f7 1 parent 7da1df1
@Almad Almad authored
View
4 .gitignore
@@ -16,3 +16,7 @@ tmp
.yardoc
_yardoc
doc/
+
+
+.DS_STORE
+
View
11 Gemfile
@@ -0,0 +1,11 @@
+source "http://rubygems.org"
+
+gemspec
+
+gem "launchy", ">= 0.3"
+gem "rest-client", ">= 1.6"
+
+group :development do
+ gem "rake", ">= 0.8.7"
+end
+
View
26 Gemfile.lock
@@ -0,0 +1,26 @@
+PATH
+ remote: .
+ specs:
+ apiary (0.0.1)
+ launchy (>= 0.3.2)
+ rest-client (~> 1.6.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.2.8)
+ launchy (2.1.0)
+ addressable (~> 2.2.6)
+ mime-types (1.18)
+ rake (0.9.2.2)
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ apiary!
+ launchy (>= 0.3)
+ rake (>= 0.8.7)
+ rest-client (>= 1.6)
View
22 LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright © Apiary Ltd. 2012
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
8 README.md
@@ -4,3 +4,11 @@ Apiary client
This will be CLI for Apiary API.
Please wait a while until it's useful.
+
+
+Credits
+----------
+
+Developed by Apiary Ltd.
+
+Heavily inspired by Heroku CLI, (c) Heroku under MIT License.
View
20 apiary.gemspec
@@ -0,0 +1,20 @@
+$:.unshift File.expand_path("../lib", __FILE__)
+
+require "apiary/version"
+
+Gem::Specification.new do |gem|
+ gem.name = "apiary"
+ gem.version = Apiary::VERSION
+
+ gem.author = "Apiary Ltd."
+ gem.email = "team@apiary.io"
+ gem.homepage = "http://apiary.io/"
+ gem.summary = "Apiary.io API client"
+ gem.description = "Apiary.io API client"
+ gem.executables = "apiary"
+ gem.license = "MIT"
+
+ gem.files = %x{ git ls-files }.split("\n").select { |d| d =~ %r{^(License|README|bin/|data/|ext/|lib/|spec/|test/)} }
+ gem.add_dependency "rest-client", "~> 1.6.1"
+ gem.add_dependency "launchy", ">= 0.3.2"
+end
View
13 apiary.rb
@@ -1,13 +0,0 @@
-require 'rubygems'
-
-require 'launchy'
-require 'rest_client'
-
-headers = {:accept => "text/html",:content_type => "text/plain"}
-response = RestClient.post "http://api.apiary.io/blueprint/generate", IO.read('apiary.apib'), headers
-
-aFile = File.new("/tmp/apiarypreview.html", "w")
-aFile.write(response)
-aFile.close
-
-Launchy.open("file:///tmp/apiarypreview.html")
View
14 bin/apiary
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+# encoding: UTF-8
+
+begin
+ require "pathname"
+ bin_file = Pathname.new(__FILE__).realpath
+
+ $:.unshift File.expand_path("../../lib", bin_file)
+
+ require "apiary/cli"
+ Apiary::CLI.start(*ARGV)
+rescue Interrupt
+ puts("\n Cancelled.")
+end
View
2  lib/apiary.rb
@@ -0,0 +1,2 @@
+module Apiary; end
+
View
12 lib/apiary/cli.rb
@@ -0,0 +1,12 @@
+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)
+ end
+
+end
View
63 lib/apiary/cmd.rb
@@ -0,0 +1,63 @@
+# Base skeleton taken from Heroku base command,
+# https://github.com/heroku/heroku/blob/master/lib/heroku/command.rb
+# (c) Heroku and contributors
+
+module Apiary
+ module Command
+ class CommandFailed < RuntimeError; end
+
+ def self.commands
+ @@commands ||= {}
+ end
+
+ def self.command_aliases
+ @@command_aliases ||= {}
+ end
+
+ def self.load
+ Dir[File.join(File.dirname(__FILE__), "commands", "*.rb")].each do |file|
+ require file
+ end
+ end
+
+ def self.parse(cmd)
+ commands[cmd] || commands[command_aliases[cmd]]
+ end
+
+ def self.prepare_run(cmd, args=[])
+ command = parse(cmd)
+
+ unless command
+ if %w( -v --version ).include?(cmd)
+ command = parse('version')
+ else
+ error([
+ "`#{cmd}` is not an apiary command.",
+ ].compact.join("\n"))
+ end
+ end
+
+ # add args, opts
+ [ command[:klass].new(), command[:method] ]
+ end
+
+ def self.register_command(command)
+ commands[command[:command]] = command
+ end
+
+
+ def self.run(cmd, arguments=[])
+ object, method = prepare_run(cmd, arguments.dup)
+ object.send(method)
+ rescue CommandFailed => e
+ error e.message
+ rescue OptionParser::ParseError
+ commands[cmd] ? run("help", [cmd]) : run("help")
+ end
+
+ def self.error(message)
+ $stderr.puts(message)
+ exit(1)
+ end
+ end
+end
View
41 lib/apiary/commands/base.rb
@@ -0,0 +1,41 @@
+class Apiary::Command::Base
+
+ attr_reader :args
+ attr_reader :options
+
+ def initialize(args=[], options={})
+ @args = args
+ @options = options
+ end
+
+ def self.namespace
+ self.to_s.split("::").last.downcase
+ end
+
+ 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)
+
+ # 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)
+ )
+ end
+
+end
View
27 lib/apiary/commands/preview.rb
@@ -0,0 +1,27 @@
+require "apiary/commands/base"
+
+# Display preview of local blueprint file
+#
+class Apiary::Command::Preview < Apiary::Command::Base
+
+ # preview
+ #
+ # Launch web browser and display preview of local blueprint file
+ #
+ def index
+
+ api_server = ENV['APIARY_API_HOST'] || "api.apiary.io"
+ 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
+
+ aFile = File.new("/tmp/apiarypreview.html", "w")
+ aFile.write(response)
+ aFile.close
+
+ Launchy.open("file:///tmp/apiarypreview.html")
+ end
+
+end
View
3  lib/apiary/version.rb
@@ -0,0 +1,3 @@
+module Apiary
+ VERSION = "0.0.1"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.