search_query 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/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ == search_query
2
+
3
+ Put appropriate LICENSE for your project here.
@@ -0,0 +1,33 @@
1
+ = Search Query
2
+
3
+ A query string parser.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
10
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
11
+ * Send me a pull request. Bonus points for topic branches.
12
+
13
+ == Install
14
+
15
+ $ gem install search_query
16
+
17
+ == How to use
18
+
19
+ From string to hash
20
+
21
+ SearchQuery::Query.new("param = 10") => {:param => 10}
22
+
23
+ SearchQuery::Query.new("param=9&&param=10&&param=11") => {:param => [9,10,11]}
24
+
25
+ From hash to String
26
+
27
+ SearchQuery::Query.new({:param => "test"} => "param=test"
28
+
29
+ SearchQuery::Query.new({:param_1 => "test", :param_2 => 10} => "param_1=test&&param_2=10"
30
+
31
+ == Copyright
32
+
33
+ See LICENSE for details.
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+
3
+ require 'rake'
4
+ require 'rake/rdoctask'
5
+
6
+ require "rspec"
7
+ require "rspec/core/rake_task"
8
+
9
+ require File.expand_path('../lib/search_query/version', __FILE__)
10
+
11
+ Rspec::Core::RakeTask.new(:spec) do |spec|
12
+ spec.pattern = "spec/**/*_spec.rb"
13
+ end
14
+
15
+ desc 'Builds the gem'
16
+ task :build do
17
+ sh "gem build search_query.gemspec"
18
+ end
19
+
20
+ desc 'Tags version, pushes to remote, and pushes gem'
21
+ task :release => [:spec, :build] do
22
+ # sh "git tag v#{SearchQuery::Version}"
23
+ # sh "git push origin master"
24
+ # sh "git push origin v#{SearchQuery::Version}"
25
+ sh "gem push search_query-#{SearchQuery::Version}.gem"
26
+ end
@@ -0,0 +1,66 @@
1
+ module SearchQuery
2
+ class Query
3
+ def initialize(query)
4
+ if query.kind_of?(String)
5
+ generate_hsh(query)
6
+ generate_str(@hsh_query)
7
+ else
8
+ generate_str(query)
9
+ generate_hsh(@str_query)
10
+ end
11
+ end
12
+
13
+ def to_hsh
14
+ return @hsh_query
15
+ end
16
+
17
+ def to_s
18
+ return @str_query
19
+ end
20
+
21
+ private
22
+
23
+ def generate_str(query)
24
+ @str_query = String.new
25
+ query.each do |key, value|
26
+ if value.kind_of?(Array)
27
+ value.each do |v|
28
+ if !@str_query || @str_query.empty?
29
+ @str_query << "#{key}=#{v}"
30
+ else
31
+ @str_query << "&&#{key}=#{v}"
32
+ end
33
+ end
34
+ else
35
+ if !@str_query || @str_query.empty?
36
+ @str_query << "#{key}=#{value}"
37
+ else
38
+ @str_query << "&&#{key}=#{value}"
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def generate_hsh(query)
45
+ @hsh_query = Hash.new
46
+ query.gsub!(' ', '')
47
+ query.scan(/(\w+)=([0-9a-zA-Z]+)/).each do |key, value|
48
+ if @hsh_query.include?(key.to_sym) && @hsh_query[key.to_sym].kind_of?(Array)
49
+ @hsh_query[key.to_sym].push(parse(value))
50
+ elsif @hsh_query.include?(key.to_sym) && !@hsh_query[key.to_sym].kind_of?(Array)
51
+ @hsh_query[key.to_sym] = [@hsh_query[key.to_sym], parse(value)]
52
+ else
53
+ @hsh_query[key.to_sym] = parse(value)
54
+ end
55
+ end
56
+ end
57
+
58
+ def parse(value)
59
+ begin
60
+ return Integer(value)
61
+ rescue
62
+ return value
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module SearchQuery
2
+ Version = '0.0.1'
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+
3
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ require 'search_query'
5
+
6
+ FIXTURES_PATH = "#{File.dirname(__FILE__)}/fixtures"
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe SearchQuery::Query do
4
+
5
+ describe 'String params' do
6
+ it "set query with on int param" do
7
+ query = SearchQuery::Query.new("param=10")
8
+ query.to_hsh.should eql({:param => 10})
9
+ query.to_s.should eql("param=10")
10
+ end
11
+
12
+ it "set query with on string param" do
13
+ query = SearchQuery::Query.new("param=test")
14
+ query.to_hsh.should eql({:param => "test"})
15
+ query.to_s.should eql("param=test")
16
+ end
17
+
18
+ it "set query with on int param and whitespaces" do
19
+ query = SearchQuery::Query.new("param = 10")
20
+ query.to_hsh.should eql({:param => 10})
21
+ query.to_s.should eql("param=10")
22
+ end
23
+
24
+ it "set query with one int param and one string param" do
25
+ query = SearchQuery::Query.new("param_1=10&&param_2=test")
26
+ query.to_hsh.should eql({:param_1 => 10, :param_2 => 'test'})
27
+ query.to_s.should eql("param_2=test&&param_1=10")
28
+ end
29
+
30
+ it "set query with a param with 3 values" do
31
+ query = SearchQuery::Query.new("param=9&&param=10&&param=11")
32
+ query.to_hsh.should eql({:param => [9,10,11]})
33
+ query.to_s.should eql("param=9&&param=10&&param=11")
34
+ end
35
+ end
36
+
37
+
38
+ describe 'Hash params' do
39
+ it "set query with on int param" do
40
+ query = SearchQuery::Query.new({:param => "10"})
41
+ query.to_hsh.should eql({:param => 10})
42
+ query.to_s.should eql("param=10")
43
+ end
44
+
45
+ it "set query with on string param" do
46
+ query = SearchQuery::Query.new({:param => "test"})
47
+ query.to_hsh.should eql({:param => "test"})
48
+ query.to_s.should eql("param=test")
49
+ end
50
+ end
51
+
52
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search_query
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Alexander Klaiber
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-11 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A query string parser
23
+ email: alex.klaiber@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - LICENSE
31
+ files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ - Rakefile
35
+ - lib/search_query/version.rb
36
+ - lib/search_query.rb
37
+ - spec/unit/search_query_spec.rb
38
+ - spec/spec_helper.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/aklaiber/search_query
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A query string parser
73
+ test_files: []
74
+