rack-params_key_converter 0.0.1

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: 5b71456231c531fed160e397b5ca09c973e41fb3
4
+ data.tar.gz: ee2cb019cf80cffb37101088adfa86f8b0a1148c
5
+ SHA512:
6
+ metadata.gz: b53341f1904e5f15f99a5e85b2344902a63cfaa236b66728c781b0f481ffaf824b5f1a9f3bbb51a507453a73d2607c4b7341a05d36238e9b14292e6ef43d9719
7
+ data.tar.gz: 4e043b79f6de62796ccebd1a08a9c1e0d4fd88c68a177fc400af767e6517e2786b00606bbdbcda3d52c660094415820b0a5fe7989b401aff8a5fe5c6f4d753e9
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-params_key_converter.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 sogo.ohta
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.
@@ -0,0 +1,38 @@
1
+ # Rack::ParamsKeyConverter
2
+
3
+ This is a rack middleware that convert params key between JavaScript app and Rails app.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-params_key_converter'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rack-params_key_converter
20
+
21
+ ## Usage
22
+
23
+ Add below to your rails app's config/initializers
24
+ ```ruby
25
+ Rails.configuration.middleware.insert_after(ActionDispatch::ParamsParser, Rack::ParamsKeyConverter)
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( https://github.com/[my-github-username]/rack-params_key_converter/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
35
+
36
+ ## LiCENSE
37
+
38
+ MIT
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,54 @@
1
+ require "active_support"
2
+ require "json"
3
+
4
+ module Rack
5
+ class ParamsKeyConverter
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ # GET
12
+ env["action_dispatch.request.query_parameters"] = convert_key(:underscore, env["action_dispatch.request.query_parameters"])
13
+ # POST
14
+ env["rack.request.form_hash"] = convert_key(:underscore, env["rack.request.form_hash"])
15
+ # JSON
16
+ env["action_dispatch.request.request_parameters"] = convert_key(:underscore, env["action_dispatch.request.request_parameters"])
17
+
18
+ status, headers, body = @app.call(env)
19
+
20
+ str = body.shift
21
+ h = JSON.parse(str)
22
+ body = [convert_key(:camelize, h).to_json]
23
+
24
+ [status, headers, body]
25
+ end
26
+
27
+ def convert_key(strategy, hash)
28
+ return hash if hash.blank?
29
+ return hash unless [Hash, Array].include?(hash.class)
30
+ converted_hash = {}
31
+
32
+ hash.each do |k, v|
33
+ k = k.to_s
34
+ case v
35
+ when Array
36
+ converted_hash[make_key(k, strategy)] = v.grep(Hash) { |x| convert_key(strategy, x) }
37
+ when Hash
38
+ converted_hash[make_key(k, strategy)] = convert_key(strategy, v)
39
+ else
40
+ converted_hash[make_key(k, strategy)] = v
41
+ end
42
+ end
43
+
44
+ converted_hash
45
+ end
46
+
47
+ def make_key(k, strategy)
48
+ if strategy == :camelize
49
+ return k.send(strategy, :lower)
50
+ end
51
+ k.send(strategy)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rack-params_key_converter"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["sogo.ohta"]
9
+ spec.email = ["sogo.ohta@gmail.com"]
10
+ spec.summary = "Rails app's params converter between JS app and Rails"
11
+ spec.description = 'This is a rack middleware that convert params key between JavaScript app and Rails app.'
12
+ spec.homepage = "https://github.com/s5ot/rack-params_key_converter"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_dependency "rack"
23
+ spec.add_dependency "activesupport"
24
+ spec.add_dependency "json"
25
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-params_key_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sogo.ohta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-18 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This is a rack middleware that convert params key between JavaScript
84
+ app and Rails app.
85
+ email:
86
+ - sogo.ohta@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/rack/params_key_converter.rb
97
+ - rack-params_key_converter.gemspec
98
+ homepage: https://github.com/s5ot/rack-params_key_converter
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.2.2
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Rails app's params converter between JS app and Rails
122
+ test_files: []