olive_branch 2.1.4 → 4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e39a8f70f61df011f30f51af0d4c57819ec0d905e59d90c59911915c5709e47
4
- data.tar.gz: f635a399ec1e1406e298f2787241b25b2878bdd2d8460f2fb3368d6fe069fb3c
3
+ metadata.gz: 6385aaf40477e1da61f791e86f1a97c5e2207bb039384f624fcc3a02b3850a01
4
+ data.tar.gz: 05b1cc138df81ea951f34e788d2df7ba9c0e046db0311e7d3ec0dccde0cd8bb7
5
5
  SHA512:
6
- metadata.gz: 20b145eb4334cff4f2b2a1d009b5e353c9262bfcd944eef6f956f069163e95bdf4ac9fa49891f39011f4c94b4ff67b4cfa74dfb9f51e42dde26e27cc8741f686
7
- data.tar.gz: 33ddfa6fd29b2701a5505f7128454bd0dc553e76955db2be9adcd0852cae06f6709e285a5caf4d305c8609d8bec110dd3620d5edaa8a2429dbf7328da36dcfbc
6
+ metadata.gz: 2e7f4d9ca0335cf4b1eaf98e8bcd7fe2d7c3c5651b52f65570fbacb6e2a7c497186d083feca611af4788e13783c80e315fb387136bf1131f7979ef5405cbda23
7
+ data.tar.gz: 6d958d754397f57209001e2ea3cec65e830f904e8e2ebc203dd97fef3b3469d71fa1791c591158b561d481a5bc59c0525ba8191153752f02118ffa87e2f7d5ef
data/README.md CHANGED
@@ -13,21 +13,34 @@ This gem lets your API users pass in and receive camelCased or dash-cased keys,
13
13
  gem "olive_branch"
14
14
  ```
15
15
 
16
- 2. Add this to `config/applcation.rb`:
16
+ 2. Add this to `config/applcation.rb` if you want the clients to control the transformation behaviour through the `Key-Inflection` HTTP header sent by the client:
17
17
 
18
18
  ```ruby
19
19
  config.middleware.use OliveBranch::Middleware
20
20
  ```
21
21
 
22
+ Alternative, if you want to always convert between snake_case and camelCase for your API and only your API, to keep Rubyist and JavaScript developer's happy, use the following configuration:
23
+
24
+ ```ruby
25
+ excluded_routes = ->(env) { !env["PATH_INFO"].match(%r{^/api}) }
26
+ config.middleware.use OliveBranch::Middleware,
27
+ inflection: "camel",
28
+ exclude_params: excluded_routes,
29
+ exclude_response: excluded_routes
30
+ ```
31
+
32
+ in your `config/application.rb`.
33
+
22
34
  ## Use
23
35
 
24
- Include a `X-Key-Inflection` header with values of `camel`, `dash`, or `snake` in your JSON API requests.
36
+ Include a `Key-Inflection` header with values of `camel`, `dash`, `snake` or `pascal` in your JSON API requests.
25
37
 
26
38
  For more examples, see [our blog post](https://www.viget.com/articles/introducing-olivebranch).
27
39
 
28
40
  ## Optimizations and configuration
29
41
 
30
- `OliveBranch` uses `multi_json`, which will choose the fastest available JSON parsing library and use that. Combined with `Oj` can speed things up and save ~20% rails response time.
42
+ `OliveBranch` uses `multi_json`, which will automatically choose the fastest available JSON parsing library present in your application.
43
+ Most Ruby applications default to using the JSON library that ships with Ruby. However, by including a coder that `multi_json` considers faster, like [Oj](https://github.com/ohler55/oj) in your gemfile, you can potentially save up to ~20% response time.
31
44
 
32
45
  The middleware can be initialized with custom camelize/dasherize implementations, so if you know you have a fixed size set of keys, you can save a considerable amount of time by providing a custom camelize that caches like so:
33
46
 
@@ -48,7 +61,13 @@ end
48
61
  config.middleware.use OliveBranch::Middleware, camelize: FastCamel.method(:camelize)
49
62
  ```
50
63
 
51
- A default inflection can be specified so you don't have to include the `X-Key-Inflection` header on every request.
64
+ Default inflection header key can be changed like
65
+
66
+ ```ruby
67
+ config.middleware.use OliveBranch::Middleware, inflection_header: 'Inflect-With'
68
+ ```
69
+
70
+ A default inflection can be specified so you don't have to include the `Key-Inflection` header on every request. If you opt for default inflection, you may want to exclude the routes that Rails uses (see Filtering).
52
71
 
53
72
  ```ruby
54
73
  config.middleware.use OliveBranch::Middleware, inflection: 'camel'
@@ -88,9 +107,25 @@ config.middleware.use OliveBranch::Middleware, exclude_response: -> (env) {
88
107
  }
89
108
  ```
90
109
 
110
+ #### Rails routes & Action Text
111
+
112
+ If you're using default inflection, exclude the routes that Rails uses
113
+ ```ruby
114
+ rails_routes = -> (env) { env['PATH_INFO'].match(/^\/rails/) }
115
+ config.middleware.use OliveBranch::Middleware, inflection: "camel", exclude_params: rails_routes, exclude_response: rails_routes
116
+ ```
117
+
118
+ ## Upgrading to version 3
119
+
120
+ Default inflection header changed from `X-Key-Inflection` to `Key-Inflection`.
121
+
91
122
  ## Troubleshooting
92
123
 
