query_string 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 34e92e657e8f11169706f1d3e43c13e4f9c85d353aec420e116825fbef34a6de
4
+ data.tar.gz: 86bb197ccd1dfb64ea89e13197ddb76db2b5463b5e56377bcbbb3a1d6e04da99
5
+ SHA512:
6
+ metadata.gz: 9e1614029543337217106fa9ccca8c19287722de469f1c04f508826046147cbc73eb8ca4744b75ab3f4a1e5c559347fb2c391c42ed11906d5e0fd6394bc08ecd
7
+ data.tar.gz: 73195f9516fe105883a2678b3c23cd96ba40b06aae16432e59e0e51d1b77d57e029a33abbce664576f987b9ac9d2af101f2379e81875d0cd432719d7875f9e38
@@ -0,0 +1,76 @@
1
+ version: 2
2
+
3
+ steps: &steps
4
+ steps:
5
+ - checkout
6
+ # Download and cache dependencies
7
+ - restore_cache:
8
+ keys:
9
+ - dependencies-{{ checksum "Gemfile.lock" }}
10
+ # fallback to using the latest cache if no exact match is found
11
+ - dependencies-
12
+ - run:
13
+ name: install dependencies
14
+ command: bundle install --jobs=4 --retry=3 --path vendor/bundle
15
+ - save_cache:
16
+ paths:
17
+ - ./vendor/bundle
18
+ key: dependencies-{{ checksum "Gemfile.lock" }}
19
+
20
+ - run: bundle exec rake run_task_from_env
21
+
22
+ jobs:
23
+ ruby-2.2:
24
+ docker:
25
+ - image: circleci/ruby:2.2
26
+ environment:
27
+ - TASK: spec
28
+ <<: *steps
29
+
30
+ ruby-2.3:
31
+ docker:
32
+ - image: circleci/ruby:2.3
33
+ environment:
34
+ - TASK: spec
35
+ <<: *steps
36
+
37
+ ruby-2.4:
38
+ docker:
39
+ - image: circleci/ruby:2.4
40
+ environment:
41
+ - TASK: spec
42
+ <<: *steps
43
+
44
+ ruby-2.5:
45
+ docker:
46
+ - image: circleci/ruby:2.5
47
+ environment:
48
+ - TASK: spec
49
+ - CODECOV: true
50
+ <<: *steps
51
+
52
+ jruby-9.2:
53
+ docker:
54
+ - image: circleci/jruby:9.2
55
+ environment:
56
+ - TASK: spec
57
+ <<: *steps
58
+
59
+ rubocop:
60
+ docker:
61
+ - image: circleci/ruby:2.5
62
+ environment:
63
+ - TASK: rubocop
64
+ <<: *steps
65
+
66
+
67
+ workflows:
68
+ version: 2
69
+ build:
70
+ jobs:
71
+ - ruby-2.2
72
+ - ruby-2.3
73
+ - ruby-2.4
74
+ - ruby-2.5
75
+ - jruby-9.2
76
+ - rubocop
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ coverage/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.2
3
+
4
+ Layout/MultilineMethodCallIndentation:
5
+ EnforcedStyle: indented
6
+
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - spec/**/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in querystring.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Simen Svale Skogsrud, 2018 Alexander Popov (AlexWayfer)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"),
5
+ to deal in the Software without restriction, including without limitation
6
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ and/or sell copies of the Software, and to permit persons to whom
8
+ the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included
11
+ in all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
+ DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # QueryString
2
+
3
+ [![CircleCI branch](https://img.shields.io/circleci/project/github/AlexWayfer/query_string/master.svg?style=flat-square)](https://circleci.com/gh/AlexWayfer/query_string/tree/master)
4
+ [![Codecov branch](https://img.shields.io/codecov/c/github/AlexWayfer/query_string/master.svg?style=flat-square)](https://codecov.io/gh/AlexWayfer/query_string/branch/master)
5
+ [![license](https://img.shields.io/github/license/AlexWayfer/query_string.svg?style=flat-square)](https://github.com/AlexWayfer/query_string/blob/master/LICENSE)
6
+
7
+ A straight forward gem to build HTTP query strings from Ruby Hashes and Arrays following the query string conventions of Rails and Sinatra. It supports Hashes, Arrays, nested Hashes and Arrays of Hashes and all combinations thereof. E.g.:
8
+
9
+ ```ruby
10
+ QueryString.build(a: 1, b: 'c')
11
+ #=> 'a=1&b=c'
12
+
13
+ QueryString.build(a: { b: 'c', d: 'e' }, f: 'g')
14
+ #=> 'a[b]=c&a[d]=e&f=g'
15
+
16
+ QueryString.build(a: ['foo', 'bar'])
17
+ #=> 'a[]=foo&a[]=bar'
18
+
19
+ QueryString.build(a: [{ b:'c', d:'e' }, { b:'g' }])
20
+ #=> 'a[][b]=c&a[][d]=e&a[][b]=g'
21
+ ```
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+
8
+ require 'rubocop/rake_task'
9
+ RuboCop::RakeTask.new(:rubocop)
10
+
11
+ task :run_task_from_env do
12
+ sh "bundle exec rake #{ENV['TASK']}"
13
+ end
@@ -0,0 +1,3 @@
1
+ module QueryString
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'query_string/version'
2
+
3
+ # Module with class methods for convertation
4
+ module QueryString
5
+ class << self
6
+ def build(value, prefix = nil)
7
+ case value
8
+ when Hash
9
+ build_hash(value, prefix)
10
+ when Array
11
+ value.map { |v| build(v, "#{prefix}[]") }.join('&')
12
+ when nil then prefix.to_s
13
+ else
14
+ raise ArgumentError, 'value must be a Hash' if prefix.nil?
15
+ "#{prefix}=#{escape(value)}"
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def build_hash(hash, prefix)
22
+ hash.map do |k, v|
23
+ build(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
24
+ end.join('&')
25
+ end
26
+
27
+ def escape(value)
28
+ URI.encode_www_form_component(value)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/query_string/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'query_string'
5
+ s.version = QueryString::VERSION
6
+ s.authors = ['Simen Svale Skogsrud', 'Alexander Popov']
7
+ s.email = ['simen@bengler.no', 'alex.wayfer@gmail.com']
8
+ s.homepage = 'https://github.com/AlexWayfer/query_string'
9
+ s.summary = 'Build HTTP query strings from nested Ruby Hashes and Arrays.'
10
+ s.description =
11
+ 'It supports Hashes, Arrays, nested Hashes and Arrays of Hashes ' \
12
+ 'and all combinations thereof.'
13
+
14
+ s.license = 'MIT'
15
+
16
+ s.required_ruby_version = '>= 2.2'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.require_paths = ['lib']
21
+
22
+ # specify any dependencies here; for example:
23
+ s.add_development_dependency 'codecov', '~> 0.1'
24
+ s.add_development_dependency 'rake', '~> 12'
25
+ s.add_development_dependency 'rspec', '~> 3.7'
26
+ s.add_development_dependency 'rubocop', '~> 0.57.2'
27
+ s.add_development_dependency 'simplecov', '~> 0.16'
28
+ # s.add_runtime_dependency 'rest-client'
29
+ end
@@ -0,0 +1,88 @@
1
+ require 'bundler'
2
+ Bundler.require
3
+
4
+ describe QueryString do
5
+ describe '.build' do
6
+ subject { described_class.build(input) }
7
+
8
+ context 'input is empty Hash' do
9
+ let(:input) { {} }
10
+
11
+ it { is_expected.to eq '' }
12
+ end
13
+
14
+ context 'input is nil' do
15
+ let(:input) { nil }
16
+
17
+ it { is_expected.to eq '' }
18
+ end
19
+
20
+ context 'input is String' do
21
+ let(:input) { 'foo=bar' }
22
+
23
+ it do
24
+ expect { subject }.to raise_error(ArgumentError, 'value must be a Hash')
25
+ end
26
+ end
27
+
28
+ context 'input is one-level Hash' do
29
+ let(:input) { { a: 1, b: 'c', d: :e, f: 2.34 } }
30
+
31
+ it { is_expected.to eq 'a=1&b=c&d=e&f=2.34' }
32
+ end
33
+
34
+ context 'input is nested Hashes' do
35
+ let(:input) { { a: { b: 'c', d: 'e' }, f: 'g' } }
36
+
37
+ it { is_expected.to eq 'a[b]=c&a[d]=e&f=g' }
38
+ end
39
+
40
+ context 'input is Hash with Array' do
41
+ let(:input) { { a: %w[foo bar] } }
42
+
43
+ it { is_expected.to eq 'a[]=foo&a[]=bar' }
44
+ end
45
+
46
+ context 'input is Hash with Array of Hashes' do
47
+ let(:input) { { a: [{ b: 'c', d: 'e' }, { b: 'g' }] } }
48
+
49
+ it { is_expected.to eq 'a[][b]=c&a[][d]=e&a[][b]=g' }
50
+ end
51
+
52
+ context 'input is Hash with unencoded keys and values' do
53
+ let(:input) { { 'test field' => { 'another weird' => 'foo/(bar)' } } }
54
+
55
+ it { is_expected.to eq 'test+field[another+weird]=foo%2F%28bar%29' }
56
+ end
57
+
58
+ context 'input is Hash with nil value' do
59
+ let(:input) { { a: nil, b: '', d: nil } }
60
+
61
+ it { is_expected.to eq 'a&b=&d' }
62
+ end
63
+
64
+ context 'input is Hash with nil in Array value' do
65
+ let(:input) { { a: [nil] } }
66
+
67
+ it { is_expected.to eq 'a[]' }
68
+ end
69
+
70
+ context 'input is Hash with empty String in Array value' do
71
+ let(:input) { { a: [''] } }
72
+
73
+ it { is_expected.to eq 'a[]=' }
74
+ end
75
+
76
+ context 'input is Hash with empty Hash value' do
77
+ let(:input) { { a: {} } }
78
+
79
+ it { is_expected.to eq '' }
80
+ end
81
+
82
+ context 'input is Hash with empty Array value' do
83
+ let(:input) { { a: [] } }
84
+
85
+ it { is_expected.to eq '' }
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,10 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter '/spec/'
4
+ end
5
+ SimpleCov.start
6
+
7
+ if ENV['CODECOV']
8
+ require 'codecov'
9
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
10
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_string
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Simen Svale Skogsrud
8
+ - Alexander Popov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-07-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: codecov
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '0.1'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '0.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '12'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '12'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.7'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.7'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.57.2
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.57.2
70
+ - !ruby/object:Gem::Dependency
71
+ name: simplecov
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.16'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.16'
84
+ description: It supports Hashes, Arrays, nested Hashes and Arrays of Hashes and all
85
+ combinations thereof.
86
+ email:
87
+ - simen@bengler.no
88
+ - alex.wayfer@gmail.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".circleci/config.yml"
94
+ - ".editorconfig"
95
+ - ".gitignore"
96
+ - ".rspec"
97
+ - ".rubocop.yml"
98
+ - Gemfile
99
+ - LICENSE
100
+ - README.md
101
+ - Rakefile
102
+ - lib/query_string.rb
103
+ - lib/query_string/version.rb
104
+ - query_string.gemspec
105
+ - spec/query_string_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/AlexWayfer/query_string
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '2.2'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.7.6
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Build HTTP query strings from nested Ruby Hashes and Arrays.
131
+ test_files:
132
+ - spec/query_string_spec.rb
133
+ - spec/spec_helper.rb