rack-json_stringify 0.0.3

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: 0660a4ebbc95c9d5b03722a3d8cceb7c4f8c9d03
4
+ data.tar.gz: 334f86bd8218b72ba303d390a7897a3d55ec86a0
5
+ SHA512:
6
+ metadata.gz: 94b42fdecc989afb8229674d42bd09a7fa57238db8fb5aaf20ee953074ddd39678e067bb50755d2d80a196073b84f467b3239988d83c09ee96928c458ae90a10
7
+ data.tar.gz: fe9c27d2e13929b6f6c2da99158df45d08b1aea3c3a76347491195cb6246e83eb5ab74922572a13dc9df3ae47f76a4352ec471145b2f22f3ccd4189474a4bf6c
@@ -0,0 +1,61 @@
1
+ # rack-jsonp-middleware
2
+ [![Travis CI Status](https://travis-ci.org/robertodecurnex/rack-json_stringify.png)](https://travis-ci.org/robertodecurnex/rack-json_stringify) [![Gemnasium Dependencies Status](https://gemnasium.com/robertodecurnex/rack-json_stringify.png)](https://gemnasium.com/robertodecurnex/rack-json_stringify) [![Code Climate](https://codeclimate.com/github/robertodecurnex/rack-json_stringify.png)](https://codeclimate.com/github/robertodecurnex/rack-json_stringify) [![Coverage Status](https://coveralls.io/repos/robertodecurnex/rack-json_stringify/badge.png?branch=master)](https://coveralls.io/r/robertodecurnex/rack-json_stringify)
3
+
4
+ takes any JSON response and format all the values (but null) to string whenever the proper header value is set.
5
+
6
+ ## Overview
7
+
8
+ ## Authors
9
+
10
+ Roberto Decurnex (decurnex.roberto@gmail.com)
11
+
12
+ ## Contributors
13
+
14
+ ## Install
15
+
16
+ If you are using Bundler you can easily add the following line to your Gemfile:
17
+
18
+ gem 'rack-json_stringify'
19
+
20
+ Or you can just install it as a ruby gem by running:
21
+
22
+ $ gem install rack-json_stringify
23
+
24
+ ## Configuration
25
+
26
+ ### Rails 3
27
+
28
+ In your `config/application.rb` file add:
29
+
30
+ require 'rack/json_stringify'
31
+
32
+ And, within the config block:
33
+
34
+ config.middleware.use Rack::JsonStringify
35
+
36
+ ### Rails 2
37
+
38
+ Same as for Rails 3 but modifying the `config/environment.rb` file instead.
39
+
40
+ ### Rack Apps
41
+
42
+ In your `config.ru` file add the following lines:
43
+
44
+ require 'rack/json_stringify'
45
+ use Rack::JsonStringify
46
+
47
+ ## Download
48
+
49
+ You can also clone the project with Git by running:
50
+ $ git clone git://github.com/robertodecurnex/rack-json_stringify
51
+
52
+ ## Examples
53
+
54
+ Given that http://domain.com/action.json returns:
55
+
56
+ {"key":1}
57
+
58
+ Then http://domain.com/action.json with the X-ACCEPTED-TYPES=String will return:
59
+
60
+ {"key":"1"}
61
+
@@ -0,0 +1,65 @@
1
+ require 'json'
2
+
3
+ module Rack
4
+
5
+ class JsonStringify
6
+
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ @env = env
13
+
14
+ status, headers, body = @app.call(env)
15
+
16
+ return [400,{},[]] if self.stringify? && !self.json_response?(headers['Content-Type'])
17
+
18
+ if self.stringify?
19
+ json = ''
20
+ body.each { |s| json << s }
21
+ body = [JSON.dump(self.stringify(JSON.parse(json)))]
22
+ headers['Content-Length'] = Rack::Utils.bytesize(body[0]).to_s
23
+ end
24
+
25
+ [status, headers, body]
26
+ end
27
+
28
+ protected
29
+
30
+ # Returns true whenever the response Content Type is set to application/json
31
+ #
32
+ # @param [String] content_Ctype the response content type
33
+ # @return [TrueClass,FalseClass]
34
+ def json_response?(content_type)
35
+ !content_type.nil? && !content_type.match(/^application\/json/i).nil?
36
+ end
37
+
38
+ # Returns the given element with its value/all its values as String (except for nil, true and fasle)
39
+ #
40
+ # @param [String,Hash,Array,TrueClass,FalseClass,#to_s] element the element to be stringified
41
+ # @return [String] the stringified element
42
+ def stringify(element)
43
+ return element if [nil, true, false].include?(element)
44
+
45
+ case element.class.to_s
46
+ when 'Hash'
47
+ element.dup.each { |key, value| element[key] = self.stringify(value) }
48
+ return element
49
+ when 'Array'
50
+ element.collect { |value| self.stringify(value) }
51
+ else
52
+ return element.to_s
53
+ end
54
+ end
55
+
56
+ # Returns true whenever the current request must be stringified
57
+ #
58
+ # @return [TrueClass,FalseClass]
59
+ def stringify?
60
+ "#{@env['HTTP_X_ACCEPTED_TYPES']}".downcase == 'string'
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,9 @@
1
+ module Rack
2
+
3
+ class JsonStringify
4
+
5
+ VERSION = "0.0.3"
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rack::JsonStringify do
6
+
7
+ before :each do
8
+ @response_status = 200
9
+ @response_headers = {
10
+ 'Content-Length' => '57'
11
+ }
12
+ @response_body = ['{"key1":666,"key2":"666","key3":[1,"2"],"key4":{"key5":3}}']
13
+
14
+ @app = lambda do |params|
15
+ [@response_status, @response_headers, @response_body]
16
+ end
17
+ end
18
+
19
+ describe 'when an non stringified response is requested' do
20
+
21
+ before :each do
22
+ @request = Rack::MockRequest.env_for('/action.json')
23
+ @non_stringified_response = Rack::JsonStringify.new(@app).call(@request)
24
+ @non_stringified_response_status, @non_stringified_response_headers, @non_stringified_response_body = @non_stringified_response
25
+ end
26
+
27
+ it 'should not modify the response status code' do
28
+ @non_stringified_response_status.should == @response_status
29
+ end
30
+
31
+ it 'should update the response content length to the new value' do
32
+ @non_stringified_response_headers['Content-Length'].should == @response_headers['Content-Length']
33
+ end
34
+
35
+ it 'should stringify every value of the response json' do
36
+ @non_stringified_response_body.should == @response_body
37
+ end
38
+
39
+ end
40
+
41
+ describe 'when an stringified response is requested' do
42
+
43
+ before :each do
44
+ @request = Rack::MockRequest.env_for('/action.json', {'X-ACCEPTED-TYPES' => 'String'})
45
+ @stringified_response = Rack::JsonStringify.new(@app).call(@request)
46
+ @stringified_response_status, @stringified_response_headers, @stringified_response_body = @stringified_response
47
+ end
48
+
49
+ it 'should not modify the response status code' do
50
+ @stringified_response_status.should == @response_status
51
+ end
52
+
53
+ it 'should update the response content length to the new value' do
54
+ @stringified_response_headers['Content-Length'].should == '64'
55
+ end
56
+
57
+ it 'should stringify every value of the response json' do
58
+ @stringified_response_body.should == ['{"key1":"666","key2":"666","key3":["1","2"],"key4":{"key5":"3"}}']
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,11 @@
1
+ # Create and push coverage reports
2
+ # This MUST be done before any other requrie in order to get the reports.
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+ #########################################################################
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+
10
+ require 'rack'
11
+ require 'rack/json_stringify'
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-json_stringify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Decurnex
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - decurnex.roberto@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - lib/rack/json_stringify.rb
105
+ - lib/rack/json_stringify/version.rb
106
+ - spec/spec_helper.rb
107
+ - spec/json_stringify_spec.rb
108
+ - README.md
109
+ homepage: ''
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.0.6
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: This Rack middleware takes any JSON response and format all the values (but
133
+ null) to string whenever the proper header value is set.
134
+ test_files:
135
+ - spec/spec_helper.rb
136
+ - spec/json_stringify_spec.rb
137
+ has_rdoc: