Skip to content

Commit 263867c

Browse files
committed
Use a hash of options and output as a data attribute for use in javascript, also make easy use of Basic Auth
1 parent d213155 commit 263867c

File tree

4 files changed

+163
-15
lines changed

4 files changed

+163
-15
lines changed

app/assets/javascripts/grape_swagger_rails/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
//= require ./swagger
2222
//= require ./swagger-ui
2323
//= require ./highlight.7.3.pack
24+
//= require ./base64
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
*
3+
* Base64 encode / decode
4+
* http://www.webtoolkit.info/
5+
*
6+
**/
7+
var Base64 = {
8+
9+
// private property
10+
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
11+
12+
// public method for encoding
13+
encode : function(input) {
14+
var output = "";
15+
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
16+
var i = 0;
17+
18+
input = Base64._utf8_encode(input);
19+
20+
while (i < input.length) {
21+
22+
chr1 = input.charCodeAt(i++);
23+
chr2 = input.charCodeAt(i++);
24+
chr3 = input.charCodeAt(i++);
25+
26+
enc1 = chr1 >> 2;
27+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
28+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
29+
enc4 = chr3 & 63;
30+
31+
if (isNaN(chr2)) {
32+
enc3 = enc4 = 64;
33+
} else if (isNaN(chr3)) {
34+
enc4 = 64;
35+
}
36+
37+
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
38+
39+
}
40+
41+
return output;
42+
},
43+
44+
// public method for decoding
45+
decode : function(input) {
46+
var output = "";
47+
var chr1, chr2, chr3;
48+
var enc1, enc2, enc3, enc4;
49+
var i = 0;
50+
51+
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
52+
53+
while (i < input.length) {
54+
55+
enc1 = this._keyStr.indexOf(input.charAt(i++));
56+
enc2 = this._keyStr.indexOf(input.charAt(i++));
57+
enc3 = this._keyStr.indexOf(input.charAt(i++));
58+
enc4 = this._keyStr.indexOf(input.charAt(i++));
59+
60+
chr1 = (enc1 << 2) | (enc2 >> 4);
61+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
62+
chr3 = ((enc3 & 3) << 6) | enc4;
63+
64+
output = output + String.fromCharCode(chr1);
65+
66+
if (enc3 != 64) {
67+
output = output + String.fromCharCode(chr2);
68+
}
69+
if (enc4 != 64) {
70+
output = output + String.fromCharCode(chr3);
71+
}
72+
73+
}
74+
75+
output = Base64._utf8_decode(output);
76+
77+
return output;
78+
79+
},
80+
81+
// private method for UTF-8 encoding
82+
_utf8_encode : function(string) {
83+
string = string.replace(/\r\n/g, "\n");
84+
var utftext = "";
85+
86+
for (var n = 0; n < string.length; n++) {
87+
88+
var c = string.charCodeAt(n);
89+
90+
if (c < 128) {
91+
utftext += String.fromCharCode(c);
92+
} else if ((c > 127) && (c < 2048)) {
93+
utftext += String.fromCharCode((c >> 6) | 192);
94+
utftext += String.fromCharCode((c & 63) | 128);
95+
} else {
96+
utftext += String.fromCharCode((c >> 12) | 224);
97+
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
98+
utftext += String.fromCharCode((c & 63) | 128);
99+
}
100+
101+
}
102+
103+
return utftext;
104+
},
105+
106+
// private method for UTF-8 decoding
107+
_utf8_decode : function(utftext) {
108+
var string = "";
109+
var i = 0;
110+
var c = c1 = c2 = 0;
111+
112+
while (i < utftext.length) {
113+
114+
c = utftext.charCodeAt(i);
115+
116+
if (c < 128) {
117+
string += String.fromCharCode(c);
118+
i++;
119+
} else if ((c > 191) && (c < 224)) {
120+
c2 = utftext.charCodeAt(i + 1);
121+
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
122+
i += 2;
123+
} else {
124+
c2 = utftext.charCodeAt(i + 1);
125+
c3 = utftext.charCodeAt(i + 2);
126+
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
127+
i += 3;
128+
}
129+
130+
}
131+
132+
return string;
133+
}
134+
};

app/views/grape_swagger_rails/application/index.html.erb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html data-swagger-options="<%= GrapeSwaggerRails.options.to_json %>">
33
<head>
44
<title>Swagger UI</title>
55
<link href='//fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'/>
66
<%= stylesheet_link_tag 'grape_swagger_rails/application.css' %>
77
<%= javascript_include_tag 'grape_swagger_rails/application.js' %>
88
<script type="text/javascript">
99
$(function () {
10+
var options = $("html").data('swagger-options');
11+
1012
window.swaggerUi = new SwaggerUi({
11-
url: "<%= GrapeSwaggerRails.discoveryUrl %>",
12-
apiKey: "<%= GrapeSwaggerRails.apiKey %>",
13+
url: options.url,
1314
dom_id:"swagger-ui-container",
1415
supportHeaderParams: true,
15-
headers: "<%= GrapeSwaggerRails.headers.to_json %>",
16-
supportedSubmitMethods: ['get', 'post', 'put', 'patch', 'delete'],
16+
headers: options.headers,
17+
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
1718
onComplete: function(swaggerApi, swaggerUi){
1819
if(console) {
1920
console.log("Loaded SwaggerUI")
@@ -35,8 +36,13 @@
3536
var key = $('#input_apiKey')[0].value;
3637
console.log("key: " + key);
3738
if(key && key.trim() != "") {
39+
if (options.api_auth == 'basic') {
40+
key = "Basic " + Base64.encode(key);
41+
}
3842
console.log("added key " + key);
39-
window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "query"));
43+
window.authorizations.add("key", new ApiKeyAuthorization(options.api_key_name, key, options.api_key_type));
44+
} else {
45+
window.authorizations.add("key", null);
4046
}
4147
})
4248
window.swaggerUi.load();
@@ -47,7 +53,7 @@
4753
<body>
4854
<div id='header'>
4955
<div class="swagger-ui-wrap">
50-
<a id="logo" href="<%= GrapeSwaggerRails.appUrl %>"><%= GrapeSwaggerRails.appName %></a>
56+
<a id="logo" href="<%= GrapeSwaggerRails.options.app_url %>"><%= GrapeSwaggerRails.options.app_name %></a>
5157

5258
<form id='api_selector'>
5359
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>

lib/grape-swagger-rails.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
require "grape-swagger-rails/engine"
22

33
module GrapeSwaggerRails
4-
mattr_accessor :discoveryUrl, :apiKey, :headers,
5-
:appName, :appUrl
6-
7-
self.discoveryUrl = '/swagger_doc.json'
8-
self.apiKey = 'special-key'
9-
self.headers = {}
10-
self.appName = 'Swagger'
11-
self.appUrl = 'http://swagger.wordnik.com'
4+
class Options < Struct.new(:url, :api_key_name, :api_key_type, :api_auth, :headers, :app_name, :app_url)
5+
end
6+
7+
mattr_accessor :options
8+
9+
self.options = Options.new(
10+
url: '/swagger_doc.json',
11+
api_key_name: 'api_key',
12+
api_key_type: 'query',
13+
api_auth: '', # 'basic'
14+
headers: {},
15+
app_name: 'Swagger',
16+
app_url: 'http://swagger.wordnik.com'
17+
)
18+
1219
end
1320

0 commit comments

Comments
 (0)