apiaryio 0.3.5 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +3 -2
  2. data/.travis.yml +3 -11
  3. data/README.md +7 -10
  4. data/Rakefile +4 -29
  5. data/apiary.gemspec +15 -9
  6. data/appveyor.yml +20 -11
  7. data/bin/apiary +1 -2
  8. data/circle.yml +2 -2
  9. data/features/preview.feature +1 -1
  10. data/features/step_definitions/file_content_step.rb +3 -12
  11. data/features/support/env.rb +2 -4
  12. data/lib/apiary.rb +0 -2
  13. data/lib/apiary/agent.rb +13 -0
  14. data/lib/apiary/cli.rb +5 -7
  15. data/lib/apiary/command/fetch.rb +48 -60
  16. data/lib/apiary/command/preview.rb +27 -46
  17. data/lib/apiary/command/publish.rb +27 -38
  18. data/lib/apiary/file_templates/preview.erb +1 -1
  19. data/lib/apiary/helpers.rb +45 -0
  20. data/lib/apiary/version.rb +1 -1
  21. data/spec/{cli_spec.rb → apiary/cli_spec.rb} +0 -1
  22. data/spec/{command → apiary/command}/fetch_spec.rb +0 -0
  23. data/spec/{command → apiary/command}/preview_spec.rb +1 -1
  24. data/spec/{command → apiary/command}/publish_spec.rb +7 -4
  25. data/spec/apiary/helpers_spec.rb +67 -0
  26. data/spec/fixtures/api_blueprint_and_swagger/apiary.apib +5 -0
  27. data/spec/fixtures/api_blueprint_and_swagger/swagger.yaml +5 -0
  28. data/{features → spec}/fixtures/apiary-invalid.apib +0 -0
  29. data/{features → spec}/fixtures/apiary.apib +0 -0
  30. data/{features → spec}/fixtures/apiary_with_bom.apib +0 -0
  31. data/spec/fixtures/empty_folder/.gitkeep +0 -0
  32. data/spec/fixtures/only_api_blueprint/apiary.apib +5 -0
  33. data/spec/fixtures/only_swagger/swagger.yaml +5 -0
  34. data/spec/spec.opts +1 -0
  35. data/spec/spec_helper.rb +8 -4
  36. data/spec/support/aruba.rb +1 -11
  37. data/spec/support/webmock.rb +1 -0
  38. metadata +70 -53
  39. data/features/support/setup.rb +0 -5
  40. data/lib/apiary/common.rb +0 -29
  41. data/spec/common_spec.rb +0 -29
@@ -6,14 +6,14 @@ require 'json'
6
6
  require 'tmpdir'
7
7
  require 'erb'
8
8
 
9
- require "apiary/common"
10
- require "apiary/helpers/javascript_helper"
9
+ require 'apiary/agent'
10
+ require 'apiary/helpers'
11
+ require 'apiary/helpers/javascript_helper'
11
12
 
12
- module Apiary
13
- module Command
13
+ module Apiary::Command
14
14
  # Display preview of local blueprint file
15
15
  class Preview
16
-
16
+ include Apiary::Helpers
17
17
  include Apiary::Helpers::JavascriptHelper
18
18
 
19
19
  PREVIEW_TEMPLATE_PATH = "#{File.expand_path File.dirname(__FILE__)}/../file_templates/preview.erb"
@@ -28,7 +28,7 @@ module Apiary
28
28
 
29
29
  def initialize(opts)
30
30
  @options = OpenStruct.new(opts)
31
- @options.path ||= 'apiary.apib'
31
+ @options.path ||= '.'
32
32
  @options.api_host ||= 'api.apiary.io'
33
33
  @options.port ||= 8080
34
34
  @options.proxy ||= ENV['http_proxy']
@@ -37,10 +37,14 @@ module Apiary
37
37
  @options.headers ||= {
38
38
  :accept => 'text/html',
39
39
  :content_type => 'text/plain',
40
- :user_agent => "Apiary Client Gem (https://help.apiary.io/tools/apiary-cli/)"
40
+ :user_agent => Apiary.user_agent
41
41
  }
42
42
 
43
- validate_apib_file
43
+ begin
44
+ @source_path = api_description_source_path(@options.path)
45
+ rescue Exception => e
46
+ abort "#{e.message}"
47
+ end
44
48
  end
45
49
 
46
50
  def execute
@@ -52,24 +56,25 @@ module Apiary
52
56
  end
53
57
 
54
58
  def server
55
- run_server
56
- end
59
+ app = self.rack_app do
60
+ generate
61
+ end
57
62
 
