Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #47 from apiaryio/abtris/refactor-thor
Browse files Browse the repository at this point in the history
Refactor thor
  • Loading branch information
honzajavorek committed Sep 29, 2014
2 parents 484a3ec + c912791 commit 36b066d
Show file tree
Hide file tree
Showing 23 changed files with 211 additions and 156 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ Gemfile.lock

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# testing
features/fixtures/test.html
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
require "rubygems"
require "rspec/core/rake_task"
require 'yard'
require 'cucumber'
require 'cucumber/rake/task'

Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = "features --format pretty"
end

desc "Run all specs"
RSpec::Core::RakeTask.new(:spec) do |t|
Expand All @@ -11,6 +17,8 @@ end
desc 'Default: Run all specs.'
task :default => :spec

task :test => :spec

task :doc => :yard
task :gem => :gemspec

Expand Down
3 changes: 3 additions & 0 deletions apiary.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ Gem::Specification.new do |gem|
gem.add_dependency "rest-client", "~> 1.6.7"
gem.add_dependency "rack", ">= 1.4.0", "< 1.6.0"
gem.add_dependency "rake"
gem.add_dependency "thor"

gem.add_development_dependency "rspec", "~> 3.1.0"
gem.add_development_dependency "webmock"
gem.add_development_dependency "yard"
gem.add_development_dependency "aruba"
gem.add_development_dependency "cucumber"

end
4 changes: 4 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
override:
- bundle exec rake test
- bundle exec rake features
7 changes: 7 additions & 0 deletions features/fetch.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Fetch apiary.apib from API_NAME.apiary.io

# This is integration testing you have to set APIARY_API_KEY
Scenario: Fetch apiary.apib from API_NAME.apiary.io

When I run `apiary fetch --api-name apiaryclienttest`
Then the output should contain the content of file "apiary.apib"
48 changes: 48 additions & 0 deletions features/fixtures/apiary.apib
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
FORMAT: 1A

# Apiary Client Test - DON'T CHANGE DOCUMENT!

This document is used for testing apiary-client

# Group Notes
Notes related resources of the **Notes API**

## Notes Collection [/notes
### List all Notes [GET]
+ Response 200 (application/json)

[{
"id": 1, "title": "Jogging in park"
}, {
"id": 2, "title": "Pick-up posters from post-office"
}]

### Create a Note [POST

+ Request

sflmvs;mv;dsm{ "title": "Buy cheese and bread for breakfast." }

+ Response 201 (application/json)

{ "id": 3, "title": "Buy cheese and bread for breakfast." }

## Note [/notes/{id}]
A single Note object with all its details

+ Parameters
+ id (required, number, `1`) ... Numeric `id` of the Note to perform action with. Has example value.

### Retrieve a Note [GET]
+ Response 200 (application/json)

+ Header

X-My-Header: The Value

+ Body

{ "id": 2, "title": "Pick-up posters from post-office" }

### Remove a Note [DELETE]
+ Response 204
7 changes: 7 additions & 0 deletions features/preview.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Show API documentation in specified browser

# This is integration testing you have to set APIARY_API_KEY
Scenario: Write generated HTML into specified file

When I run `apiary preview --path apiary.apib --output=test.html`
Then a file named "test.html" should exist
7 changes: 7 additions & 0 deletions features/publish.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Publish apiary.apib on docs.API_NAME.apiary.io

# This is integration testing you have to set APIARY_API_KEY
Scenario: Publish apiary.apib on docs.API_NAME.apiary.io

When I run `apiary publish --path=apiary.apib --api-name 1111apiaryclienttest`
Then the exit status should be 1
19 changes: 19 additions & 0 deletions features/step_definitions/file_content_step.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Then /^the output should contain the content of file "(.*)"$/ do |filename|
expected = nil
in_current_dir do
expected = File.read(filename)
end

assert_partial_output(expected, all_output)
end


Then /^output file "(.*)" should contain the content of file "(.*)"$/ do |input, output|
expected = nil
in_current_dir do
actual = File.read(input)
expected = File.read(output)
end

assert_partial_output(expected, actual)
end
7 changes: 7 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'aruba/cucumber'
require 'fileutils'

Before do
@dirs << "../../features/fixtures"
ENV['PATH'] = "./bin#{File::PATH_SEPARATOR}#{ENV['PATH']}"
end
1 change: 1 addition & 0 deletions features/support/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'aruba/cucumber'
10 changes: 10 additions & 0 deletions features/version.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: Version of Apiary client

Scenario: Print the semantic version of Apiary client

# Note the output should be a semantic version (semver.org)
# The matching regex was taken from https://github.com/isaacs/node-semver/issues/32#issue-15023919

When I run `apiary version`
Then the output should match /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/
And the exit status should be 0
111 changes: 37 additions & 74 deletions lib/apiary/cli.rb
Original file line number Diff line number Diff line change
@@ -1,88 +1,51 @@
# encoding: utf-8
require 'optparse'
require "thor"
require "apiary/command/fetch"
require "apiary/command/preview"
require "apiary/command/publish"

module Apiary
class CLI
class CLI < Thor

