jsonview 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +15 -0
- data/Rakefile +2 -0
- data/jsonview.gemspec +17 -0
- data/lib/jsonview.rb +7 -0
- data/lib/jsonview/middleware.rb +69 -0
- data/lib/jsonview/railtie.rb +7 -0
- data/lib/jsonview/version.rb +3 -0
- metadata +74 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Preston Guillory
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Jsonview
|
2
|
+
|
3
|
+
Jsonview is a Rack middleware to help with inspecting JSON API responses in a browser. When the browser asks for HTML and *not* JSON and the server returns JSON anyway, this middleware will convert the response into a pretty-printed HTML representation. API calls made from Javascript or a server will not have Accept:text/html headers and should be unaffected.
|
4
|
+
|
5
|
+
Inspired by http://jsonview.com/
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'jsonview'
|
12
|
+
|
13
|
+
In Rails apps, it will add the middleware automatically. In other environments, you'll have to add it manually:
|
14
|
+
|
15
|
+
use Jsonview::Middleware
|
data/Rakefile
ADDED
data/jsonview.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/jsonview/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Preston Guillory"]
|
6
|
+
gem.email = ["pguillory@gmail.com"]
|
7
|
+
gem.description = %q{JSONView for Rack}
|
8
|
+
gem.summary = %q{Rack middleware to convert JSON responses into attractive HTML representations. Mimics the JSONView browser plugin.}
|
9
|
+
gem.homepage = "https://github.com/pguillory/jsonview"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "jsonview"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Jsonview::VERSION
|
17
|
+
end
|
data/lib/jsonview.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module Jsonview
|
2
|
+
class Middleware
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
response = @app.call(env)
|
9
|
+
accept = env['HTTP_ACCEPT'] || ''
|
10
|
+
|
11
|
+
if accept =~ /html/ && !(accept =~ /json/)
|
12
|
+
status, headers, body = response
|
13
|
+
content_type = headers['Content-Type'] || ''
|
14
|
+
|
15
|
+
if content_type =~ /json/
|
16
|
+
json = body.to_enum(:each).to_a.join
|
17
|
+
html = html_document(JSON.parse(json))
|
18
|
+
headers['Content-Type'] = "text/html;charset=utf-8"
|
19
|
+
headers['Content-Length'] = html.bytesize.to_s
|
20
|
+
response = [status, headers, [html]]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
response
|
25
|
+
end
|
26
|
+
|
27
|
+
def html_document(value)
|
28
|
+
<<-HTML
|
29
|
+
<html>
|
30
|
+
<head>
|
31
|
+
<style>
|
32
|
+
null { color: red }
|
33
|
+
string { color: green }
|
34
|
+
number, boolean { color: blue }
|
35
|
+
hash, array { display: block; margin-left: 20px }
|
36
|
+
key { font-weight: bold }
|
37
|
+
</style>
|
38
|
+
</head>
|
39
|
+
<body>
|
40
|
+
#{represent(value)}
|
41
|
+
</body>
|
42
|
+
</html>
|
43
|
+
HTML
|
44
|
+
end
|
45
|
+
|
46
|
+
def represent(value)
|
47
|
+
case value
|
48
|
+
when nil
|
49
|
+
"<null>NULL</null>"
|
50
|
+
when String
|
51
|
+
"<string>\"#{encode(value)}\"</string>"
|
52
|
+
when Fixnum
|
53
|
+
"<number>#{value.to_s}</number>"
|
54
|
+
when TrueClass, FalseClass
|
55
|
+
"<boolean>#{value.to_s}</boolean>"
|
56
|
+
when Array
|
57
|
+
"[\n#{value.map{|v| "<array>#{represent(v)}</array>\n" }.join}]\n"
|
58
|
+
when Hash
|
59
|
+
"{\n#{value.to_a.map{|(k, v)| "<hash><key>#{encode(k.to_s)}</key>: #{represent(v)}</hash>\n" }.join}}\n"
|
60
|
+
else
|
61
|
+
raise "Didn't expect a #{value.class}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def encode(string)
|
66
|
+
string.gsub /[<>&"]/, '<' => '<', '>' => '>', '&' => '&', '"' => '"'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonview
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Preston Guillory
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-12-10 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: JSONView for Rack
|
22
|
+
email:
|
23
|
+
- pguillory@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- Gemfile
|
32
|
+
- LICENSE
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- jsonview.gemspec
|
36
|
+
- lib/jsonview.rb
|
37
|
+
- lib/jsonview/middleware.rb
|
38
|
+
- lib/jsonview/railtie.rb
|
39
|
+
- lib/jsonview/version.rb
|
40
|
+
homepage: https://github.com/pguillory/jsonview
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.24
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Rack middleware to convert JSON responses into attractive HTML representations. Mimics the JSONView browser plugin.
|
73
|
+
test_files: []
|
74
|
+
|