58
- def show
59
- generate_static
63
+ Rack::Server.start(:Port => @options.port, :Host => @options.host, :app => app)
60
64
  end
61
65
 
62
- def validate_apib_file
63
- common = Apiary::Common.new
64
- common.validate_apib_file(@options.path)
65
- end
66
+ def show
67
+ preview_string = generate
66
68
 
67
- def path
68
- @options.path || "#{File.basename(Dir.pwd)}.apib"
69
+ File.open(preview_path, 'w') do |file|
70
+ file.write preview_string
71
+ file.flush
72
+ @options.output ? write_generated_path(file.path, @options.output) : open_generated_page(file.path)
73
+ end
69
74
  end
70
75
 
71
76
  def browser
72
- BROWSERS[@options.browser] || nil
77
+ BROWSERS[@options.browser] || nil
73
78
  end
74
79
 
75
80
  def rack_app(&block)
@@ -78,14 +83,6 @@ module Apiary
78
83
  end
79
84
  end
80
85
 
81
- def run_server
82
- app = self.rack_app do
83
- generate
84
- end
85
-
86
- Rack::Server.start(:Port => @options.port, :Host => @options.host, :app => app)
87
- end
88
-
89
86
  # TODO: add linux and windows systems
90
87
  def open_generated_page(path)
91
88
  exec "open #{browser_options} #{path}"
@@ -99,30 +96,15 @@ module Apiary
99
96
  template = load_preview_template
100
97
 
101
98
  data = {
102
- title: File.basename(@options.path, '.*'),
103
- blueprint: load_blueprint
99
+ title: File.basename(@source_path, '.*'),
100
+ source: api_description_source(@source_path)
104
101
  }
105
102
 
106
103
  template.result(binding)
107
104
  end
108
105
 
109
- def generate_static
110
- preview_string = generate
111
-
112
- File.open(preview_path, 'w') do |file|
113
- file.write preview_string
114
- file.flush
115
- @options.output ? write_generated_path(file.path, @options.output) : open_generated_page(file.path)
116
- end
117
- end
118
-
119
- def load_blueprint
120
- file = File.open @options.path, 'r'
121
- file.read
122
- end
123
-
124
106
  def preview_path
125
- basename = File.basename(@options.path, '.*')
107
+ basename = File.basename(@source_path, '.*')
126
108
  temp = Dir.tmpdir
127
109
  "#{temp}/#{basename}-preview.html"
128
110
  end
@@ -139,5 +121,4 @@ module Apiary
139
121
  "-a #{BROWSERS[@options.browser.to_sym]}" if @options.browser
140
122
  end
141
123
  end
142
- end
143
124
  end
@@ -3,69 +3,65 @@ require 'rest-client'
3
3
  require 'rack'
4
4
  require 'ostruct'
5
5
  require 'json'
6
- require "apiary/common"
7
6
 
8
- module Apiary
9
- module Command
7
+ require 'apiary/agent'
8
+ require 'apiary/helpers'
9
+
10
+ module Apiary::Command
10
11
  # Display preview of local blueprint file
11
12
  class Publish
13
+ include Apiary::Helpers
12
14
 
13
15
  attr_reader :options
14
16
 
15
17
  def initialize(opts)
16
18
  @options = OpenStruct.new(opts)
17
- @options.path ||= "apiary.apib"
18
- @options.api_host ||= "api.apiary.io"
19
+ @options.path ||= '.'
20
+ @options.api_host ||= 'api.apiary.io'
19
21
  @options.api_name ||= false
20
22
  @options.api_key ||= ENV['APIARY_API_KEY']
21
23
  @options.proxy ||= ENV['http_proxy']
22
24
  @options.headers ||= {
23
- :accept => "text/html",
24
- :content_type => "text/plain",
25
+ :accept => 'text/html',
26
+ :content_type => 'text/plain',
25
27
  :authentication => "Token #{@options.api_key}",
26
- :user_agent => "Apiary Client Gem (https://help.apiary.io/tools/apiary-cli/)"
28
+ :user_agent => Apiary.user_agent
27
29
  }
28
- @options.message ||= "Saving blueprint from apiary-client"
30
+ @options.message ||= 'Saving API Description Document from apiary-client'
31
+
32
+ begin
33
+ @source_path = api_description_source_path(@options.path)
34
+ rescue Exception => e
35
+ abort "#{e.message}"
36
+ end
29
37
  end
30
38
 
31
- def execute()
39
+ def execute
32
40
  publish_on_apiary
33
41
  end
34
42
 
