rack-adequate-json 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c972e073214d7cae4033a81d25e83628c67164c
4
+ data.tar.gz: c619d6b43799da62ce89ce25f5974ec3782c98a6
5
+ SHA512:
6
+ metadata.gz: c91f1b16f176020919606b7358fb61981ddc6e95cfabe15532b0289619d7ed5fa6834324ce8af8a601cf45126d1d7e263a40bfba8b9a6d6f942fc1a5f8fcdf33
7
+ data.tar.gz: 30f17ba7860274793f22f35035d51d4dbce9c8d71ab4e4b11aba1a73e43052889d429b5ee892677d41b9f38c6a8dc0d2a4e1cfad568b4c0cd23e4ded07717672
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-adequatejson.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ashod Ayanyan
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,58 @@
1
+ # Rack Adequate Json
2
+
3
+ Filters JSON response given attribute names to reduce payload size
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-adequate-json'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-adequate-json
18
+
19
+ ## Configure
20
+
21
+ ### Rails
22
+
23
+ ``` ruby
24
+ #config/application.rb
25
+ module AppName
26
+ class Application < Rails::Application
27
+ # Middleware options
28
+ # root: the root key for the json payload , default: nil
29
+ # target_param: query param of filter fields , default: 'fields'
30
+ config.middleware.use 'Rack::AdequateJson' , { root: 'data' }
31
+ end
32
+ end
33
+ ```
34
+
35
+ ### Sinatra
36
+
37
+ ``` ruby
38
+ require 'rack/adequate_json'
39
+
40
+ class AppName < Sinatra::Base
41
+ configure do
42
+ # Middleware options
43
+ # root: the root key for the json payload , default: nil
44
+ # target_param: query param of filter fields , default: 'fields'
45
+ use Rack::AdequateJson , { root: 'data' , target_param: 'select' }
46
+ end
47
+ end
48
+
49
+ ```
50
+
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( http://github.com/<my-github-username>/rack-adequate-json/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require_relative './adequate_json/version'
2
+ require_relative './adequate_json/adequate_json.rb'
@@ -0,0 +1,75 @@
1
+ module Rack
2
+ class AdequateJson
3
+ attr_reader :app, :root, :target_param, :status, :headers
4
+
5
+ def initialize(app , options={})
6
+ @app = app
7
+ @root = options.fetch(:root, nil)
8
+ @target_param = options.fetch(:target_param, "fields")
9
+ end
10
+
11
+ def call(env)
12
+ call_and_setup(env)
13
+ if content_type_json? && filter_fields
14
+ [ status, headers, response_stream { |b| filter_json_body(b) } ]
15
+ else
16
+ [status, headers, response_stream]
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def call_and_setup(env)
23
+ @status, @headers, @response = app.call(env)
24
+ @request = request(env)
25
+ @filter_fields = nil
26
+ end
27
+
28
+ def filter_json_body(body)
29
+ json_body = JSON.parse(body)
30
+ data = root ? json_body[root] : json_body
31
+
32
+ if data.is_a?(Hash)
33
+ slice_data!(data, filter_fields)
34
+ elsif data.is_a?(Array)
35
+ data.each{ |data| slice_data!(data, filter_fields) }
36
+ end
37
+
38
+ json_body.to_json
39
+ end
40
+
41
+ def filter_fields
42
+ if params[target_param] && !params[target_param].strip.empty?
43
+ @filter_fields ||= params[target_param].split(',').map(&:strip)
44
+ else
45
+ nil
46
+ end
47
+ end
48
+
49
+ def response_stream(&block)
50
+ body = []
51
+ @response.each do |body_part|
52
+ body << ( block ? block.call(body_part) : body_part)
53
+ end
54
+ body
55
+ end
56
+
57
+ def slice_data!(data, fields)
58
+ data.select!{|k,v| fields.include?(k) } if fields
59
+ end
60
+
61
+ def request(env)
62
+ Rack::Request.new(env)
63
+ end
64
+
65
+ def params
66
+ @request.params
67
+ end
68
+
69
+ def content_type_json?
70
+ @headers["Content-Type"].include?("json")
71
+ end
72
+
73
+
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class AdequateJson
3
+ VERSION = "0.1.2"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/adequate_json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-adequate-json"
8
+ spec.version = Rack::AdequateJson::VERSION
9
+ spec.authors = ["Ashod Ayanyan"]
10
+ spec.email = ["aayanyan@gmail.com"]
11
+ spec.summary = %q{Rack Middleware to reduce size of json payload}
12
+ spec.description = %q{Rack Middleware to reduce size of json payload - Allows clients consuming json apis to select attributes within payload}
13
+ spec.homepage = "https://github.com/ashoda/rack-adequate-json"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake" , "~> 10.4"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ spec.add_development_dependency "rack-test", "~> 0.6"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ end
@@ -0,0 +1,55 @@
1
+ require 'json'
2
+ require 'rack/test'
3
+
4
+ describe 'Rack::AdequateJson' do
5
+ include Rack::Test::Methods
6
+ let(:app) { ->(env) { [200,{"Content-Type" => "application/json" }, [original_json] ] } }
7
+ let(:stack) { Rack::AdequateJson.new(app, root: root_key ) }
8
+ let(:request) { Rack::MockRequest.new(stack) }
9
+
10
+ RSpec.shared_examples "json field filtering middleware" do
11
+ context "with filter fields provide" do
12
+ it { expect(response.body).to eq(filtered_json) }
13
+ end
14
+
15
+ context "with no filter fields provide" do
16
+ let(:query_params) { nil }
17
+ it { expect(response.body).to eq(original_json) }
18
+ end
19
+ end
20
+
21
+ describe "request passing through middleware" do
22
+ let(:url) { '/' }
23
+ let(:query_params) { '?fields=a,b' }
24
+ let(:response) { request.get("#{url}#{query_params}") }
25
+
26
+ context "given root element" do
27
+ let(:root_key) { "data" }
28
+ context "collection" do
29
+ let(:original_json) { {data:[{a:1, b:'test1', c:'rest2'},{a:2, b:'test2', c:'rest2'}]}.to_json }
30
+ let(:filtered_json) { {data:[{a:1, b:'test1'},{a:2, b:'test2'}]}.to_json }
31
+ it_behaves_like "json field filtering middleware"
32
+ end
33
+ context "object" do
34
+ let(:original_json) { {data:{a:1, b:'test1', c:'rest2'}}.to_json }
35
+ let(:filtered_json) { {data:{a:1, b:'test1'}}.to_json }
36
+ it_behaves_like "json field filtering middleware"
37
+ end
38
+ end
39
+
40
+ context "given no root element" do
41
+ let(:root_key) { nil }
42
+ context "given collection" do
43
+ let(:original_json) { [{a:1, b:'test1', c:'rest2'},{a:2, b:'test2', c:'rest2'}].to_json }
44
+ let(:filtered_json) { [{a:1, b:'test1'},{a:2, b:'test2'}].to_json }
45
+ it_behaves_like "json field filtering middleware"
46
+ end
47
+ context "given object" do
48
+ let(:original_json) { {a:1, b:'test1', c:'rest2'}.to_json }
49
+ let(:filtered_json) { {a:1, b:'test1'}.to_json }
50
+ it_behaves_like "json field filtering middleware"
51
+ end
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,97 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.setup
4
+ Bundler.require(:default, :development)
5
+
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
9
+ # file to always be loaded, without a need to explicitly require it in any files.
10
+ #
11
+ # Given that it is always loaded, you are encouraged to keep this file as
12
+ # light-weight as possible. Requiring heavyweight dependencies from this file
13
+ # will add to the boot time of your test suite on EVERY test run, even for an
14
+ # individual file that may not need all of that loaded. Instead, consider making
15
+ # a separate helper file that requires the additional dependencies and performs
16
+ # the additional setup, and require it from the spec files that actually need it.
17
+ #
18
+ # The `.rspec` file also contains a few flags that are not defaults but that
19
+ # users commonly want.
20
+
21
+ require 'rack/adequate_json'
22
+
23
+ #
24
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
25
+ RSpec.configure do |config|
26
+ # rspec-expectations config goes here. You can use an alternate
27
+ # assertion/expectation library such as wrong or the stdlib/minitest
28
+ # assertions if you prefer.
29
+ config.expect_with :rspec do |expectations|
30
+ # This option will default to `true` in RSpec 4. It makes the `description`
31
+ # and `failure_message` of custom matchers include text for helper methods
32
+ # defined using `chain`, e.g.:
33
+ # be_bigger_than(2).and_smaller_than(4).description
34
+ # # => "be bigger than 2 and smaller than 4"
35
+ # ...rather than:
36
+ # # => "be bigger than 2"
37
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
38
+ end
39
+
40
+ # rspec-mocks config goes here. You can use an alternate test double
41
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
42
+ config.mock_with :rspec do |mocks|
43
+ # Prevents you from mocking or stubbing a method that does not exist on
44
+ # a real object. This is generally recommended, and will default to
45
+ # `true` in RSpec 4.
46
+ mocks.verify_partial_doubles = true
47
+ end
48
+
49
+ # The settings below are suggested to provide a good initial experience
50
+ # with RSpec, but feel free to customize to your heart's content.
51
+ =begin
52
+ # These two settings work together to allow you to limit a spec run
53
+ # to individual examples or groups you care about by tagging them with
54
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
55
+ # get run.
56
+ config.filter_run :focus
57
+ config.run_all_when_everything_filtered = true
58
+
59
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
60
+ # For more details, see:
61
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
62
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
63
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
64
+ config.disable_monkey_patching!
65
+
66
+ # This setting enables warnings. It's recommended, but in some cases may
67
+ # be too noisy due to issues in dependencies.
68
+ config.warnings = true
69
+
70
+ # Many RSpec users commonly either run the entire suite or an individual
71
+ # file, and it's useful to allow more verbose output when running an
72
+ # individual spec file.
73
+ if config.files_to_run.one?
74
+ # Use the documentation formatter for detailed output,
75
+ # unless a formatter has already been configured
76
+ # (e.g. via a command-line flag).
77
+ config.default_formatter = 'doc'
78
+ end
79
+
80
+ # Print the 10 slowest examples and example groups at the
81
+ # end of the spec run, to help surface which specs are running
82
+ # particularly slow.
83
+ config.profile_examples = 10
84
+
85
+ # Run specs in random order to surface order dependencies. If you find an
86
+ # order dependency and want to debug it, you can fix the order by providing
87
+ # the seed, which is printed after each run.
88
+ # --seed 1234
89
+ config.order = :random
90
+
91
+ # Seed global randomization in this process using the `--seed` CLI option.
92
+ # Setting this allows you to use `--seed` to deterministically reproduce
93
+ # test failures related to randomization by passing the same `--seed` value
94
+ # as the one that triggered the failure.
95
+ Kernel.srand config.seed
96
+ =end
97
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-adequate-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Ashod Ayanyan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-02 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ description: Rack Middleware to reduce size of json payload - Allows clients consuming
84
+ json apis to select attributes within payload
85
+ email:
86
+ - aayanyan@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/rack/adequate_json.rb
98
+ - lib/rack/adequate_json/adequate_json.rb
99
+ - lib/rack/adequate_json/version.rb
100
+ - rack-adequate-json.gemspec
101
+ - spec/adequate_json_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/ashoda/rack-adequate-json
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Rack Middleware to reduce size of json payload
127
+ test_files:
128
+ - spec/adequate_json_spec.rb
129
+ - spec/spec_helper.rb
130
+ has_rdoc: