grape-slate 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e5f0ec63333f2ed9755fbd1a9e739f255c37857
4
- data.tar.gz: 1ea43b80a15189cc8f4206ccc9106be2768dffbc
3
+ metadata.gz: 5046a1a84a749c671e8b84fbd68de5ec134223cd
4
+ data.tar.gz: c594e654de27ff590cea59d4866a1034bd4155c5
5
5
  SHA512:
6
- metadata.gz: e91a4c1503f278fb21f166c9776d46da546dadfb0f7bc2a5c03b0a06cd26630eea431fb1483c614bba7304cc6f4b823e868231c4d9029e2678107d50752ab36f
7
- data.tar.gz: 54d8ced9b41b87f6d82d9160b3b4849b757bb56671054a48d5321abfe29afe43a1ededab84469e0c7abc794c86faacbefbbf230b1f1d824227046a1a3209a168
6
+ metadata.gz: a3ceb49766fa67ebc883815837914a7504c66765b33ae0566a9ac8bb7458fee848c846a6dac86b356341439cab0fa6a8dec1cd57497edc0aa951c58651b09d98
7
+ data.tar.gz: 744ef5f8a0c94a2c6c3a7ab169b3e19b1ac6a83dcfc87751d46543d10018942183a3d23aa20b9d004ec53e1edf5d478b5f14d790baed90fdf92fde5af8c561b8
data/Gemfile CHANGED
@@ -4,5 +4,3 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'grape', github: 'intridea/grape'
7
-
8
- gem 'grape-markdown', path: '/var/gems/grape-markdown'
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_runtime_dependency 'grape-markdown', '~> 0.0'
21
+ spec.add_runtime_dependency 'grape-markdown', '~> 0.0.3'
22
22
 
23
23
  spec.add_development_dependency 'coveralls', '~> 0.7'
24
24
  spec.add_development_dependency 'rspec', '~> 2.14'
@@ -1,24 +1,18 @@
1
1
  require 'grape-markdown'
2
2
 
3
3
  module GrapeSlate
4
- autoload :Version, 'grape-slate/version'
5
- autoload :Config, 'grape-slate/config'
6
- autoload :SampleGenerator, 'grape-slate/sample_generator'
7
- autoload :Document, 'grape-slate/document'
4
+ autoload :Version, 'grape-slate/version'
5
+ autoload :SampleGenerator, 'grape-slate/sample_generator'
6
+ autoload :Document, 'grape-slate/document'
7
+ autoload :ExampleGenerator, 'grape-slate/example_generator'
8
8
 
9
9
  def self.config
10
- block_given? ? yield(Config) : Config
11
- end
12
-
13
- class UnsupportedIDType < StandardError
14
- def message
15
- 'Unsupported id type, supported types are [integer, uuid, bson]'
16
- end
17
- end
18
-
19
- class BSONNotDefinied < StandardError
20
- def message
21
- 'BSON type id requested but bson library is not present'
10
+ if block_given?
11
+ yield(GrapeMarkdown::Configuration)
12
+ else
13
+ GrapeMarkdown::Configuration
22
14
  end
23
15
  end
24
16
  end
17
+
18
+ GrapeMarkdown::Configuration.extend :host
@@ -1,55 +1,17 @@
1
1
  module GrapeSlate
2
2
  class Document < GrapeMarkdown::Document
3
- attr_reader :api_class, :document_template, :properties_template
4
-
5
- delegate(*GrapeMarkdown::Config::SETTINGS, to: 'GrapeSlate::Config')
3
+ attr_reader :shell_template
6
4
 
7
5
  def initialize(api_class)
8
- @api_class = api_class
9
- @document_template = template_for(:document)
10
- @properties_template = template_for(:properties)
11
- end
12
-
13
- def generate
14
- ERB.new(document_template, nil, '-').result(binding)
15
- end
16
-
17
- def write
18
- fail 'Not yet supported'
19
- end
20
-
21
- def routes
22
- @routes ||= api_class.routes.map do |route|
23
- GrapeMarkdown::Route.new(route)
24
- end
25
- end
26
-
27
- def resources
28
- @resources ||= begin
29
- grouped_routes = routes.group_by(&:route_name).reject do |name, routes|
30
- resource_exclusion.include?(name.to_sym)
31
- end
32
-
33
- grouped_routes.map do |name, routes|
34
- GrapeMarkdown::Resource.new(name, routes)
35
- end
36
- end
37
- end
6
+ super
38
7
 