35
43
  def publish_on_apiary
36
44
  unless @options.api_name
37
- abort "Please provide an api-name option (subdomain part from your http://docs.<api-name>.apiary.io/)"
45
+ abort 'Please provide an api-name option (subdomain part from your http://docs.<api-name>.apiary.io/)'
38
46
  end
39
47
 
40
48
  unless @options.api_key
41
- abort "API key must be provided through environment variable APIARY_API_KEY. Please go to https://login.apiary.io/tokens to obtain it."
49
+ abort 'API key must be provided through environment variable APIARY_API_KEY. Please go to https://login.apiary.io/tokens to obtain it.'
42
50
  end
43
51
 
44
- self.query_apiary(@options.api_host, @options.path)
45
-
52
+ query_apiary
46
53
  end
47
54
 
48
- def validate_apib_file(apib_file)
49
- common = Apiary::Common.new
50
- common.validate_apib_file(apib_file)
51
- end
52
-
53
- def get_apib_file(apib_file)
54
- common = Apiary::Common.new
55
- common.get_apib_file(apib_file)
56
- end
55
+ def query_apiary
56
+ url = "https://#{@options.api_host}/blueprint/publish/#{@options.api_name}"
57
+ source = api_description_source(@source_path)
57
58
 
58
- def path
59
- @options.path || "#{File.basename(Dir.pwd)}.apib"
60
- end
61
-
62
- def query_apiary(host, path)
63
- url = "https://#{host}/blueprint/publish/#{@options.api_name}"
64
- if validate_apib_file path
59
+ unless source.nil?
65
60
  data = {
66
- :code => get_apib_file(path),
61
+ :code => source,
67
62
  :messageToSave => @options.message
68
63
  }
64
+
69
65
  RestClient.proxy = @options.proxy
70
66
 
71
67
  begin
@@ -87,12 +83,5 @@ module Apiary
87
83
  end
88
84
  end
89
85
  end
90
-
91
- private
92
- def api_name
93
- "-a"
94
- end
95
-
96
86
  end
97
- end
98
87
  end
@@ -8,7 +8,7 @@
8
8
  <script src="https://api.apiary.io/seeds/embed.js"></script>
9
9
  <script>
10
10
  var embed = new Apiary.Embed({
11
- apiBlueprint: "<%= escape_javascript data[:blueprint] %>"
11
+ apiBlueprint: "<%= escape_javascript data[:source] %>"
12
12
  });
13
13
  </script>
14
14
  </body>
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ module Apiary
4
+ module Helpers
5
+ def api_description_source_path(path)
6
+ raise "Invalid path #{path}" unless File.exist? path
7
+ return path if File.file? path
8
+ source_path = choose_one(path)
9
+ return source_path unless source_path.nil?
10
+ raise 'No API Description Document found'
11
+ end
12
+
13
+ def api_description_source(path)
14
+ source_path = api_description_source_path(path)
15
+ source = nil
16
+ File.open(source_path, 'r:bom|utf-8') { |file| source = file.read }
17
+ source
18
+ end
19
+
20
+ protected
21
+
22
+ def choose_one(path)
23
+ apibPath = api_blueprint(path)
24
+ swaggerPath = swagger(path)
25
+
26
+ if apibPath && swaggerPath
27
+ warn 'WARNING: Both apiary.apib and swagger.yaml are present. The apiary.apib file will be used. To override this selection specify path to desired file'
28
+ end
29
+
30
+ apibPath || swaggerPath
31
+ end
32
+
33
+ def api_blueprint(path)
34
+ source_path = File.join(path, 'apiary.apib')
35
+ return source_path if File.exist? source_path
36
+ return nil
37
+ end
38
+
39
+ def swagger(path)
40
+ source_path = File.join(path, 'swagger.yaml')
41
+ return source_path if File.exist? source_path
42
+ return nil
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module Apiary
2
- VERSION = "0.3.5"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -38,4 +38,3 @@ describe Apiary::CLI do
38
38
  end
39
39
  end
40
40
  end
41
-
@@ -4,7 +4,7 @@ describe Apiary::Command::Preview do
4
4
 
5
5
  let(:command) do
6
6
  opts = {
7
- path: "#{File.expand_path File.dirname(__FILE__)}/../../features/fixtures/apiary.apib"
7
+ path: "#{File.expand_path File.dirname(__FILE__)}/../../fixtures/apiary.apib"
8
8
  }
9
9
  Apiary::Command::Preview.new(opts)
10
10
  end
@@ -5,12 +5,13 @@ describe Apiary::Command::Publish do
5
5
  let(:message) do
