crack 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of crack might be problematic. Click here for more details.
- data/crack.gemspec +2 -2
- data/lib/crack.rb +2 -2
- data/lib/crack/util.rb +17 -0
- data/lib/crack/xml.rb +4 -4
- data/test/hash_test.rb +4 -33
- data/test/string_test.rb +8 -8
- metadata +4 -4
- data/lib/crack/core_extensions.rb +0 -128
data/crack.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{crack}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{John Nunemaker}, %q{Wynn Netherland}]
|
@@ -22,8 +22,8 @@ Gem::Specification.new do |s|
|
|
22
22
|
"Rakefile",
|
23
23
|
"crack.gemspec",
|
24
24
|
"lib/crack.rb",
|
25
|
-
"lib/crack/core_extensions.rb",
|
26
25
|
"lib/crack/json.rb",
|
26
|
+
"lib/crack/util.rb",
|
27
27
|
"lib/crack/xml.rb",
|
28
28
|
"test/crack_test.rb",
|
29
29
|
"test/data/twittersearch-firefox.json",
|
data/lib/crack.rb
CHANGED
data/lib/crack/util.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Crack
|
2
|
+
module Util
|
3
|
+
def snake_case(str)
|
4
|
+
return str.downcase if str =~ /^[A-Z]+$/
|
5
|
+
str.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
|
6
|
+
return $+.downcase
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_xml_attributes(hash)
|
10
|
+
hash.map do |k,v|
|
11
|
+
%{#{Crack::Util.snake_case(k.to_s).sub(/^(.{1,1})/) { |m| m.downcase }}="#{v.to_s.gsub('"', '"')}"}
|
12
|
+
end.join(' ')
|
13
|
+
end
|
14
|
+
|
15
|
+
extend self
|
16
|
+
end
|
17
|
+
end
|
data/lib/crack/xml.rb
CHANGED
@@ -48,12 +48,12 @@ class REXMLUtilityNode #:nodoc:
|
|
48
48
|
self.available_typecasts = self.typecasts.keys
|
49
49
|
|
50
50
|
def initialize(name, normalized_attributes = {})
|
51
|
-
|
51
|
+
|
52
52
|
# unnormalize attribute values
|
53
53
|
attributes = Hash[* normalized_attributes.map { |key, value|
|
54
54
|
[ key, unnormalize_xml_entities(value) ]
|
55
55
|
}.flatten]
|
56
|
-
|
56
|
+
|
57
57
|
@name = name.tr("-", "_")
|
58
58
|
# leave the type alone if we don't know what it is
|
59
59
|
@type = self.class.available_typecasts.include?(attributes["type"]) ? attributes.delete("type") : attributes["type"]
|
@@ -173,14 +173,14 @@ class REXMLUtilityNode #:nodoc:
|
|
173
173
|
# @return <String> The HTML node in text form.
|
174
174
|
def to_html
|
175
175
|
attributes.merge!(:type => @type ) if @type
|
176
|
-
"<#{name}#{
|
176
|
+
"<#{name}#{Crack::Util.to_xml_attributes(attributes)}>#{@nil_element ? '' : inner_html}</#{name}>"
|
177
177
|
end
|
178
178
|
|
179
179
|
# @alias #to_html #to_s
|
180
180
|
def to_s
|
181
181
|
to_html
|
182
182
|
end
|
183
|
-
|
183
|
+
|
184
184
|
private
|
185
185
|
|
186
186
|
def unnormalize_xml_entities value
|
data/test/hash_test.rb
CHANGED
@@ -5,53 +5,24 @@ class CrackTest < Test::Unit::TestCase
|
|
5
5
|
setup do
|
6
6
|
@hash = { :one => "ONE", "two" => "TWO", :three => "it \"should\" work" }
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
should "turn the hash into xml attributes" do
|
10
|
-
attrs = @hash
|
10
|
+
attrs = Crack::Util.to_xml_attributes(@hash)
|
11
11
|
attrs.should =~ /one="ONE"/m
|
12
12
|
attrs.should =~ /two="TWO"/m
|
13
13
|
attrs.should =~ /three="it "should" work"/m
|
14
14
|
end
|
15
15
|
|
16
16
|
should 'preserve _ in hash keys' do
|
17
|
-
attrs = {
|
17
|
+
attrs = Crack::Util.to_xml_attributes({
|
18
18
|
:some_long_attribute => "with short value",
|
19
19
|
:crash => :burn,
|
20
20
|
:merb => "uses extlib"
|
21
|
-
}
|
21
|
+
})
|
22
22
|
|
23
23
|
attrs.should =~ /some_long_attribute="with short value"/
|
24
24
|
attrs.should =~ /merb="uses extlib"/
|
25
25
|
attrs.should =~ /crash="burn"/
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
29
|
-
context "to_params" do
|
30
|
-
{
|
31
|
-
{ "foo" => "bar", "baz" => "bat" } => "foo=bar&baz=bat",
|
32
|
-
{ "foo" => [ "bar", "baz" ] } => "foo[]=bar&foo[]=baz",
|
33
|
-
{ "foo" => [ {"bar" => "1"}, {"bar" => 2} ] } => "foo[][bar]=1&foo[][bar]=2",
|
34
|
-
{ "foo" => { "bar" => [ {"baz" => 1}, {"baz" => "2"} ] } } => "foo[bar][][baz]=1&foo[bar][][baz]=2",
|
35
|
-
{ "foo" => {"1" => "bar", "2" => "baz"} } => "foo[1]=bar&foo[2]=baz"
|
36
|
-
}.each do |hash, params|
|
37
|
-
should "should covert hash: #{hash.inspect} to params: #{params.inspect}" do
|
38
|
-
hash.to_params.split('&').sort.should == params.split('&').sort
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
should 'not leave a trailing &' do
|
43
|
-
{
|
44
|
-
:name => 'Bob',
|
45
|
-
:address => {
|
46
|
-
:street => '111 Ruby Ave.',
|
47
|
-
:city => 'Ruby Central',
|
48
|
-
:phones => ['111-111-1111', '222-222-2222']
|
49
|
-
}
|
50
|
-
}.to_params.should_not =~ /&$/
|
51
|
-
end
|
52
|
-
|
53
|
-
should 'URL encode unsafe characters' do
|
54
|
-
{:q => "?&\" +"}.to_params.should == "q=%3F%26%22%20%2B"
|
55
|
-
end
|
56
|
-
end
|
57
28
|
end
|
data/test/string_test.rb
CHANGED
@@ -3,29 +3,29 @@ require 'test_helper'
|
|
3
3
|
class CrackTest < Test::Unit::TestCase
|
4
4
|
context "snake_case" do
|
5
5
|
should "lowercases one word CamelCase" do
|
6
|
-
"Merb".
|
6
|
+
Crack::Util.snake_case("Merb").should == "merb"
|
7
7
|
end
|
8
8
|
|
9
9
|
should "makes one underscore snake_case two word CamelCase" do
|
10
|
-
"MerbCore".
|
10
|
+
Crack::Util.snake_case("MerbCore").should == "merb_core"
|
11
11
|
end
|
12
12
|
|
13
13
|
should "handles CamelCase with more than 2 words" do
|
14
|
-
"SoYouWantContributeToMerbCore".
|
14
|
+
Crack::Util.snake_case("SoYouWantContributeToMerbCore").should == "so_you_want_contribute_to_merb_core"
|
15
15
|
end
|
16
16
|
|
17
17
|
should "handles CamelCase with more than 2 capital letter in a row" do
|
18
|
-
"CNN".
|
19
|
-
"CNNNews".
|
20
|
-
"HeadlineCNNNews".
|
18
|
+
Crack::Util.snake_case("CNN").should == "cnn"
|
19
|
+
Crack::Util.snake_case("CNNNews").should == "cnn_news"
|
20
|
+
Crack::Util.snake_case("HeadlineCNNNews").should == "headline_cnn_news"
|
21
21
|
end
|
22
22
|
|
23
23
|
should "does NOT change one word lowercase" do
|
24
|
-
"merb".
|
24
|
+
Crack::Util.snake_case("merb").should == "merb"
|
25
25
|
end
|
26
26
|
|
27
27
|
should "leaves snake_case as is" do
|
28
|
-
"merb_core".
|
28
|
+
Crack::Util.snake_case("merb_core").should == "merb_core"
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Nunemaker
|
@@ -35,8 +35,8 @@ files:
|
|
35
35
|
- Rakefile
|
36
36
|
- crack.gemspec
|
37
37
|
- lib/crack.rb
|
38
|
-
- lib/crack/core_extensions.rb
|
39
38
|
- lib/crack/json.rb
|
39
|
+
- lib/crack/util.rb
|
40
40
|
- lib/crack/xml.rb
|
41
41
|
- test/crack_test.rb
|
42
42
|
- test/data/twittersearch-firefox.json
|
@@ -1,128 +0,0 @@
|
|
1
|
-
require 'uri'
|
2
|
-
|
3
|
-
class Object #:nodoc:
|
4
|
-
# @return <TrueClass, FalseClass>
|
5
|
-
#
|
6
|
-
# @example [].blank? #=> true
|
7
|
-
# @example [1].blank? #=> false
|
8
|
-
# @example [nil].blank? #=> false
|
9
|
-
#
|
10
|
-
# Returns true if the object is nil or empty (if applicable)
|
11
|
-
def blank?
|
12
|
-
nil? || (respond_to?(:empty?) && empty?)
|
13
|
-
end unless method_defined?(:blank?)
|
14
|
-
end # class Object
|
15
|
-
|
16
|
-
class Numeric #:nodoc:
|
17
|
-
# @return <TrueClass, FalseClass>
|
18
|
-
#
|
19
|
-
# Numerics can't be blank
|
20
|
-
def blank?
|
21
|
-
false
|
22
|
-
end unless method_defined?(:blank?)
|
23
|
-
end # class Numeric
|
24
|
-
|
25
|
-
class NilClass #:nodoc:
|
26
|
-
# @return <TrueClass, FalseClass>
|
27
|
-
#
|
28
|
-
# Nils are always blank
|
29
|
-
def blank?
|
30
|
-
true
|
31
|
-
end unless method_defined?(:blank?)
|
32
|
-
end # class NilClass
|
33
|
-
|
34
|
-
class TrueClass #:nodoc:
|
35
|
-
# @return <TrueClass, FalseClass>
|
36
|
-
#
|
37
|
-
# True is not blank.
|
38
|
-
def blank?
|
39
|
-
false
|
40
|
-
end unless method_defined?(:blank?)
|
41
|
-
end # class TrueClass
|
42
|
-
|
43
|
-
class FalseClass #:nodoc:
|
44
|
-
# False is always blank.
|
45
|
-
def blank?
|
46
|
-
true
|
47
|
-
end unless method_defined?(:blank?)
|
48
|
-
end # class FalseClass
|
49
|
-
|
50
|
-
class String #:nodoc:
|
51
|
-
# @example "".blank? #=> true
|
52
|
-
# @example " ".blank? #=> true
|
53
|
-
# @example " hey ho ".blank? #=> false
|
54
|
-
#
|
55
|
-
# @return <TrueClass, FalseClass>
|
56
|
-
#
|
57
|
-
# Strips out whitespace then tests if the string is empty.
|
58
|
-
def blank?
|
59
|
-
strip.empty?
|
60
|
-
end unless method_defined?(:blank?)
|
61
|
-
|
62
|
-
def snake_case
|
63
|
-
return self.downcase if self =~ /^[A-Z]+$/
|
64
|
-
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
|
65
|
-
return $+.downcase
|
66
|
-
end unless method_defined?(:snake_case)
|
67
|
-
end # class String
|
68
|
-
|
69
|
-
class Hash #:nodoc:
|
70
|
-
# @return <String> This hash as a query string
|
71
|
-
#
|
72
|
-
# @example
|
73
|
-
# { :name => "Bob",
|
74
|
-
# :address => {
|
75
|
-
# :street => '111 Ruby Ave.',
|
76
|
-
# :city => 'Ruby Central',
|
77
|
-
# :phones => ['111-111-1111', '222-222-2222']
|
78
|
-
# }
|
79
|
-
# }.to_params
|
80
|
-
# #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
|
81
|
-
def to_params
|
82
|
-
params = self.map { |k,v| normalize_param(k,v) }.join
|
83
|
-
params.chop! # trailing &
|
84
|
-
params
|
85
|
-
end
|
86
|
-
|
87
|
-
# @param key<Object> The key for the param.
|
88
|
-
# @param value<Object> The value for the param.
|
89
|
-
#
|
90
|
-
# @return <String> This key value pair as a param
|
91
|
-
#
|
92
|
-
# @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
|
93
|
-
def normalize_param(key, value)
|
94
|
-
param = ''
|
95
|
-
stack = []
|
96
|
-
|
97
|
-
if value.is_a?(Array)
|
98
|
-
param << value.map { |element| normalize_param("#{key}[]", element) }.join
|
99
|
-
elsif value.is_a?(Hash)
|
100
|
-
stack << [key,value]
|
101
|
-
else
|
102
|
-
param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
|
103
|
-
end
|
104
|
-
|
105
|
-
stack.each do |parent, hash|
|
106
|
-
hash.each do |key, value|
|
107
|
-
if value.is_a?(Hash)
|
108
|
-
stack << ["#{parent}[#{key}]", value]
|
109
|
-
else
|
110
|
-
param << normalize_param("#{parent}[#{key}]", value)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
param
|
116
|
-
end
|
117
|
-
|
118
|
-
# @return <String> The hash as attributes for an XML tag.
|
119
|
-
#
|
120
|
-
# @example
|
121
|
-
# { :one => 1, "two"=>"TWO" }.to_xml_attributes
|
122
|
-
# #=> 'one="1" two="TWO"'
|
123
|
-
def to_xml_attributes
|
124
|
-
map do |k,v|
|
125
|
-
%{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v.to_s.gsub('"', '"')}"}
|
126
|
-
end.join(' ')
|
127
|
-
end
|
128
|
-
end
|