39
- def properties_table(resource)
40
- ERB.new(properties_template, nil, '-').result(resource.resource_binding)
8
+ @shell_template = template_for(:shell)
41
9
  end
42
10
 
43
- def formatted_request_headers
44
- formatted_headers(GrapeSlate::Config.request_headers)
45
- end
11
+ def shell_example(route, resource)
12
+ example = ExampleGenerator::Shell.new(route, resource)
46
13
 
47
- def formatted_response_headers
48
- formatted_headers(GrapeSlate::Config.response_headers)
49
- end
50
-
51
- def show_request_sample?(route)
52
- %w(PUT POST).include?(route.route_method)
14
+ render(shell_template, example.example_binding)
53
15
  end
54
16
 
55
17
  private
@@ -60,19 +22,5 @@ module GrapeSlate
60
22
 
61
23
  File.read(path)
62
24
  end
63
-
64
- def formatted_headers(headers)
65
- return '' unless headers.present?
66
-
67
- spacer = "\n" + (' ' * 12)
68
-
69
- strings = headers.map do |header|
70
- key, value = *header.first
71
-
72
- "#{key}: #{value}"
73
- end
74
-
75
- " + Headers\n" + spacer + strings.join(spacer)
76
- end
77
25
  end
78
26
  end
@@ -0,0 +1,5 @@
1
+ module GrapeSlate
2
+ module ExampleGenerator
3
+ autoload :Shell, 'grape-slate/example_generator/shell'
4
+ end
5
+ end
@@ -0,0 +1,57 @@
1
+ class GrapeSlate::ExampleGenerator::Shell
2
+ attr_reader :route, :resource
3
+
4
+ delegate :route_name, to: :route
5
+ delegate :host, :request_headers, to: 'GrapeMarkdown::Configuration'
6
+
7
+ def initialize(route, resource)
8
+ @route = route
9
+ @resource = resource
10
+ end
11
+
12
+ def lines
13
+ @lines ||= [command, request, data, headers, verbose].flatten.compact
14
+ end
15
+
16
+ def example_binding
17
+ binding
18
+ end
19
+
20
+ private
21
+
22
+ def method
23
+ route.route_method
24
+ end
25
+
26
+ def command
27
+ "curl #{host}/#{route_name}/#{id}"
28
+ end
29
+
30
+ def request_by_id?
31
+ method == 'GET' && !route.list? || %w(PUT DELETE).include?(method)
32
+ end
33
+
34
+ def id
35
+ return unless request_by_id?
36
+
37
+ GrapeMarkdown::Configuration.generate_id
38
+ end
39
+
40
+ def request
41
+ return unless %w(POST PUT DELETE).include?(method)
42
+
43
+ "--request #{method}"
44
+ end
45
+
46
+ def data
47
+ "--data '#{resource.sample_request(false)}'"
48
+ end
49
+
50
+ def headers
51
+ request_headers.map { |header| "--header '#{header.first.join(': ')}'" }
52
+ end
53
+
54
+ def verbose
55
+ '--verbose'
56
+ end
57
+ end
@@ -19,9 +19,7 @@ toc_footers:
19
19
  <% resource_by_path.routes.each do |route| %>
20
20
  ## <%= route.route_description %>
21
21
 
22
- ```shell
23
- curl http://example.com
24
- ```
22
+ <%= shell_example(route, resource) %>
25
23
 
26
24
  > The above command returns JSON structured like this:
27
25
 
@@ -0,0 +1,12 @@
1
+ ```shell
2
+ <%
3
+ first = lines.shift
4
+ last = lines.pop
5
+ -%>
6
+ <%= first -%>
7
+ <%= ' \\' if lines.any? %>
8
+ <% lines.each do |line| -%>
9
+ <%= line -%> \
10
+ <% end -%>
11
+ <%= last %>
12
+ ```
@@ -1,3 +1,3 @@
1
1
  module GrapeSlate
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -5,9 +5,11 @@ describe GrapeSlate::Document do
5
5
 