6
6
  Apiary::Command::Publish.new({
7
7
  :api_name => 'myapi',
8
- :path => './features/fixtures/apiary.apib'
8
+ :path => 'spec/fixtures/apiary.apib',
9
+ :api_key => 'testkey'
9
10
  }).options.message
10
11
  end
11
12
 
12
13
  it 'uses the default message' do
13
- expect(message).to eq('Saving blueprint from apiary-client')
14
+ expect(message).to eq('Saving API Description Document from apiary-client')
14
15
  end
15
16
  end
16
17
 
@@ -19,7 +20,8 @@ describe Apiary::Command::Publish do
19
20
  Apiary::Command::Publish.new({
20
21
  :api_name => 'myapi',
21
22
  :message => 'Custom message',
22
- :path => './features/fixtures/apiary.apib'
23
+ :path => 'spec/fixtures/apiary.apib',
24
+ :api_key => 'testkey'
23
25
  }).options.message
24
26
  end
25
27
 
@@ -35,7 +37,8 @@ describe Apiary::Command::Publish do
35
37
  Apiary::Command::Publish.new({
36
38
  :api_name => 'myapi',
37
39
  :message => 'Custom message',
38
- :path => './features/fixtures/apiary.apib'
40
+ :path => 'spec/fixtures/apiary.apib',
41
+ :api_key => 'testkey'
39
42
  }).execute
40
43
  end
41
44
 
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apiary::Helpers do
4
+ include Apiary::Helpers
5
+
6
+ describe '#api_description_source_path' do
7
+ context 'path doesn\'t exists' do
8
+ it 'should raise error saying that Directory doesn\'t exists' do
9
+ path = 'spec/fixtures/invalid_path'
10
+ expect { api_description_source_path(path)}.to raise_error(/Invalid path/)
11
+ end
12
+ end
13
+
14
+ context 'missing file is in path' do
15
+ it 'should raise error saying that file doesn\'t exists' do
16
+ path = 'spec/fixtures/only_api_blueprint/swagger.yaml'
17
+ expect { api_description_source_path(path)}.to raise_error(/Invalid path/)
18
+ end
19
+ end
20
+
21
+ context 'directory is in path and contains API Blueprint only' do
22
+ it 'should return path ending to `apiary.apib`' do
23
+ path = 'spec/fixtures/only_api_blueprint'
24
+ expect(api_description_source_path(path)).to match(/apiary\.apib$/)
25
+ end
26
+ end
27
+
28
+ context 'directory is in path and contains Swagger only' do
29
+ it 'should return path ending to `swagger.yaml`' do
30
+ path = 'spec/fixtures/only_swagger'
31
+ expect(api_description_source_path(path)).to match(/swagger\.yaml$/)
32
+ end
33
+ end
34
+
35
+ context 'directory is in path and contains both API Blueprint and Swagger' do
36
+ it 'should prefere API Blueprint and return path ending to `apiary.apib`' do
37
+ path = 'spec/fixtures/api_blueprint_and_swagger'
38
+ expect(api_description_source_path(path)).to match(/apiary\.apib$/)
39
+ expect { api_description_source_path(path) }.to output("WARNING: Both apiary.apib and swagger.yaml are present. The apiary.apib file will be used. To override this selection specify path to desired file\n").to_stderr
40
+ end
41
+ end
42
+
43
+ context 'empty folder' do
44
+ it 'should raise error saying that file doesn\'t exists' do
45
+ path = 'spec/fixtures/empty_folder'
46
+ expect { api_description_source_path(path)}.to raise_error('No API Description Document found')
47
+ end
48
+ end
49
+
50
+ context 'existing file is in path' do
51
+ it 'should return same path as was entered' do
52
+ path = 'spec/fixtures/only_api_blueprint/apiary.apib'
53
+ expect(api_description_source_path(path)).to equal(path)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '#api_description_source' do
59
+ it 'get file with and without BOM' do
60
+ file1 = api_description_source('spec/fixtures/apiary.apib')
61
+ file2 = api_description_source('spec/fixtures/apiary_with_bom.apib')
62
+ expect(file1).not_to be_nil
63
+ expect(file2).not_to be_nil
64
+ expect(file1).to eq(file2)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,5 @@
1
+ FORMAT: 1A
2
+
3
+ # Minimal
4
+
5
+ Minimal API Blueprint
@@ -0,0 +1,5 @@
1
+ swagger: "2.0"
2
+ info:
3
+ version: 1.0.0
4
+ title: Minimal
5
+ description: Minimal Swagger
File without changes