93
- We've seen a few folks raise issues that inbound transformations are not taking place. This is often due to the fact that OliveBranch, by default, is only transforming keys when a request's Content-Type is `application/json`. If you would like to force inbound transformation on every request, you must define an override for the `content_type_check` functionality:
124
+ We've seen folks raise issues that inbound transformations are not taking place. This is often due to the fact that OliveBranch, by default, is only transforming keys when a request's Content-Type is `application/json`.
125
+
126
+ Note that your HTTP client library may suppress even a manually specified `Content-Type` header if the request body is empty (e.g. [Axios does this](https://github.com/axios/axios/issues/86)). This is a common gotcha for GET requests, the body of which are [often expected to be empty](https://stackoverflow.com/questions/978061/http-get-with-request-body) for reasons of caching. If you're seeing the middleware perform on POST or PATCH requests, but not GET requests, this may be your issue.
127
+
128
+ You may choose to force inbound transformation on every request by overriding the `content_type_check` functionality:
94
129
 
95
130
  ```ruby
96
131
  config.middleware.use OliveBranch::Middleware, content_type_check: -> (content_type) { true }
@@ -22,6 +22,10 @@ module OliveBranch
22
22
  end
23
23
  end
24
24
 
25
+ def pascalize(string)
26
+ string.underscore.camelize(:upper)
27
+ end
28
+
25
29
  def camelize(string)
26
30
  string.underscore.camelize(:lower)
27
31
  end
@@ -46,10 +50,13 @@ module OliveBranch
46
50
  @app = app
47
51
  @camelize = args[:camelize] || Transformations.method(:camelize)
48
52
  @dasherize = args[:dasherize] || Transformations.method(:dasherize)
53
+ @pascalize = args[:pascalize] || Transformations.method(:pascalize)
49
54
  @content_type_check = args[:content_type_check] || Checks.method(:content_type_check)
50
55
  @exclude_response = args[:exclude_response] || Checks.method(:default_exclude)
51
56
  @exclude_params = args[:exclude_params] || Checks.method(:default_exclude)
52
57
  @default_inflection = args[:inflection]
58
+ @inflection_header = args.fetch(:inflection_header, 'Key-Inflection').gsub(/[^a-z0-9]/i, '_').upcase
59
+ @inflection_header = "HTTP_#{@inflection_header}" unless @inflection_header.start_with?('HTTP_')
53
60
  end
54
61
 
55
62
  def call(env)
@@ -83,19 +90,24 @@ module OliveBranch
83
90
  end
84
91
 
85
92
  def exclude_response?(env, headers)
86
- exclude?(env, headers["Content-Type"], @exclude_response)
93
+ exclude_rails_route?(env) ||
94
+ exclude?(env, headers['Content-Type'], @exclude_response)
87
95
  end
88
96
 
89
97
  def exclude?(env, content_type, block)
90
98
  !inflection_type(env) || !valid_content_type?(content_type) || block.call(env)
91
99
  end
92
100
 
101
+ def exclude_rails_route?(env)
102
+ env['PATH_INFO'].to_s.start_with?('/rails')
103
+ end
104
+
93
105
  def valid_content_type?(content_type)
94
106
  @content_type_check.call(content_type)
95
107
  end
96
108
 
97
109
  def inflection_type(env)
98
- env["HTTP_X_KEY_INFLECTION"] || @default_inflection
110
+ env[@inflection_header] || @default_inflection
99
111
  end
100
112
 
101
113
  def inflection_method(env)
@@ -105,6 +117,8 @@ module OliveBranch
105
117
  @camelize
106
118
  elsif inflection == "dash"
107
119
  @dasherize
120
+ elsif inflection == 'pascal'
121
+ @pascalize
108
122
  else
109
123
  # probably misconfigured, do nothing
110
124
  -> (string) { string }
@@ -1,3 +1,3 @@
1
1
  module OliveBranch
2
- VERSION = '2.1.4'
2
+ VERSION = '4.0.1'
3
3
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: olive_branch
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.4
4
+ version: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Fatsi
8
8
  - David Eisinger
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-11-08 00:00:00.000000000 Z
12
+ date: 2021-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -39,20 +39,6 @@ dependencies:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: oj
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- version: '0'
49
- type: :runtime
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: '0'
56
42
  - !ruby/object:Gem::Dependency
57
43
  name: rspec
58
44
  requirement: !ruby/object:Gem::Requirement
@@ -95,20 +81,6 @@ dependencies:
95
81
  - - ">="
96
82
  - !ruby/object:Gem::Version
97
83
  version: '0'
98
- - !ruby/object:Gem::Dependency
99
- name: sqlite3
100
- requirement: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- version: '0'
105
- type: :development
106
- prerelease: false
107
- version_requirements: !ruby/object:Gem::Requirement
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- version: '0'
112
84
  description: Handle camel/snake/dash case conversion
113
85
  email:
114
86
  - eli.fatsi@viget.com
@@ -126,7 +98,7 @@ homepage: https://github.com/vigetlabs/olive_branch
126
98
  licenses:
127
99
  - MIT
128
100
  metadata: {}
129
- post_install_message:
101
+ post_install_message:
130
102
  rdoc_options: []
131
103
  require_paths:
132
104
  - lib
@@ -141,9 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
113
  - !ruby/object:Gem::Version
142
114
  version: '0'
143
115
  requirements: []
144
- rubyforge_project:
145
- rubygems_version: 2.7.6
146
- signing_key:
116
+ rubygems_version: 3.0.1
117
+ signing_key:
147
118
  specification_version: 4
148
119
  summary: Handle camel/snake/dash case conversion
149
120
  test_files: []