apiaryio 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
+ sudo: false
2
3
  rvm:
3
4
  - "1.9.3"
4
5
  - "2.0.0"
@@ -17,13 +18,6 @@ matrix:
17
18
  - rvm: rbx-2
18
19
  cache: bundler
19
20
  notifications:
20
- email:
21
- recipients:
22
- - lukas@apiary.io
23
- - adam@apiary.io
24
- - ladislav@apiary.io
25
- on_success: change
26
- on_failure: always
27
21
  hipchat:
28
22
  rooms:
29
23
  secure: "HqBUrmf4P2bgvwb4b9PL1tBae2wcZaXadOgwRwAFhdhDZFHaKXIXlJRUW8tN\ntdO83OZgatxJrpWWQj8VYTfDuhlE3b3NxVWeXc3PkfrHmuvejQf4veh7kwR0\njLVE6jb+ZbFIwRE2W0VFLFKYQHI6PLem4W0OKXW5Shqzy8Ewlow="
data/README.md CHANGED
@@ -3,7 +3,7 @@ apiaryio
3
3
 
4
4
  Apiary.io CLI
5
5
 
6
- [![Build Status](https://travis-ci.org/apiaryio/apiary-client.png?branch=master)](https://travis-ci.org/apiaryio/apiary-client)
6
+ [![Build Status](https://travis-ci.org/apiaryio/apiary-client.png?branch=master)](https://travis-ci.org/apiaryio/apiary-client) [![Build status](https://ci.appveyor.com/api/projects/status/0hmkivbnhf9p3f8d/branch/master?svg=true)](https://ci.appveyor.com/project/Apiary/apiary-client/branch/master)
7
7
 
8
8
 
9
9
  ## Install
@@ -0,0 +1,22 @@
1
+ ---
2
+ version: "{build}"
3
+ clone_depth: 10
4
+ install:
5
+ - SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
6
+ - ruby --version
7
+ - gem --version
8
+ - gem install bundler --quiet --no-ri --no-rdoc
9
+ - bundler --version
10
+ build_script:
11
+ - bundle install
12
+ test_script:
13
+ - rake test
14
+ artifacts:
15
+ - path: pkg\*.gem
16
+ environment:
17
+ matrix:
18
+ - ruby_version: "193"
19
+ - ruby_version: "200"
20
+ - ruby_version: "200-x64"
21
+ - ruby_version: "21"
22
+ - ruby_version: "21-x64"
@@ -1,8 +1,7 @@
1
1
  require 'rubygems'
2
- require "apiary/version"
3
- require "apiary/cli"
4
- require "apiary/common"
5
- Dir["#{File.dirname(__FILE__)}/apiary/command/*.rb"].each { |f| require(f) }
2
+ require 'apiary/version'
3
+ require 'apiary/cli'
4
+ require 'apiary/common'
6
5
 
7
6
  module Apiary
8
7
  end
@@ -1,39 +1,39 @@
1
1
  # encoding: utf-8
2
- require "thor"
3
- require "apiary/command/fetch"
4
- require "apiary/command/preview"
5
- require "apiary/command/publish"
2
+ require 'thor'
3
+ require 'apiary/command/fetch'
4
+ require 'apiary/command/preview'
5
+ require 'apiary/command/publish'
6
6
 
7
7
  module Apiary
8
8
  class CLI < Thor
9
9
 
10
- desc "fetch", "Fetch apiary.apib from API_NAME.apiary.io"
10
+ desc 'fetch', 'Fetch apiary.apib from API_NAME.apiary.io'
11
11
  method_option :api_name, :type => :string, :required => true, :default => ''
12
- method_option :api_host, :type => :string, :banner => "HOST", :desc => "Specify apiary host"
13
- method_option :output, :type => :string, :banner => "FILE", :desc => "Write apiary.apib into specified file"
12
+ method_option :api_host, :type => :string, :banner => 'HOST', :desc => 'Specify apiary host'
13
+ method_option :output, :type => :string, :banner => 'FILE', :desc => 'Write apiary.apib into specified file'
14
14
 
15
15
  def fetch
16
16
  cmd = Apiary::Command::Fetch.new options
17
17
  cmd.execute
18
18
  end
19
19
 
20
- desc "preview", "Show API documentation in default browser"
21
- method_option :browser, :type => :string, :enum => %w(chrome safari firefox), :banner => "chrome|safari|firefox", :desc => "Show API documentation in specified browser"
22
- method_option :output, :type => :string, :banner => "FILE", :desc => "Write generated HTML into specified file"
23
- method_option :path, :type => :string, :desc => "Specify path to blueprint file", :default => 'apiary.apib'
24
- method_option :api_host, :type => :string, :banner => "HOST", :desc => "Specify apiary host"
25
- method_option :server, :type => :boolean, :desc => "Start standalone web server on port 8080"
26
- method_option :port, :type => :numeric, :banner => "PORT", :desc => "Set port for --server option"
20
+ desc 'preview', 'Show API documentation in default browser'
21
+ method_option :browser, :type => :string, :enum => %w(chrome safari firefox), :banner => 'chrome|safari|firefox', :desc => 'Show API documentation in specified browser'
22
+ method_option :output, :type => :string, :banner => 'FILE', :desc => 'Write generated HTML into specified file'
23
+ method_option :path, :type => :string, :desc => 'Specify path to blueprint file', :default => 'apiary.apib'
24
+ method_option :api_host, :type => :string, :banner => 'HOST', :desc => 'Specify apiary host'
25
+ method_option :server, :type => :boolean, :desc => 'Start standalone web server on port 8080'
26
+ method_option :port, :type => :numeric, :banner => 'PORT', :desc => 'Set port for --server option'
27
27
 
28
28
  def preview
29
29
  cmd = Apiary::Command::Preview.new options
30
30
  cmd.execute
31
31
  end
32
32
 
33
- desc "publish", "Publish apiary.apib on docs.API_NAME.apiary.io"
34
- method_option :message, :type => :string, :banner => "COMMIT_MESSAGE", :desc => "Publish with custom commit message"
35
- method_option :path, :type => :string, :desc => "Specify path to blueprint file", :default => 'apiary.apib'
36
- method_option :api_host, :type => :string, :banner => "HOST", :desc => "Specify apiary host"
33
+ desc 'publish', 'Publish apiary.apib on docs.API_NAME.apiary.io'
34
+ method_option :message, :type => :string, :banner => 'COMMIT_MESSAGE', :desc => 'Publish with custom commit message'
35
+ method_option :path, :type => :string, :desc => 'Specify path to blueprint file', :default => 'apiary.apib'
36
+ method_option :api_host, :type => :string, :banner => 'HOST', :desc => 'Specify apiary host'
37
37
  method_option :api_name, :type => :string, :required => true, :default => ''
38
38
 
39
39
  def publish
@@ -41,7 +41,7 @@ module Apiary
41
41
  cmd.execute
42
42
  end
43
43
 
44
- desc "version", "Show version"
44
+ desc 'version', 'Show version'
45
45
  method_option :aliases => "-v"
46
46
 
47
47
  def version
@@ -8,7 +8,6 @@ module Apiary
8
8
  module Command
9
9
  # Retrieve blueprint from apiary
10
10
  class Fetch
11
-
12
11
  attr_reader :options
13
12
 
14
13
  # TODO: use OpenStruct to store @options
@@ -43,10 +42,10 @@ module Apiary
43
42
  end
44
43
 
45
44
  response = self.query_apiary(@options.api_host, @options.path)
46
- unless @options.output
47
- response["code"]
48
- else
45
+ if @options.output
49
46
  write_generated_path(response["code"], @options.output)
47
+ else
48
+ response["code"]
50
49
  end
51
50
  end
52
51
 
@@ -3,30 +3,39 @@ require 'rest_client'
3
3
  require 'rack'
4
4
  require 'ostruct'
5
5
  require 'json'
6
+ require 'tmpdir'
7
+ require 'erb'
6
8
 
7
9
  require "apiary/common"
10
+ require "apiary/helpers/javascript_helper"
8
11
 
9
12
  module Apiary
10
13
  module Command
11
14
  # Display preview of local blueprint file
12
15
  class Preview
13
16
 
17
+ include Apiary::Helpers::JavascriptHelper
18
+
19
+ PREVIEW_TEMPLATE_PATH = "#{File.expand_path File.dirname(__FILE__)}/../file_templates/preview.erb"
20
+
14
21
  BROWSERS = {
15
- :safari => "Safari",
16
- :chrome => "Google Chrome",
17
- :firefox => "Firefox"
22
+ :safari => 'Safari',
23
+ :chrome => 'Google Chrome',
24
+ :firefox => 'Firefox'
18
25
  }
19
26
 
20
27
  attr_reader :options
21
28
 
22
29
  def initialize(opts)
23
30
  @options = OpenStruct.new(opts)
24
- @options.path ||= "apiary.apib"
25
- @options.api_host ||= "api.apiary.io"
26
- @options.headers ||= {:accept => "text/html", :content_type => "text/plain"}
31
+ @options.path ||= 'apiary.apib'
32
+ @options.api_host ||= 'api.apiary.io'
33
+ @options.headers ||= {:accept => 'text/html', :content_type => 'text/plain'}
27
34
  @options.port ||= 8080
28
35
  @options.proxy ||= ENV['http_proxy']
29
36
  @options.server ||= false
37
+
38
+ validate_apib_file
30
39
  end
31
40
 
32
41
  def execute
@@ -42,12 +51,12 @@ module Apiary
42
51
  end
43
52
 
44
53
  def show
45
- generate_static(@options.path)
54
+ generate_static
46
55
  end
47
56
 
48
- def validate_apib_file(apib_file)
57
+ def validate_apib_file
49
58
  common = Apiary::Common.new
50
- common.validate_apib_file(apib_file)
59
+ common.validate_apib_file(@options.path)
51
60
  end
52
61
 
53
62
  def path
@@ -66,45 +75,12 @@ module Apiary
66
75
 
67
76
  def run_server
68
77
  app = self.rack_app do
69
- self.query_apiary(@options.api_host, @options.path)
78
+ generate
70
79
  end
71
80
 
72
81
  Rack::Server.start(:Port => @options.port, :app => app)
73
82
  end
74
83
 
75
- def preview_path(path)
76
- basename = File.basename(@options.path)
77
- "/tmp/#{basename}-preview.html"
78
- end
79
-
80
- def query_apiary(host, path)
81
- url = "https://#{host}/blueprint/generate"
82
- if validate_apib_file(path)
83
- begin
84
- data = File.read(path)
85
- rescue
86
- abort "File #{path} not found."
87
- end
88
-
89
- RestClient.proxy = @options.proxy
90
-
91
- begin
92
- RestClient.post(url, data, @options.headers)
93
- rescue RestClient::BadRequest => e
94
- err = JSON.parse e.response
95
- if err.has_key? 'parserError'
96
- abort "#{err['message']}: #{err['parserError']} (Line: #{err['line']}, Column: #{err['column']})"
97
- else
98
- abort "Apiary service responded with an error: #{err['message']}"
99
- end
100
- rescue RestClient::Exception => e
101
- abort "Apiary service responded with an error: #{e.message}"
102
- end
103
- else
104
- abort "Sorry, Apiary can't display invalid blueprint."
105
- end
106
- end
107
-
108
84
  # TODO: add linux and windows systems
109
85
  def open_generated_page(path)
110
86
  exec "open #{browser_options} #{path}"
@@ -114,17 +90,49 @@ module Apiary
114
90
  File.write(outfile, File.read(path))
115
91
  end
116
92
 
117
- def generate_static(path)
118
- File.open(preview_path(path), "w") do |file|
119
- file.write(query_apiary(@options.api_host, path))
93
+ def generate
94
+ template = load_preview_template
95
+
96
+ data = {
97
+ title: File.basename(@options.path, '.*'),
98
+ blueprint: load_blueprint
99
+ }
100
+
101
+ template.result(binding)
102
+ end
103
+
104
+ def generate_static
105
+ preview_string = generate
106
+
107
+ File.open(preview_path, 'w') do |file|
108
+ file.write preview_string
109
+ file.flush
120
110
  @options.output ? write_generated_path(file.path, @options.output) : open_generated_page(file.path)
121
111
  end
122
112
  end
123
113
 
114
+ def load_blueprint
115
+ file = File.open @options.path, 'r'
116
+ file.read
117
+ end
118
+
119
+ def preview_path
120
+ basename = File.basename(@options.path, '.*')
121
+ temp = Dir.tmpdir
122
+ "#{temp}/#{basename}-preview.html"
123
+ end
124
+
125
+ def load_preview_template
126
+ file = File.open(PREVIEW_TEMPLATE_PATH, 'r')
127
+ template_string = file.read
128
+ ERB.new(template_string)
129
+ end
130
+
124
131
  private
125
- def browser_options
126
- "-a #{BROWSERS[@options.browser.to_sym]}" if @options.browser
127
- end
132
+
133
+ def browser_options
134
+ "-a #{BROWSERS[@options.browser.to_sym]}" if @options.browser
135
+ end
128
136
  end
129
137
  end
130
138
  end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title><%= data[:title] %></title>
6
+ </head>
7
+ <body>
8
+ <script src="https://api.apiary.io/seeds/embed.js"></script>
9
+ <script>
10
+ var embed = new Apiary.Embed({
11
+ apiBlueprint: "<%= escape_javascript data[:blueprint] %>"
12
+ });
13
+ </script>
14
+ </body>
15
+ </html>
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ module Apiary
3
+ module Helpers
4
+ module JavascriptHelper
5
+ JS_ESCAPE_MAP = {
6
+ '\\' => '\\\\',
7
+ '</' => '<\/',
8
+ "\r\n" => '\n',
9
+ "\n" => '\n',
10
+ "\r" => '\n',
11
+ '"' => '\\"',
12
+ "'" => "\\'"
13
+ }
14
+
15
+ JS_ESCAPE_MAP["\342\200\250".force_encoding(Encoding::UTF_8).encode!] = '&#x2028;'
16
+ JS_ESCAPE_MAP["\342\200\251".force_encoding(Encoding::UTF_8).encode!] = '&#x2029;'
17
+
18
+ def escape_javascript(javascript)
19
+ if javascript
20
+ javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] }
21
+ else
22
+ ''
23
+ end
24
+ end
25
+
26
+ alias_method :j, :escape_javascript
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Apiary
2
- VERSION = "0.2.5"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apiary::Command::Preview do
4
+
5
+ let(:command) do
6
+ opts = {
7
+ path: "#{File.expand_path File.dirname(__FILE__)}/../../features/fixtures/apiary.apib"
8
+ }
9
+ Apiary::Command::Preview.new(opts)
10
+ end
11
+
12
+ it 'check tmp path if contains filename' do
13
+ expect(command.preview_path()).to end_with('apiary-preview.html')
14
+ end
15
+
16
+ it 'shoud contain html5 doctype' do
17
+ expect(command.generate()).to include('<!DOCTYPE html>')
18
+ end
19
+
20
+ it 'should contain embed javascript' do
21
+ expect(command.generate()).to include('https://api.apiary.io/seeds/embed.js')
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apiaryio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-05 00:00:00.000000000 Z
12
+ date: 2015-05-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -193,6 +193,7 @@ files:
193
193
  - README.md
194
194
  - Rakefile
195
195
  - apiary.gemspec
196
+ - appveyor.yml
196
197
  - bin/apiary
197
198
  - circle.yml
198
199
  - features/fetch.feature
@@ -211,9 +212,12 @@ files:
211
212
  - lib/apiary/command/preview.rb
212
213
  - lib/apiary/command/publish.rb
213
214
  - lib/apiary/common.rb
215
+ - lib/apiary/file_templates/preview.erb
216
+ - lib/apiary/helpers/javascript_helper.rb
214
217
  - lib/apiary/version.rb
215
218
  - spec/cli_spec.rb
216
219
  - spec/command/fetch_spec.rb
220
+ - spec/command/preview_spec.rb
217
221
  - spec/common_spec.rb
218
222
  - spec/spec_helper.rb
219
223
  - spec/support/aruba.rb
@@ -230,18 +234,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
230
234
  - - ! '>='
231
235
  - !ruby/object:Gem::Version
232
236
  version: '0'
233
- segments:
234
- - 0
235
- hash: -821896092872960155
236
237
  required_rubygems_version: !ruby/object:Gem::Requirement
237
238
  none: false
238
239
  requirements:
239
240
  - - ! '>='
240
241
  - !ruby/object:Gem::Version
241
242
  version: '0'
242
- segments:
243
- - 0
244
- hash: -821896092872960155
245
243
  requirements: []
246
244
  rubyforge_project:
247
245
  rubygems_version: 1.8.23
@@ -261,6 +259,7 @@ test_files:
261
259
  - features/version.feature
262
260
  - spec/cli_spec.rb
263
261
  - spec/command/fetch_spec.rb
262
+ - spec/command/preview_spec.rb
264
263
  - spec/common_spec.rb
265
264
  - spec/spec_helper.rb
266
265
  - spec/support/aruba.rb