attr_reader :command
desc "fetch", "Fetch apiary.apib from API_NAME.apiary.io"
method_option :api_name, :type => :string, :required => true, :default => ''

def initialize(args)
options = parse_options!(args)
@command = options.delete(:command)
run(options)
def fetch
cmd = Apiary::Command::Fetch.new options
cmd.execute
end

def run(options)
Apiary::Command::Runner.run(@command, options)
desc "preview", "Show API documentation in default browser"
method_option :browser, :type => :string, :enum => %w(chrome safari firefox), :banner => "chrome|safari|firefox", :desc => "Show API documentation in specified browser"
method_option :output, :type => :string, :banner => "FILE", :desc => "Write generated HTML into specified file"
method_option :path, :type => :string, :desc => "Specify path to blueprint file", :default => 'apiary.apib'
method_option :api_host, :type => :string, :banner => "HOST", :desc => "Specify apiary host"
method_option :server, :type => :boolean, :desc => "Start standalone web server on port 8080"
method_option :port, :type => :numeric, :banner => "PORT", :desc => "Set port for --server option"

def preview
cmd = Apiary::Command::Preview.new options
cmd.execute
end

def parse_options!(args)
options = {}
command = nil
if args.first && !args.first.start_with?("-")
command = args.first
end

options_parser = OptionParser.new do |opts|
opts.on("--path [PATH]") do |path|
raise OptionParser::InvalidOption unless ["fetch", "preview", "publish"].include? command
options[:path] = path
end

opts.on("--output [PATH]") do |path|
raise OptionParser::InvalidOption unless ["fetch", "preview"].include? command
options[:output] = path
end

opts.on("--api_host API_HOST") do |api_host|
raise OptionParser::InvalidOption unless ["fetch", "preview", "publish"].include? command
options[:api_host] = api_host
end

opts.on("--api-name API_HOST") do |api_name|
raise OptionParser::InvalidOption unless ["fetch", "publish"].include? command
options[:api_name] = api_name
end

opts.on("--message COMMIT_MESSAGE") do |commit_message|
raise OptionParser::InvalidOption if command != "publish"
options[:commit_message] = commit_message
end

opts.on("--browser BROWSER") do |browser|
raise OptionParser::InvalidOption if command != "preview"
options[:browser] = browser
end
desc "publish", "Publish apiary.apib on docs.API_NAME.apiary.io"
method_option :message, :type => :string, :banner => "COMMIT_MESSAGE", :desc => "Publish with custom commit message"
method_option :path, :type => :string, :desc => "Specify path to blueprint file", :default => 'apiary.apib'
method_option :api_host, :type => :string, :banner => "HOST", :desc => "Specify apiary host"
method_option :api_name, :type => :string, :required => true, :default => ''

opts.on("--server") do
raise OptionParser::InvalidOption if command != "preview"
options[:server] = true
end

opts.on("--port [PORT]") do |port|
raise OptionParser::InvalidOption unless ["fetch", "preview", "publish"].include? command
options[:port] = port
end

opts.on('-v', '--version') do
raise OptionParser::InvalidOption if command
command = :version
end

opts.on( '-h', '--help') do
raise OptionParser::InvalidOption if command
command = :help
end
end
def publish
cmd = Apiary::Command::Publish.new options
cmd.execute
end

options_parser.parse!
options[:command] = command || :help
options
desc "version", "Show version"
method_option :aliases => "-v"

rescue OptionParser::InvalidOption => e
puts e
puts Apiary::Command::Help.banner
exit 1
def version
puts Apiary::VERSION
end

end
end

Apiary::CLI.start(ARGV)
4 changes: 2 additions & 2 deletions lib/apiary/command/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def initialize(opts)
}
end

def self.execute(args)
response = new(args).fetch_from_apiary
def execute()
response = fetch_from_apiary
if response.instance_of? String
puts response
end
Expand Down
38 changes: 0 additions & 38 deletions lib/apiary/command/help.rb

This file was deleted.

19 changes: 15 additions & 4 deletions lib/apiary/command/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,30 @@ class Preview

# TODO: use OpenStruct to store @options
def initialize(opts)
puts opts
@options = OpenStruct.new(opts)
@options.path ||= "apiary.apib"
@options.api_host ||= "api.apiary.io"
@options.headers ||= {:accept => "text/html", :content_type => "text/plain"}
@options.port ||= 8080
@options.proxy ||= ENV['http_proxy']
@options.server ||= false
end

def self.execute(args)
args[:server] ? new(args).server : new(args).show
def execute
if @options.server
server
else
show
end
end

def server
run_server
end

def show
generate_static(path)
generate_static(@options.path)
end

def validate_apib_file(apib_file)
Expand Down Expand Up @@ -74,7 +80,12 @@ def preview_path(path)

def query_apiary(host, path)
url = "https://#{host}/blueprint/generate"
data = File.read(path)
begin
data = File.read(path)
rescue
abort "File #{path} not found."
end

RestClient.proxy = @options.proxy

begin
Expand Down
Loading

0 comments on commit 36b066d

Please sign in to comment.