praxis-docs-code-examples 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 78b2b5d8cbc88958c1bd5c2696235df1bc5b1b5e
4
+ data.tar.gz: 6fcb0852e63038db72e9508a8e68ab1a7670d2a2
5
+ SHA512:
6
+ metadata.gz: bfebbb6a3f42111d8b75cc81f0043293c6f3af9aa8cd7715d973135e07b5c08e3f4a2315e60e1bf32a99f07f6d1a55239e6ece701f7de863e10047f460646150
7
+ data.tar.gz: f81c7ad2152eb37b43714c46adee8f022048ff122993eff77b048a3679119df7140091764ba324f771e95fb14af76b50874caf44c173b9bf75dbb064a0f82fe4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in praxis-docs-code-examples.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jakub Hampl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # Praxis Docs Code Examples
2
+
3
+ This is a Praxis Plugin that adds code examples to the Praxis Docs Browser. Why
4
+ should you use it? Let's talk about a few reasons you probably shouldn't use it:
5
+
6
+ - Praxis currently doesn't have a way to describe an authentication mechanism for
7
+ the supported APIs. So if your API is authenticated, the examples won't actually work.
8
+ - The code generators don't necessarily (yet!) take into consideration everything
9
+ that can be expressed with the the Praxis design language and may fall
10
+ over. I recommend reviewing each example before publishing the generated code
11
+ examples to clients.
12
+ - The examples are fairly uninteresting uses of the API. A handwritten example of
13
+ doing something actually interesting with your API might be more interesting for
14
+ the reader.
15
+
16
+ That being said, this repo can serve as a good starting point for writing your own
17
+ code generators for the Praxis doc browser.
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'praxis-docs-code-examples'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install praxis-docs-code-examples
34
+
35
+ ## Usage
36
+
37
+ Add `application.bootloader.use Praxis::DocsCodeExamples` to your Praxis bootloader.
38
+
39
+ ## Development
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rightsale/praxis-docs-code-examples.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
File without changes
@@ -0,0 +1,121 @@
1
+ angular.module('PraxisDocBrowser').config(function(ExamplesProvider) {
2
+
3
+ function basicCode(action, info) {
4
+ var code = 'var request = new XMLHttpRequest();\n\n';
5
+ if (!info.endpoint) {
6
+ //throw 'Endpoint not defined in schema';
7
+ }
8
+ code += 'request.open(\'' + action.urls[0].verb +'\', \'' + info.endpoint + action.urls[0].example + '\');\n\n';
9
+
10
+ code += _.map(action.headers.type.attributes, function(value, key) {
11
+ return 'request.setRequestHeader(\''+ key + '\', \''+ value.example + '\');'
12
+ }).join('\n')
13
+
14
+ code += '\nrequest.setRequestHeader(\'Content-Type\', \'' + _.get(action, 'payload.examples.json.content_type', 'text/json') + '\');\n\n';
15
+
16
+ code += '\nrequest.onreadystatechange = function () {\n' +
17
+ ' if (this.readyState === 4) {\n' +
18
+ ' console.log(\'Status:\', this.status);\n' +
19
+ ' console.log(\'Headers:\', this.getAllResponseHeaders());\n' +
20
+ ' console.log(\'Body:\', this.responseText);\n' +
21
+ ' }\n' +
22
+ '};\n\n';
23
+
24
+ if (action.payload) {
25
+ code += 'var body = ' + action.payload.examples.json.body + ';\n\n';
26
+ code += 'request.send(JSON.stringify(body));';
27
+ } else {
28
+ code += 'request.send();';
29
+ }
30
+ return code;
31
+ }
32
+
33
+ function angularCode(resource, action, info) {
34
+ if (!info.endpoint) {
35
+ //throw 'Endpoint not defined in schema';
36
+ }
37
+ var code = '// Define Client:\n';
38
+ code += 'angular.module(\'Models\').service(\'' + resource.display_name + '\', [\'$http\', function($http) {\n';
39
+ code += ' this.' + _.camelCase(action.name) + ' = function(data) {\n';
40
+ // _.each(_.get(action, 'params.type.attributes'), function(opts, val) {
41
+ // if (opts.source === 'url') {
42
+ // code += ' var ' + _.camelCase(val) + ' = data.' + _.camelCase(val) + ';\n';
43
+ // }
44
+ // });
45
+ code += ' return $http({\n';
46
+ code += ' method: \'' + action.urls[0].verb + '\',\n';
47
+ code += ' url: \'' + info.endpoint + (action.urls[0].path.replace(/:([\w_]+)/g, function(m, variable) {
48
+ return '\' + data.' + _.camelCase(variable) + ' + \'';
49
+ }) + '\'').replace(/ \+ ''$/, '') + ',\n';
50
+ var params = _.get(action, 'params.type.attributes', {});
51
+ if (_.some(params, {source: 'query'})) {
52
+ code += ' params: {\n';
53
+ _.each(params, function(opts, name) {
54
+ if (opts.source === 'query') {
55
+ code += ' ' + name + ': data.' + _.camelCase(name) + ',\n';
56
+ }
57
+ });
58
+ code += ' },\n'
59
+ }
60
+ var headers = _.get(action, 'headers.type.attributes', {});
61
+ if (_.keys(headers).length > 0) {
62
+ code += ' headers: {\n';
63
+ _.each(headers, function(opts, name) {
64
+ if (opts.values && opts.values.length === 1) {
65
+ code += ' ' + _.snakeCase(name) + ': \'' + opts.values[0] + '\',\n';
66
+ } else {
67
+ code += ' ' + _.snakeCase(name) + ': data.' + _.camelCase(name) + ',\n';
68
+ }
69
+ });
70
+ code += ' },\n';
71
+ }
72
+ var payload = _.get(action, 'payload.type.attributes', {});
73
+ if (_.keys(payload).length > 0) {
74
+ code += ' data: {\n';
75
+ _.each(payload, function(opts, name) {
76
+ if (opts.values && opts.values.length === 1) {
77
+ code += ' ' + name + ': \'' + opts.values[0] + '\',\n';
78
+ } else {
79
+ code += ' ' + name + ': data.' + _.camelCase(name) + ',\n';
80
+ }
81
+ });
82
+ code += ' },\n'
83
+ }
84
+ code += ' });\n';
85
+ code += ' };\n'
86
+ code += '}]);\n\n';
87
+ code += '// Example Usage:\n';
88
+ code += 'angular.module(\'Controllers\').controller(\'' + resource.display_name + 'Controller\', [\'' + resource.display_name + '\', \'$scope\', function(' + resource.display_name + ', $scope) {\n';
89
+ code += ' ' + resource.display_name + '.' + _.camelCase(action.name) + '({\n';
90
+ _.each(params, function(opts, name) {
91
+ code += ' ' + name + ': ' + JSON.stringify(opts.example, null, 2) +',\n';
92
+ });
93
+ _.each(headers, function(opts, name) {
94
+ if (opts.example && (!opts.values || opts.values.length != 1)) {
95
+ code += ' ' + _.snakeCase(name) + ': ' + JSON.stringify(opts.example, null, 2) +',\n';
96
+ }
97
+ });
98
+ _.each(payload, function(opts, name) {
99
+ if (opts.example && (!opts.values || opts.values.length != 1)) {
100
+ code += ' ' + name + ': ' + JSON.stringify(opts.example, null, 2) +',\n';
101
+ }
102
+ });
103
+ code += ' }).then(function(result) {\n';
104
+ code += ' $scope.result = result.data;\n'
105
+ code += ' });\n';
106
+ code += '}]);\n';
107
+ return code;
108
+ }
109
+
110
+ ExamplesProvider.register('browser-javascript', 'JavaScript', function($compile, $context, Documentation) {
111
+ "ngInject";
112
+ return Documentation.info($context.version).then(function(info) {
113
+ var template = '<h3>Plain JavaScript</h3>\n<pre highlight="javascript">' + basicCode($context.action, info) + '</pre>\n\n';
114
+
115
+ template += '<h3>Angular</h3>\n<pre highlight="javascript">' + angularCode($context.resource, $context.action, info) + '</pre>\n\n';
116
+
117
+
118
+ return $compile(template);
119
+ });
120
+ });
121
+ });
@@ -0,0 +1,64 @@
1
+ angular.module('PraxisDocBrowser').config(function(ExamplesProvider) {
2
+
3
+ function basicCode(action, info) {
4
+ var code = 'require \'restclient\'\n\n';
5
+ if (!info.endpoint) {
6
+ //throw 'Endpoint not defined in schema';
7
+ }
8
+ if (action.headers) {
9
+ code += 'headers = {\n';
10
+ _.each(_.get(action, 'headers.type.attributes'), function(value, key) {
11
+ code += ' ' + JSON.stringify(key) + ' => '+ JSON.stringify(value.example) + ',\n'
12
+ });
13
+ code += '}\n'
14
+ }
15
+
16
+ if (action.payload) {
17
+ code += 'payload = {\n'
18
+ _.each(_.get(action, 'payload.type.attributes'), function(value, key) {
19
+ if (value.example) {
20
+ code += ' :' + key + ' => '+ JSON.stringify(value.example) + ',\n';
21
+ }
22
+ })
23
+ code += '}\n'
24
+ }
25
+
26
+ var params = '';
27
+ _.each(_.get(action, 'params.type.attributes'), function(opts, name) {
28
+ if (opts.source === 'query') {
29
+ params += ' :' + name + ' => ' + JSON.stringify(opts.example) + ',\n';
30
+ }
31
+ });
32
+
33
+ var signature = 'response = RestClient.' + action.urls[0].verb.toLowerCase() + ' \'' + info.endpoint + action.urls[0].example + '\'';
34
+
35
+ if (action.payload) {
36
+ signature += ', payload';
37
+ }
38
+
39
+ if (action.headers && params.length > 0) {
40
+ code += 'headers[:params] = {\n' + params + '}\n';
41
+ signature += ', headers'
42
+ } else if (params.length > 0) {
43
+ 'params = {\n' + params + '}\n'
44
+ signature += ', :params => params'
45
+ } else {
46
+ signature += ', headers'
47
+ }
48
+
49
+ code += signature;
50
+
51
+ code += '\n\nputs response.inspect'
52
+
53
+ return code;
54
+ }
55
+
56
+ ExamplesProvider.register('ruby', 'Ruby', function($compile, $context, Documentation) {
57
+ "ngInject";
58
+ return Documentation.info($context.version).then(function(info) {
59
+ var template = '<pre highlight="ruby">' + basicCode($context.action, info) + '</pre>\n\n';
60
+
61
+ return $compile(template);
62
+ });
63
+ });
64
+ });
@@ -0,0 +1,15 @@
1
+ require "praxis-docs-code-examples/version"
2
+
3
+ module Praxis
4
+ class DocsCodeExamples < ::Praxis::Plugin
5
+ include Singleton
6
+ def setup!
7
+ puts "Registering"
8
+ register_doc_browser_plugin File.join(File.dirname(__FILE__), 'api_browser')
9
+ end
10
+
11
+ def config_key
12
+ :docs_code_examples
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module PraxisDocsCodeExamples
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'praxis-docs-code-examples/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "praxis-docs-code-examples"
8
+ spec.version = PraxisDocsCodeExamples::VERSION
9
+ spec.authors = ["Jakub Hampl"]
10
+ spec.email = ["honitom@seznam.cz"]
11
+
12
+ spec.summary = %q{Code examples for the Praxis Docs Browser }
13
+ spec.homepage = "https://github.com/rightscale/praxis-docs-code-examples"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.10"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: praxis-docs-code-examples
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jakub Hampl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - honitom@seznam.cz
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/api_browser/generators/curl.js
54
+ - lib/api_browser/generators/javascript.js
55
+ - lib/api_browser/generators/ruby.js
56
+ - lib/praxis-docs-code-examples.rb
57
+ - lib/praxis-docs-code-examples/version.rb
58
+ - praxis-docs-code-examples.gemspec
59
+ homepage: https://github.com/rightscale/praxis-docs-code-examples
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Code examples for the Praxis Docs Browser
83
+ test_files: []