snaky_camel 0.0.2
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/snaky_camel.rb +5 -0
- data/lib/snaky_camel/exception.rb +17 -0
- data/lib/snaky_camel/middleware.rb +37 -0
- data/lib/snaky_camel/version.rb +3 -0
- data/snaky_camel.gemspec +28 -0
- metadata +117 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 77d8a7e3a539175d2ccc689e219828cc966ba7cd
|
|
4
|
+
data.tar.gz: b88c6e39cd8ce56955a1e030015e99ee25ddf62a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 92edb808282d9915202a7bab3579afe740e2387cb42a6d9e1f2ac64a08414a819bb3bc3341c10ab430f84188143b84a6c87c4344130fb844931671d2fd9542b2
|
|
7
|
+
data.tar.gz: f0d9cd972c214e20e4c74851d20e550771fc8d7ad219292f70329d0f031f14bbb007fb154ab9b58350e20825c350cc5b3ead213b77e574e5024ab567fb4efcd5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Pavel Metelev
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# SnakyCamel
|
|
2
|
+
|
|
3
|
+
SnakyCamel is Rack middleware for Rails that automatically converts request parameter keys to snake_case and response keys to camelCase.
|
|
4
|
+
|
|
5
|
+
This is useful if you Rails app is being used as an API for a JavaScript frontend. In Ruby and Rails, the widely adopted naming convention is to use `snake_case` for variables, keys etc. Popular JavaScript style guides (such as the [Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript)) tend to recommend camelCase. This gem allows you to use snake_case in all Ruby/Rails code and camelCase in JavaScript code without having to worry about converting the case of parameter names or having non-conventional names.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
1. Add this line to your application's Gemfile and execute `bundle install`:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'snaky_camel'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
2. Add this line **as the last config** to `config/application.rb`:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
config.middleware.use "SnakyCamel::Middleware"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Conversion is automatic. All parameter keys in RESTful requests to your Rails application are converted to snake_case. All JSON response keys from your Rails application are converted to camelCase.
|
|
24
|
+
|
|
25
|
+
Example:
|
|
26
|
+
|
|
27
|
+
- Your application receives a request to `yourapp.com/search?searchQuery=cobra`. In the `params` hash in your Rails controller, the param key name will be `search_query`.
|
|
28
|
+
- You render JSON with your controller with this code: `render json: { search_results: ['dromedary'] }, status: :ok`. The actual response is converted to `{ searchResults: ['dromedary'] }`.
|
|
29
|
+
|
|
30
|
+
## Contributing
|
|
31
|
+
|
|
32
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/p4sh4/snaky_camel.
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
37
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "snaky_camel"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/snaky_camel.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module SnakyCamel
|
|
2
|
+
module Exception
|
|
3
|
+
class BadResponseFormat < StandardError
|
|
4
|
+
def initialize(message = nil)
|
|
5
|
+
if message
|
|
6
|
+
@message = "Badly formed JSON response: #{message}"
|
|
7
|
+
else
|
|
8
|
+
@message = "Badly formed JSON response, check its format"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def to_s
|
|
13
|
+
@message
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module SnakyCamel
|
|
2
|
+
require 'snaky_camel/exception'
|
|
3
|
+
|
|
4
|
+
class Middleware
|
|
5
|
+
def initialize(app)
|
|
6
|
+
@app = app
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(env)
|
|
10
|
+
# Transforming request
|
|
11
|
+
request = Rack::Request.new(env)
|
|
12
|
+
|
|
13
|
+
request.GET.deep_transform_keys!{ |k| k.is_a?(String) ? k.underscore : k }
|
|
14
|
+
request.POST.deep_transform_keys!{ |k| k.is_a?(String) ? k.underscore : k }
|
|
15
|
+
|
|
16
|
+
# Transforming response
|
|
17
|
+
@app.call(env).tap do |status, headers, response|
|
|
18
|
+
if headers['Content-Type'] =~ /application\/json/
|
|
19
|
+
response.each do |res|
|
|
20
|
+
begin
|
|
21
|
+
new_response = JSON.parse(res)
|
|
22
|
+
rescue JSON::ParserError => e
|
|
23
|
+
message = e.to_s.gsub(/^\d+:\s/, '') # trim numeric code at the beginning of string
|
|
24
|
+
raise Exception::BadResponseFormat.new(message)
|
|
25
|
+
end
|
|
26
|
+
if new_response.is_a?(Array)
|
|
27
|
+
new_response.map!{ |h| h.deep_transform_keys! { |k| k.camelize(:lower) } }
|
|
28
|
+
else
|
|
29
|
+
new_response.deep_transform_keys! { |k| k.camelize(:lower) }
|
|
30
|
+
end
|
|
31
|
+
res.replace(new_response.to_json)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
data/snaky_camel.gemspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'snaky_camel/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "snaky_camel"
|
|
8
|
+
spec.version = SnakyCamel::VERSION
|
|
9
|
+
spec.authors = ["Pavel Metelev"]
|
|
10
|
+
spec.email = ["pavelmetelev@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Automatically convert request param keys to snake_case and response keys to camelCase for Rails}
|
|
13
|
+
spec.description = %q{Rack middleware for Rails that automatically converts request param keys to snake_case and response keys to camelCase helping you maintain a consistent code style in your Ruby and JavaScript code}
|
|
14
|
+
spec.homepage = "https://github.com/p4sh4/snaky_camel"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
|
19
|
+
end
|
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
21
|
+
spec.require_paths = ["lib"]
|
|
22
|
+
|
|
23
|
+
spec.add_dependency "rails", ">= 4.0"
|
|
24
|
+
|
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: snaky_camel
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Pavel Metelev
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-12-11 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rails
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '4.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '4.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.13'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.13'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '10.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '10.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
description: Rack middleware for Rails that automatically converts request param keys
|
|
70
|
+
to snake_case and response keys to camelCase helping you maintain a consistent code
|
|
71
|
+
style in your Ruby and JavaScript code
|
|
72
|
+
email:
|
|
73
|
+
- pavelmetelev@gmail.com
|
|
74
|
+
executables: []
|
|
75
|
+
extensions: []
|
|
76
|
+
extra_rdoc_files: []
|
|
77
|
+
files:
|
|
78
|
+
- ".gitignore"
|
|
79
|
+
- ".rspec"
|
|
80
|
+
- ".travis.yml"
|
|
81
|
+
- Gemfile
|
|
82
|
+
- LICENSE.txt
|
|
83
|
+
- README.md
|
|
84
|
+
- Rakefile
|
|
85
|
+
- bin/console
|
|
86
|
+
- bin/setup
|
|
87
|
+
- lib/snaky_camel.rb
|
|
88
|
+
- lib/snaky_camel/exception.rb
|
|
89
|
+
- lib/snaky_camel/middleware.rb
|
|
90
|
+
- lib/snaky_camel/version.rb
|
|
91
|
+
- snaky_camel.gemspec
|
|
92
|
+
homepage: https://github.com/p4sh4/snaky_camel
|
|
93
|
+
licenses:
|
|
94
|
+
- MIT
|
|
95
|
+
metadata: {}
|
|
96
|
+
post_install_message:
|
|
97
|
+
rdoc_options: []
|
|
98
|
+
require_paths:
|
|
99
|
+
- lib
|
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
|
+
requirements:
|
|
102
|
+
- - ">="
|
|
103
|
+
- !ruby/object:Gem::Version
|
|
104
|
+
version: '0'
|
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
110
|
+
requirements: []
|
|
111
|
+
rubyforge_project:
|
|
112
|
+
rubygems_version: 2.5.1
|
|
113
|
+
signing_key:
|
|
114
|
+
specification_version: 4
|
|
115
|
+
summary: Automatically convert request param keys to snake_case and response keys
|
|
116
|
+
to camelCase for Rails
|
|
117
|
+
test_files: []
|