rquery_string 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: fb579fd62c35ea83c4bd4058728f0d5826b48838
4
+ data.tar.gz: 18a1eec86ffef623851723f99b8d7a442cf59d24
5
+ SHA512:
6
+ metadata.gz: 93113919a2100c18db194f682a4b1d4afc5bf07d6958ec789cc12a8dda80dc465f17f857175f1da84a9a63493ac7061ee3aec68761e8417ea7d8fa8ec4e17415
7
+ data.tar.gz: 71403d61e055c20bef8db99d46cd0ca7b07bd0b0e484d453474fd33a6ef0fd8b677a96dd464080325a6ee0e41522d8805bc9413c04deb10327b0467c120cb613
@@ -0,0 +1,59 @@
1
+ require 'cgi'
2
+
3
+ module RqueryString
4
+ class Generator
5
+ def self.build_query_string(hash_para)
6
+ query_string = ""
7
+ hash_para.each do |key, value|
8
+ begin
9
+ query_string << (send "build_#{value.class.to_s.downcase}_type".to_sym, escape_key(key), value) << "&"
10
+ rescue NoMethodError => e
11
+ query_string << "#{escape_key(key)}=#{value}&"
12
+ end
13
+ end
14
+ return query_string[0..query_string.length - 2]
15
+ end
16
+
17
+ private
18
+
19
+ def self.build_hash_type(hash_key, hash_value)
20
+ query_string = ""
21
+ hash_value.each do |key, value|
22
+ new_key = "#{hash_key}[#{escape_key(key)}]"
23
+ begin
24
+ query_string << (send "build_#{value.class.to_s.downcase}_type".to_sym,
25
+ "#{new_key}", value) << "&"
26
+ rescue NoMethodError => e
27
+ query_string << "#{new_key}=#{value}&"
28
+ end
29
+ end
30
+ return query_string[0..query_string.length - 2]
31
+ end
32
+
33
+ def self.build_string_type(key, value)
34
+ str_value = "'" + value + "'"
35
+ "#{key}=#{CGI::escape(str_value)}"
36
+ end
37
+
38
+ def self.build_array_type(key, value)
39
+ query_string = ""
40
+ value.each_index do |index|
41
+ begin
42
+ query_string << (send "build_#{value[index].class.to_s.downcase}_type".to_sym,
43
+ "#{key}[]", value[index]) << "&"
44
+ rescue NoMethodError => e
45
+ query_string << "#{key}[]=#{value[index]}&"
46
+ end
47
+ end
48
+ return query_string[0..query_string.length - 2]
49
+ end
50
+
51
+ def self.escape_key(key)
52
+ if key.is_a? String
53
+ key = "'" + key + "'"
54
+ end
55
+ CGI::escape(key.to_s)
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,111 @@
1
+ require 'cgi'
2
+
3
+ module RqueryString
4
+ class Parser
5
+
6
+ def self.parse(query_string)
7
+ hash_pair_array = query_string.split("&")
8
+ result_hash = Hash.new
9
+ hash_pair_array.each do |hash_value|
10
+ key, value = hash_value.split("=")
11
+ if is_array(key)
12
+ parse_array(key, value, result_hash)
13
+ elsif is_hash(key)
14
+ parse_hash(key, value, result_hash)
15
+ elsif is_string(value)
16
+ result_hash[parse_key(key)] = parse_string(value)
17
+ else
18
+ result_hash[parse_key(key)] = eval(value)
19
+ end
20
+ end
21
+ return result_hash
22
+ end
23
+
24
+ private
25
+
26
+ def self.is_array(key)
27
+ key.end_with?("]") && ((key.index("[")+ 2) == key.length)
28
+ end
29
+
30
+ def self.is_hash(key)
31
+ key.end_with?("]") && ((key.index("[")+ 1) < key.rindex("]"))
32
+ end
33
+
34
+ def self.is_string(value)
35
+ value.start_with?("%27") && value.end_with?("%27")
36
+ end
37
+
38
+ def self.parse_array(key, value, result_hash)
39
+ array_key = parse_key(key[0..key.index("[") - 1])
40
+ result_hash[array_key] = [] if result_hash[array_key].nil?
41
+ result_hash[array_key] << parse_value(value)
42
+ end
43
+
44
+ def self.parse_hash(key, value, result_hash)
45
+ hash_key = parse_key(key[0..key.index("[") - 1])
46
+ keys = key[key.index("[")..-1].split("]").reverse
47
+ sub_result_object = nil
48
+ keys.each_index do |index|
49
+ sub_key = parse_key(keys[index][1..-1])
50
+ sub_hash_value = sub_result_object
51
+ if sub_key == :""
52
+ sub_result_object = Array.new
53
+ if sub_hash_value
54
+ sub_result_object << sub_hash_value
55
+ else
56
+ sub_result_object << parse_value(value)
57
+ end
58
+ else
59
+ sub_result_object = Hash.new
60
+ if index == 0
61
+ sub_result_object[sub_key] = parse_value(value)
62
+ else
63
+ sub_result_object[sub_key] = sub_hash_value
64
+ end
65
+ end
66
+ end
67
+ if result_hash[hash_key].nil?
68
+ result_hash[hash_key] = sub_result_object
69
+ else
70
+ if sub_result_object.instance_of?(Array) && sub_result_object.instance_of?(Array)
71
+ result_hash[hash_key].concat sub_result_object
72
+ else
73
+ merge_array_value(result_hash[hash_key], sub_result_object)
74
+ result_hash[hash_key].merge! sub_result_object
75
+ end
76
+ end
77
+ end
78
+
79
+ def self.parse_string(value)
80
+ CGI::unescape(value[3..value.rindex("%27")-1])
81
+ end
82
+
83
+ def self.parse_value(value)
84
+ if is_string(value)
85
+ parse_string(value)
86
+ else
87
+ eval(value)
88
+ end
89
+ end
90
+
91
+ def self.parse_key(key)
92
+ if is_string(key)
93
+ return parse_string(key)
94
+ else
95
+ return key.to_sym
96
+ end
97
+ end
98
+
99
+ def self.merge_array_value(old_hash_value, new_hash_value)
100
+ old_hash_value.each do |key, value|
101
+ if value.instance_of?(Array) && new_hash_value[key].instance_of?(Array)
102
+ new_hash_value[key] = value.concat new_hash_value[key]
103
+ else
104
+ if value.instance_of?(Hash) && new_hash_value[key].instance_of?(Hash)
105
+ merge_array_value(old_hash_value[key], new_hash_value[key])
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/rquery_string"
2
+ require 'generator'
3
+ require 'parser'
4
+
5
+ module RqueryString
6
+ extend self
7
+
8
+ def build(para_hash)
9
+ Generator.build_query_string(para_hash)
10
+ end
11
+
12
+ def parse(query_string)
13
+ Parser.parse(query_string)
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rquery_string
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Grant Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.14.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.14.1
27
+ description: query string generator and parser gem
28
+ email: kucss@hotmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/rquery_string/generator.rb
34
+ - lib/rquery_string/parser.rb
35
+ - lib/rquery_string.rb
36
+ homepage: https://github.com/grantchen/rquery_string
37
+ licenses: []
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.0.3
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: query string generator and parser
59
+ test_files: []
60
+ has_rdoc: