simphi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 51fece8cca558b26e70d71193bea06f978981613
4
+ data.tar.gz: b194958b230da5ccb8811920b6134f6d2880e92a
5
+ SHA512:
6
+ metadata.gz: de7d714d087b807500c80633eb9abafc12855422c5923065542c8ef458add3a009930d268a1581406f02727ef3ede11581047d903ca3249dda3add2f0ee715cc
7
+ data.tar.gz: f391d84c914223e383a403fdb61d1f155d38e152e2fca512ed51d4e817b8da17573e1006a7755278faee6bc14b121060614b7554710c0f246a65b71a36dc7e0e
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 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simphi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Alex Beznos
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,31 @@
1
+ # Simphi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simphi'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simphi
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/simphi/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
4
+
5
+ task default: :spec
data/lib/simphi.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "simphi/version"
2
+
3
+ module Simphi
4
+ end
@@ -0,0 +1,17 @@
1
+ require 'simphi/request'
2
+
3
+ module Simphi
4
+ class Middleware
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ request = Simphi::Request.new(env)
11
+ request.normalize_hash_params
12
+
13
+ status, headers, response = @app.call(request.env)
14
+ [status, headers, response]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,60 @@
1
+ require 'rack/request'
2
+
3
+ module Simphi
4
+ class Request < Rack::Request
5
+ def normalize_hash_params
6
+ params.each do |key, value|
7
+ normalize_to_proper_params(value) if with_hash? value
8
+
9
+ if with_hash? key
10
+ param = delete_param(key)
11
+ update_param(key.gsub(/-shi_hash/, ''), normalized_hash(param))
12
+ end
13
+
14
+
15
+ end if with_hash? params
16
+ end
17
+
18
+ private
19
+
20
+ def with_hash?(param)
21
+ return param =~ /-shi_hash/ if param.is_a? String
22
+ return param.to_s =~ /-shi_hash/ if param.is_a? Hash
23
+ end
24
+
25
+ def normalized_hash(params)
26
+ hashes = params.map do |_, v|
27
+ if v.length <= 1 || !v.has_key?('key') || !v.has_key?('value')
28
+ raise ArgumentError, 'Every hash element should include key and value'
29
+ end
30
+
31
+ { v['key'] => v['value'] } if v['key'].present? && v['value'].present?
32
+ end.compact
33
+
34
+ hashes.reduce Hash.new, :merge
35
+ end
36
+
37
+ def normalize_to_proper_params(hash)
38
+ if hash.is_a? Hash
39
+
40
+ list = hash.map do |k, v|
41
+ normalize_to_proper_params(v) if with_hash? v
42
+
43
+ if with_hash? k
44
+ {
45
+ key: k,
46
+ obj: { k.gsub(/-shi_hash/, '') => normalized_hash(v) }
47
+ }
48
+ end
49
+
50
+ end.compact
51
+
52
+ list.each do |v|
53
+ hash.delete(v[:key])
54
+ hash.merge!(v[:obj])
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+ end
data/simphi.gemspec ADDED
@@ -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 = "simphi"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Alex Beznos"]
9
+ spec.email = ["beznosa@yahoo.com"]
10
+ spec.summary = %q{Ability to deale with hash like inputs}
11
+ spec.description = %q{The gem will be suetable if you want to make hash from input}
12
+ spec.homepage = "https://github.com/AlexBeznos/simphi"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "simple_form", "> 2.0"
20
+ spec.add_dependency "rack", "~> 1.6.4"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,28 @@
1
+ require "simphi/middleware"
2
+ require "spec_helper"
3
+
4
+ describe Simphi::Middleware do
5
+ let(:env) { env_for('http://any-fake-domain.com') }
6
+ let(:app) { -> (env) { [200, env, "app"] } }
7
+ let!(:middleware) do
8
+ Simphi::Middleware.new(app)
9
+ end
10
+
11
+ context 'uses simphi/request by' do
12
+ after do
13
+ middleware.call(env)
14
+ end
15
+
16
+ it 'instantiate Simphi::Request' do
17
+ expect(Simphi::Request).to receive(:new).and_return(Simphi::Request.new(env))
18
+ end
19
+
20
+ it '#normalize_hash_params call' do
21
+ expect_any_instance_of(Simphi::Request).to receive(:normalize_hash_params)
22
+ end
23
+ end
24
+
25
+ def env_for url, opts={}
26
+ Rack::MockRequest.env_for(url, opts)
27
+ end
28
+ end
@@ -0,0 +1,118 @@
1
+ require "simphi/request"
2
+ require "spec_helper"
3
+
4
+ describe Simphi::Request do
5
+ context "#normalize_hash_params" do
6
+ let(:request) { Simphi::Request.new(double) }
7
+
8
+ before do
9
+ allow_any_instance_of(Simphi::Request).to receive(:POST).and_return(params_before)
10
+ allow_any_instance_of(Simphi::Request).to receive(:GET).and_return({})
11
+ end
12
+
13
+ context "when params not include hash" do
14
+ let(:params_before) do
15
+ {
16
+ "five" => "five",
17
+ "one" => {
18
+ "two" => "two",
19
+ "three" => "three"
20
+ },
21
+ "four" => "four"
22
+ }
23
+ end
24
+
25
+ it "not change the hash" do
26
+ request.normalize_hash_params
27
+
28
+ expect(request.params).to eq params_before
29
+ end
30
+ end
31
+
32
+ context "when params include well formed hashes" do
33
+ let(:params_before) do
34
+ {
35
+ "one" => "one",
36
+ "two" => {
37
+ "more" => {
38
+ "first-shi_hash" => {
39
+ "first-pair" => {
40
+ "key" => "first-key",
41
+ "value" => "first-value"
42
+ },
43
+ "second" => {
44
+ "key" => "second-key",
45
+ "value" => "second-value"
46
+ }
47
+ }
48
+ }
49
+ },
50
+ "three-shi_hash" => {
51
+ "first-pair" => {
52
+ "key" => "first-key",
53
+ "value" => "first-value"
54
+ }
55
+ }
56
+ }
57
+ end
58
+
59
+ let(:params_after) do
60
+ {
61
+ "one" => "one",
62
+ "two" => {
63
+ "more" => {
64
+ "first" => {
65
+ "first-key" => "first-value",
66
+ "second-key" => "second-value"
67
+ }
68
+ }
69
+ },
70
+ "three" => {
71
+ "first-key" => "first-value"
72
+ }
73
+ }
74
+ end
75
+
76
+ it "change the hash" do
77
+ request.normalize_hash_params
78
+
79
+ expect(request.params).to eq params_after
80
+ end
81
+ end
82
+
83
+ context "when include not formed properly hash param because of" do
84
+ context "include only key" do
85
+ let(:params_before) do
86
+ {
87
+ "first-shi_hash" => {
88
+ "first-pair" => {
89
+ "key" => "first-key"
90
+ }
91
+ }
92
+ }
93
+ end
94
+
95
+ it "should raise exception" do
96
+ expect { request.normalize_hash_params }.to raise_error(ArgumentError)
97
+ end
98
+ end
99
+
100
+ context "include key and value but with incorect names" do
101
+ let(:params_before) do
102
+ {
103
+ "first-shi_hash" => {
104
+ "first-pair" => {
105
+ "k" => "first-key",
106
+ "value" => "first-value"
107
+ }
108
+ }
109
+ }
110
+ end
111
+
112
+ it "should raise exception" do
113
+ expect { request.normalize_hash_params }.to raise_error(ArgumentError)
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,9 @@
1
+ require "simphi/middleware"
2
+ require "rack/test"
3
+ require "active_support"
4
+ require "active_support/core_ext"
5
+ # Dir["spec/**/*.rb"].each { |f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.order = "random"
9
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simphi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Beznos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simple_form
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: The gem will be suetable if you want to make hash from input
84
+ email:
85
+ - beznosa@yahoo.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/simphi.rb
97
+ - lib/simphi/middleware.rb
98
+ - lib/simphi/request.rb
99
+ - simphi.gemspec
100
+ - spec/middleware_spec.rb
101
+ - spec/request_spec.rb
102
+ - spec/spec_helper.rb
103
+ - tasks/rspec.rake
104
+ homepage: https://github.com/AlexBeznos/simphi
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Ability to deale with hash like inputs
128
+ test_files:
129
+ - spec/middleware_spec.rb
130
+ - spec/request_spec.rb
131
+ - spec/spec_helper.rb