6
6
  before do
7
7
  GrapeSlate.config do |config|
8
+ config.host = host
8
9
  config.name = name
9
10
  config.description = description
10
11
  config.resource_exclusion = [:admin]
12
+ config.include_root = true
11
13
  end
12
14
 
13
15
  GrapeSlate.config.request_headers = [
@@ -29,7 +31,6 @@ describe GrapeSlate::Document do
29
31
  subject { GrapeSlate::Document.new(klass).generate }
30
32
 
31
33
  it 'sets the title based on name' do
32
- puts subject
33
34
  expect(subject).to include("title: #{name} Reference")
34
35
  end
35
36
 
@@ -45,12 +46,12 @@ describe GrapeSlate::Document do
45
46
  expect(subject).to include(description)
46
47
  end
47
48
 
48
- # it 'includes groups for each resource' do
49
- # expect(subject).to include('# Group Widgets')
50
- # end
49
+ it 'includes a headline for each resource' do
50
+ expect(subject).to include('# Widgets')
51
+ end
51
52
 
52
- # it 'includes properties for the resources' do
53
- # expect(subject).to include('Properties')
54
- # end
53
+ it 'includes properties for the resources' do
54
+ expect(subject).to include('Properties')
55
+ end
55
56
  end
56
57
  end
@@ -1,4 +1,5 @@
1
1
  shared_context 'configuration' do
2
+ let(:host) { 'http://api.example.com' }
2
3
  let(:name) { 'some api v1' }
3
4
  let(:description) { 'some blueprint description' }
4
5
  let(:resource_exclusion) { [:admin, :swagger_docs] }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-slate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Allen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-25 00:00:00.000000000 Z
11
+ date: 2014-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape-markdown
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.0'
19
+ version: 0.0.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.0'
26
+ version: 0.0.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: coveralls
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -169,12 +169,14 @@ files:
169
169
  - Rakefile
170
170
  - grape-slate.gemspec
171
171
  - lib/grape-slate.rb
172
- - lib/grape-slate/config.rb
173
172
  - lib/grape-slate/document.rb
173
+ - lib/grape-slate/example_generator.rb
174
+ - lib/grape-slate/example_generator/shell.rb
174
175
  - lib/grape-slate/sample_generator.rb
175
176
  - lib/grape-slate/templates/document.md.erb
176
177
  - lib/grape-slate/templates/properties.md.erb
177
178
  - lib/grape-slate/templates/sample.md
179
+ - lib/grape-slate/templates/shell.md.erb
178
180
  - lib/grape-slate/version.rb
179
181
  - lib/grape/slate.rb
180
182
  - spec/grape-slate/document_spec.rb
@@ -1,62 +0,0 @@
1
- module GrapeSlate
2
- class Config
3
- SETTINGS = [
4
- :name,
5
- :description,
6
- :request_headers,
7
- :response_headers,
8
- :example_id_type,
9
- :resource_exclusion,
10
- :include_root
11
- ]
12
-
13
- class << self
14
- attr_accessor(*SETTINGS)
15
-
16
- def request_headers
17
- @request_headers ||= []
18
- end
19
-
20
- def response_headers
21
- @response_headers ||= []
22
- end
23
-
24
- def resource_exclusion
25
- @resource_exclusion ||= []
26
- end
27
-
28
- def include_root
29
- @include_root ||= false
30
- end
31
-
32
- def supported_id_types
33
- [:integer, :uuid, :bson]
34
- end
35
-
36
- def example_id_type=(value)
37
- fail UnsupportedIDType unless supported_id_types.include?(value)
38
-
39
- if value.to_sym == :bson && !Object.const_defined?('BSON')
40
- fail BSONNotDefinied
41
- end
42
-
43
- @example_id_type = value
44
- end
45
-
46
- def example_id_type
47
- @example_id_type ||= :integer
48
- end
49
-
50
- def generate_id
51
- case example_id_type
52
- when :integer
53
- SecureRandom.random_number(1000)
54
- when :uuid
55
- SecureRandom.uuid
56
- when :bson
57
- BSON::ObjectId.new.to_s
58
- end
59
- end
60
- end
61
- end
62
- end