method_interceptor 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.
data/.gitignore ADDED
@@ -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/.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 parameter_transformers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Brian Zeligson
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.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # ParameterTransformers
2
+
3
+ This gem provides a proxy which can encapsulate transformers to be
4
+ applied to given method/parameter combinations before the target
5
+ method is called or method response after it is called.
6
+
7
+ It is useful for scenarios where you might have a stateful value that
8
+ needs to be consistently applied to other parameters being passed to
9
+ library code.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'parameter_transformers'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install parameter_transformers
26
+
27
+ ## Usage
28
+
29
+ Here's an example of using the proxy to consistently apply an authentication header to all requests made with the RestClient gem and parse responses as JSON:
30
+
31
+ ```ruby
32
+ require 'rest_client'
33
+ require 'parameter_transformers'
34
+ require 'json'
35
+
36
+ add_auth_header = ->(headers) { (headers || {}).merge('AUTH-HEADER' => 'key') }
37
+ p_transformers = [:get, :post, :put, :delete].map do |method|
38
+ [[method, :headers], add_auth_header]
39
+ end
40
+ r_transformers = [:get, :post, :put, :delete].map do |method|
41
+ [method, JSON.method(:parse)]
42
+ end
43
+ rc = ParameterTransformers::Proxy.new(RestClient, Hash[p_transformers], Hash[r_transformers])
44
+
45
+ # automatically adds {'AUTH-HEADER' => 'key'} to headers on all calls now
46
+ # and automatically parses responses as json
47
+ rc.get('http://somesite.com')
48
+ rc.post('http://somesite.com', {foo: 'bar'})
49
+ rc.put('http://somesite.com', {foo: 'bar'})
50
+ rc.delete('http://somesite.com')
51
+ ```
52
+
53
+ Note per example above the format for transformers is a hash where
54
+ keys are an array of [:method_name, :argument_name] and values
55
+ are procs which receive a copy of the passed argument, and return the
56
+ transformed value to be passed to the method on the target object
57
+
58
+ Meanwhile for the response transformers, the format is a hash where
59
+ keys are the method name, and values are procs which receive the actual
60
+ response
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it ( https://github.com/[my-github-username]/parameter_transformers/fork )
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module MethodInterceptor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ require "method_interceptor/version"
2
+
3
+ module MethodInterceptor
4
+
5
+ class Proxy
6
+
7
+ instance_methods.each do |m|
8
+ undef_method(m) unless m =~ /(^__|^nil\?$|^send$|^object_id$)/
9
+ end
10
+
11
+ def initialize(target, p_transformers={}, r_transformers={})
12
+ @target = target
13
+ @p_transformers = p_transformers
14
+ @r_transformers = r_transformers
15
+ end
16
+
17
+ def respond_to?(symbol, include_priv=false)
18
+ @target.respond_to?(symbol, include_priv)
19
+ end
20
+
21
+ def method_missing(m, *args, &block)
22
+ params = @target.method(m).parameters.map(&:last)
23
+ t_args = args.dup
24
+ params.each_with_index do |param, ix|
25
+ if @p_transformers[[m, param]] &&
26
+ @p_transformers[[m, param]].kind_of?(Proc)
27
+ t_args[ix] =
28
+ @p_transformers[[m, param]].call(t_args[ix])
29
+ end
30
+ end
31
+ r = @target.send(m, *t_args, &block)
32
+ if @r_transformers[m] &&
33
+ @r_transformers[m].kind_of?(Proc)
34
+ @r_transformers[m].call(r)
35
+ else
36
+ r
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'method_interceptor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "method_interceptor"
8
+ spec.version = MethodInterceptor::VERSION
9
+ spec.authors = ["Brian Zeligson"]
10
+ spec.email = ["brian.zeligson@gmail.com"]
11
+ spec.summary = %q{Provides a proxy object that allows consistent transformation of arguments passed to and responses received from target object methods}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe MethodInterceptor::Proxy do
4
+
5
+ class Tester
6
+
7
+ def add_one_to_both(a, b)
8
+ [a+1, b+1]
9
+ end
10
+
11
+ def self.add_one_to_both(a, b)
12
+ [a+1, b+1]
13
+ end
14
+ end
15
+
16
+ it 'takes given parameter/response transformers and applies whenever matched' do
17
+ parameter_transformers = {
18
+ [:add_one_to_both, :a] => ->(x) { x + 3 },
19
+ [:add_one_to_both, :b] => ->(x) { x - 3 }
20
+ }
21
+ tc = MethodInterceptor::Proxy.new(Tester, parameter_transformers)
22
+ r = tc.add_one_to_both(4, 5)
23
+ expect(r.first).to eq(8)
24
+ expect(r.last).to eq(3)
25
+
26
+ t = MethodInterceptor::Proxy.new(Tester.new, parameter_transformers)
27
+ r = t.add_one_to_both(4, 5)
28
+ expect(r.first).to eq(8)
29
+ expect(r.last).to eq(3)
30
+
31
+ response_transformers = {
32
+ add_one_to_both: ->(x) { x.map {|i| i * 3} }
33
+ }
34
+ t = MethodInterceptor::Proxy.new(Tester.new,
35
+ parameter_transformers,
36
+ response_transformers)
37
+ r = t.add_one_to_both(4, 5)
38
+ expect(r.first).to eq(24)
39
+ expect(r.last).to eq(9)
40
+ end
41
+ end
@@ -0,0 +1,92 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ require_relative '../lib/method_interceptor'
21
+ # rspec-expectations config goes here. You can use an alternate
22
+ # assertion/expectation library such as wrong or the stdlib/minitest
23
+ # assertions if you prefer.
24
+ config.expect_with :rspec do |expectations|
25
+ # This option will default to `true` in RSpec 4. It makes the `description`
26
+ # and `failure_message` of custom matchers include text for helper methods
27
+ # defined using `chain`, e.g.:
28
+ # be_bigger_than(2).and_smaller_than(4).description
29
+ # # => "be bigger than 2 and smaller than 4"
30
+ # ...rather than:
31
+ # # => "be bigger than 2"
32
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
33
+ end
34
+
35
+ # rspec-mocks config goes here. You can use an alternate test double
36
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
37
+ config.mock_with :rspec do |mocks|
38
+ # Prevents you from mocking or stubbing a method that does not exist on
39
+ # a real object. This is generally recommended, and will default to
40
+ # `true` in RSpec 4.
41
+ mocks.verify_partial_doubles = true
42
+ end
43
+
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+ =begin
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is
55
+ # recommended. For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ =end
92
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_interceptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Zeligson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-04-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.7'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.7'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '10.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '10.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ''
63
+ email:
64
+ - brian.zeligson@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/method_interceptor.rb
76
+ - lib/method_interceptor/version.rb
77
+ - method_interceptor.gemspec
78
+ - spec/method_interceptor_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Provides a proxy object that allows consistent transformation of arguments
105
+ passed to and responses received from target object methods
106
+ test_files:
107
+ - spec/method_interceptor_spec.rb
108
+ - spec/spec_helper.rb