kerryb-httparty 0.3.3 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/History CHANGED
@@ -1,3 +1,20 @@
1
+ == 0.4.5 2009-04-23
2
+ * 1 minor update
3
+ * added message to the response object
4
+
5
+ == 0.4.2 2009-03-30
6
+ * 2 minor changes
7
+ * response code now returns an integer instead of a string (jqr)
8
+ * rubyforge project setup for crack so i'm now depending on that instead of jnunemaker-crack
9
+
10
+ == 0.4.1 2009-03-29
11
+ * 1 minor fix
12
+ * gem 'jnunemaker-crack' instead of gem 'crack'
13
+
14
+ == 0.4.0 2009-03-29
15
+ * 1 minor change
16
+ * Switched xml and json parsing to crack (same code as before just moved to gem for easier reuse in other projects)
17
+
1
18
  == 0.3.1 2009-02-10
2
19
  * 1 minor fix, 1 minor enhancement
3
20
  * Fixed unescaping umlauts (siebertm)
data/Manifest CHANGED
@@ -19,13 +19,10 @@ features/steps/remote_service_steps.rb
19
19
  features/supports_redirection.feature
20
20
  History
21
21
  httparty.gemspec
22
- lib/core_extensions.rb
23
22
  lib/httparty/cookie_hash.rb
23
+ lib/httparty/core_extensions.rb
24
24
  lib/httparty/exceptions.rb
25
25
  lib/httparty/module_inheritable_attributes.rb
26
- lib/httparty/parsers/json.rb
27
- lib/httparty/parsers/xml.rb
28
- lib/httparty/parsers.rb
29
26
  lib/httparty/request.rb
30
27
  lib/httparty/response.rb
31
28
  lib/httparty/version.rb
@@ -43,8 +40,6 @@ spec/fixtures/twitter.xml
43
40
  spec/fixtures/undefined_method_add_node_for_nil.xml
44
41
  spec/hash_spec.rb
45
42
  spec/httparty/cookie_hash_spec.rb
46
- spec/httparty/parsers/json_spec.rb
47
- spec/httparty/parsers/xml_spec.rb
48
43
  spec/httparty/request_spec.rb
49
44
  spec/httparty/response_spec.rb
50
45
  spec/httparty_spec.rb
data/README CHANGED
@@ -28,8 +28,13 @@ options. Below is an example of how easy it is.
28
28
 
29
29
  == REQUIREMENTS:
30
30
 
31
+ * Crack http://github.com/jnunemaker/crack/ - For XML and JSON parsing.
31
32
  * You like to party!
32
33
 
33
34
  == INSTALL:
34
35
 
35
- * sudo gem install httparty
36
+ * sudo gem install httparty
37
+
38
+ == DOCS:
39
+
40
+ http://rdoc.info/projects/jnunemaker/httparty
data/Rakefile CHANGED
@@ -14,6 +14,7 @@ Echoe.new(ProjectName, HTTParty::Version) do |p|
14
14
  p.url = "http://#{ProjectName}.rubyforge.org"
15
15
  p.author = "John Nunemaker"
16
16
  p.email = "nunemaker@gmail.com"
17
+ p.extra_deps = [['crack', '>= 0.1.1']]
17
18
  p.need_tar_gz = false
18
19
  p.docs_host = WebsitePath
19
20
  end
data/examples/basic.rb CHANGED
@@ -4,8 +4,8 @@ require 'pp'
4
4
 
5
5
  # You can also use post, put, delete in the same fashion
6
6
  response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
7
- puts response.body, response.code, response.headers.inspect
7
+ puts response.body, response.code, response.message, response.headers.inspect
8
8
 
9
9
  response.each do |item|
10
10
  puts item['user']['screen_name']
11
- end
11
+ end
@@ -17,7 +17,7 @@ Then /it should return a Hash equaling:/ do |hash_table|
17
17
  end
18
18
 
19
19
  Then /it should return a response with a (\d+) response code/ do |code|
20
- @response_from_httparty.code.should eql(code)
20
+ @response_from_httparty.code.should eql(code.to_i)
21
21
  end
22
22
 
23
23
  Then /it should raise an HTTParty::RedirectionTooDeep exception/ do
data/httparty.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{httparty}
5
- s.version = "0.3.3"
5
+ s.version = "0.4.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["John Nunemaker"]
9
- s.date = %q{2009-02-10}
9
+ s.date = %q{2009-04-23}
10
10
  s.default_executable = %q{httparty}
11
11
  s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
12
12
  s.email = %q{nunemaker@gmail.com}
13
13
  s.executables = ["httparty"]
14
- s.extra_rdoc_files = ["bin/httparty", "lib/core_extensions.rb", "lib/httparty/cookie_hash.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/parsers/json.rb", "lib/httparty/parsers/xml.rb", "lib/httparty/parsers.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "README"]
15
- s.files = ["bin/httparty", "cucumber.yml", "examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "features/basic_authentication.feature", "features/command_line.feature", "features/deals_with_http_error_codes.feature", "features/handles_multiple_formats.feature", "features/steps/env.rb", "features/steps/httparty_response_steps.rb", "features/steps/httparty_steps.rb", "features/steps/mongrel_helper.rb", "features/steps/remote_service_steps.rb", "features/supports_redirection.feature", "History", "httparty.gemspec", "lib/core_extensions.rb", "lib/httparty/cookie_hash.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/parsers/json.rb", "lib/httparty/parsers/xml.rb", "lib/httparty/parsers.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "setup.rb", "spec/fixtures/delicious.xml", "spec/fixtures/empty.xml", "spec/fixtures/google.html", "spec/fixtures/twitter.json", "spec/fixtures/twitter.xml", "spec/fixtures/undefined_method_add_node_for_nil.xml", "spec/hash_spec.rb", "spec/httparty/cookie_hash_spec.rb", "spec/httparty/parsers/json_spec.rb", "spec/httparty/parsers/xml_spec.rb", "spec/httparty/request_spec.rb", "spec/httparty/response_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/string_spec.rb", "website/css/common.css", "website/index.html"]
14
+ s.extra_rdoc_files = ["bin/httparty", "lib/httparty/cookie_hash.rb", "lib/httparty/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "README"]
15
+ s.files = ["bin/httparty", "cucumber.yml", "examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "features/basic_authentication.feature", "features/command_line.feature", "features/deals_with_http_error_codes.feature", "features/handles_multiple_formats.feature", "features/steps/env.rb", "features/steps/httparty_response_steps.rb", "features/steps/httparty_steps.rb", "features/steps/mongrel_helper.rb", "features/steps/remote_service_steps.rb", "features/supports_redirection.feature", "History", "httparty.gemspec", "lib/httparty/cookie_hash.rb", "lib/httparty/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "setup.rb", "spec/fixtures/delicious.xml", "spec/fixtures/empty.xml", "spec/fixtures/google.html", "spec/fixtures/twitter.json", "spec/fixtures/twitter.xml", "spec/fixtures/undefined_method_add_node_for_nil.xml", "spec/hash_spec.rb", "spec/httparty/cookie_hash_spec.rb", "spec/httparty/request_spec.rb", "spec/httparty/response_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/string_spec.rb", "website/css/common.css", "website/index.html"]
16
16
  s.has_rdoc = true
17
17
  s.homepage = %q{http://httparty.rubyforge.org}
18
18
  s.post_install_message = %q{When you HTTParty, you must party hard!}
@@ -27,11 +27,14 @@ Gem::Specification.new do |s|
27
27
  s.specification_version = 2
28
28
 
29
29
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
30
+ s.add_runtime_dependency(%q<crack>, [">= 0.1.1"])
30
31
  s.add_development_dependency(%q<echoe>, [">= 0"])
31
32
  else
33
+ s.add_dependency(%q<crack>, [">= 0.1.1"])
32
34
  s.add_dependency(%q<echoe>, [">= 0"])
33
35
  end
34
36
  else
37
+ s.add_dependency(%q<crack>, [">= 0.1.1"])
35
38
  s.add_dependency(%q<echoe>, [">= 0"])
36
39
  end
37
40
  end
data/lib/httparty.rb CHANGED
@@ -2,8 +2,10 @@ $:.unshift(File.dirname(__FILE__))
2
2
 
3
3
  require 'net/http'
4
4
  require 'net/https'
5
- require 'core_extensions'
6
5
  require 'httparty/module_inheritable_attributes'
6
+ require 'rubygems'
7
+ gem 'crack'
8
+ require 'crack'
7
9
 
8
10
  module HTTParty
9
11
 
@@ -16,7 +18,8 @@ module HTTParty
16
18
  'text/javascript' => :json,
17
19
  'text/html' => :html,
18
20
  'application/x-yaml' => :yaml,
19
- 'text/yaml' => :yaml
21
+ 'text/yaml' => :yaml,
22
+ 'text/plain' => :plain
20
23
  } unless defined?(AllowedFormats)
21
24
 
22
25
  def self.included(base)
@@ -99,7 +102,7 @@ module HTTParty
99
102
  # format :json
100
103
  # end
101
104
  def format(f)
102
- raise UnsupportedFormat, "Must be one of: #{AllowedFormats.values.join(', ')}" unless AllowedFormats.value?(f)
105
+ raise UnsupportedFormat, "Must be one of: #{AllowedFormats.values.uniq.join(', ')}" unless AllowedFormats.value?(f)
103
106
  default_options[:format] = f
104
107
  end
105
108
 
@@ -164,13 +167,14 @@ module HTTParty
164
167
  end
165
168
 
166
169
  def self.normalize_base_uri(url) #:nodoc:
167
- use_ssl = (url =~ /^https/) || url.include?(':443')
168
- ends_with_slash = url =~ /\/$/
170
+ normalized_url = url.dup
171
+ use_ssl = (normalized_url =~ /^https/) || normalized_url.include?(':443')
172
+ ends_with_slash = normalized_url =~ /\/$/
169
173
 
170
- url.chop! if ends_with_slash
171
- url.gsub!(/^https?:\/\//i, '')
174
+ normalized_url.chop! if ends_with_slash
175
+ normalized_url.gsub!(/^https?:\/\//i, '')
172
176
 
173
- "http#{'s' if use_ssl}://#{url}"
177
+ "http#{'s' if use_ssl}://#{normalized_url}"
174
178
  end
175
179
 
176
180
  class Basement #:nodoc:
@@ -195,7 +199,7 @@ module HTTParty
195
199
  end
196
200
 
197
201
  require 'httparty/cookie_hash'
202
+ require 'httparty/core_extensions'
198
203
  require 'httparty/exceptions'
199
204
  require 'httparty/request'
200
205
  require 'httparty/response'
201
- require 'httparty/parsers'
@@ -0,0 +1,25 @@
1
+ class BlankSlate #:nodoc:
2
+ instance_methods.each { |m| undef_method m unless m =~ /^__/ }
3
+ end
4
+
5
+ # 1.8.6 has mistyping of transitive in if statement
6
+ require "rexml/document"
7
+ module REXML #:nodoc:
8
+ class Document < Element #:nodoc:
9
+ def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
10
+ if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
11
+ output = Output.new( output, xml_decl.encoding )
12
+ end
13
+ formatter = if indent > -1
14
+ if transitive
15
+ REXML::Formatters::Transitive.new( indent, ie_hack )
16
+ else
17
+ REXML::Formatters::Pretty.new( indent, ie_hack )
18
+ end
19
+ else
20
+ REXML::Formatters::Default.new( ie_hack )
21
+ end
22
+ formatter.write( self, output )
23
+ end
24
+ end
25
+ end
@@ -99,7 +99,7 @@ module HTTParty
99
99
  perform
100
100
  else
101
101
  parsed_response = parse_response(response.body)
102
- Response.new(parsed_response, response.body, response.code, response.to_hash)
102
+ Response.new(parsed_response, response.body, response.code, response.message, response.to_hash)
103
103
  end
104
104
  end
105
105
 
@@ -107,9 +107,9 @@ module HTTParty
107
107
  return nil if body.nil? or body.strip.empty?
108
108
  case format
109
109
  when :xml
110
- HTTParty::Parsers::XML.parse(body)
110
+ Crack::XML.parse(body)
111
111
  when :json
112
- HTTParty::Parsers::JSON.decode(body)
112
+ Crack::JSON.parse(body)
113
113
  when :yaml
114
114
  YAML::load(body)
115
115
  else
@@ -1,12 +1,13 @@
1
1
  module HTTParty
2
2
  class Response < BlankSlate #:nodoc:
3
- attr_accessor :body, :code, :headers
3
+ attr_accessor :body, :code, :message, :headers
4
4
  attr_reader :delegate
5
5
 
6
- def initialize(delegate, body, code, headers={})
6
+ def initialize(delegate, body, code, message, headers={})
7
7
  @delegate = delegate
8
8
  @body = body
9
- @code = code
9
+ @code = code.to_i
10
+ @message = message
10
11
  @headers = headers
11
12
  end
12
13
 
@@ -1,3 +1,3 @@
1
1
  module HTTParty #:nodoc:
2
- Version = '0.3.1'
2
+ Version = '0.4.3'
3
3
  end
@@ -5,8 +5,9 @@ describe HTTParty::Response do
5
5
  before do
6
6
  @response_object = {'foo' => 'bar'}
7
7
  @body = "{foo:'bar'}"
8
- @code = 200
9
- @response = HTTParty::Response.new(@response_object, @body, @code)
8
+ @code = '200'
9
+ @message = 'OK'
10
+ @response = HTTParty::Response.new(@response_object, @body, @code, @message)
10
11
  end
11
12
 
12
13
  it "should set delegate" do
@@ -18,22 +19,30 @@ describe HTTParty::Response do
18
19
  end
19
20
 
20
21
  it "should set code" do
21
- @response.code.should == @code
22
+ @response.code.should.to_s == @code
23
+ end
24
+
25
+ it "should set code as a Fixnum" do
26
+ @response.code.should be_an_instance_of(Fixnum)
27
+ end
28
+
29
+ it "should set body" do
30
+ @response.body.should == @body
22
31
  end
23
32
  end
24
33
 
25
34
  it "should be able to set headers during initialization" do
26
- response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, {'foo' => 'bar'})
35
+ response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK', {'foo' => 'bar'})
27
36
  response.headers.should == {'foo' => 'bar'}
28
37
  end
29
38
 
30
39
  it "should send missing methods to delegate" do
31
- response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200)
40
+ response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK')
32
41
  response['foo'].should == 'bar'
33
42
  end
34
43
 
35
44
  it "should be able to iterate delegate if it is array" do
36
- response = HTTParty::Response.new([{'foo' => 'bar'}, {'foo' => 'baz'}], "[{foo:'bar'}, {foo:'baz'}]", 200)
45
+ response = HTTParty::Response.new([{'foo' => 'bar'}, {'foo' => 'baz'}], "[{foo:'bar'}, {foo:'baz'}]", 200, 'OK')
37
46
  response.size.should == 2
38
47
  lambda {
39
48
  response.each { |item| }
@@ -41,12 +50,12 @@ describe HTTParty::Response do
41
50
  end
42
51
 
43
52
  xit "should allow hashes to be accessed with dot notation" do
44
- response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200)
53
+ response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK')
45
54
  response.foo.should == 'bar'
46
55
  end
47
56
 
48
57
  xit "should allow nested hashes to be accessed with dot notation" do
49
- response = HTTParty::Response.new({'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200)
58
+ response = HTTParty::Response.new({'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200, 'OK')
50
59
  response.foo.should == {'bar' => 'baz'}
51
60
  response.foo.bar.should == 'baz'
52
61
  end
@@ -19,6 +19,12 @@ describe HTTParty do
19
19
  @klass.base_uri('http://api.foobar.com')
20
20
  @klass.base_uri.should == 'http://api.foobar.com'
21
21
  end
22
+
23
+ it 'should not modify the parameter during assignment' do
24
+ uri = 'http://api.foobar.com'
25
+ @klass.base_uri(uri)
26
+ uri.should == 'http://api.foobar.com'
27
+ end
22
28
  end
23
29
 
24
30
  describe "#normalize_base_uri" do
@@ -36,6 +42,12 @@ describe HTTParty do
36
42
  uri = HTTParty.normalize_base_uri('https://api.foo.com/v1:443')
37
43
  uri.should == 'https://api.foo.com/v1:443'
38
44
  end
45
+
46
+ it 'should not modify the parameter' do
47
+ uri = 'http://api.foobar.com'
48
+ HTTParty.normalize_base_uri(uri)
49
+ uri.should == 'http://api.foobar.com'
50
+ end
39
51
  end
40
52
 
41
53
  describe "headers" do
@@ -149,11 +161,23 @@ describe HTTParty do
149
161
  @klass.default_options[:format].should == :yaml
150
162
  end
151
163
 
164
+ it "should allow plain" do
165
+ @klass.format :plain
166
+ @klass.default_options[:format].should == :plain
167
+ end
168
+
152
169
  it 'should not allow funky format' do
153
170
  lambda do
154
171
  @klass.format :foobar
155
172
  end.should raise_error(HTTParty::UnsupportedFormat)
156
173
  end
174
+
175
+ it 'should only print each format once with an exception' do
176
+ lambda do
177
+ @klass.format :foobar
178
+ end.should raise_error(HTTParty::UnsupportedFormat, "Must be one of: json, plain, html, yaml, xml")
179
+ end
180
+
157
181
  end
158
182
 
159
183
  describe "with explicit override of automatic redirect handling" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kerryb-httparty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,9 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-10 00:00:00 -08:00
12
+ date: 2009-04-23 00:00:00 -07:00
13
13
  default_executable: httparty
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: crack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.1.1
24
+ version:
15
25
  - !ruby/object:Gem::Dependency
16
26
  name: echoe
17
27
  type: :development
@@ -30,13 +40,10 @@ extensions: []
30
40
 
31
41
  extra_rdoc_files:
32
42
  - bin/httparty
33
- - lib/core_extensions.rb
34
43
  - lib/httparty/cookie_hash.rb
44
+ - lib/httparty/core_extensions.rb
35
45
  - lib/httparty/exceptions.rb
36
46
  - lib/httparty/module_inheritable_attributes.rb
37
- - lib/httparty/parsers/json.rb
38
- - lib/httparty/parsers/xml.rb
39
- - lib/httparty/parsers.rb
40
47
  - lib/httparty/request.rb
41
48
  - lib/httparty/response.rb
42
49
  - lib/httparty/version.rb
@@ -64,13 +71,10 @@ files:
64
71
  - features/supports_redirection.feature
65
72
  - History
66
73
  - httparty.gemspec
67
- - lib/core_extensions.rb
68
74
  - lib/httparty/cookie_hash.rb
75
+ - lib/httparty/core_extensions.rb
69
76
  - lib/httparty/exceptions.rb
70
77
  - lib/httparty/module_inheritable_attributes.rb
71
- - lib/httparty/parsers/json.rb
72
- - lib/httparty/parsers/xml.rb
73
- - lib/httparty/parsers.rb
74
78
  - lib/httparty/request.rb
75
79
  - lib/httparty/response.rb
76
80
  - lib/httparty/version.rb
@@ -88,8 +92,6 @@ files:
88
92
  - spec/fixtures/undefined_method_add_node_for_nil.xml
89
93
  - spec/hash_spec.rb
90
94
  - spec/httparty/cookie_hash_spec.rb
91
- - spec/httparty/parsers/json_spec.rb
92
- - spec/httparty/parsers/xml_spec.rb
93
95
  - spec/httparty/request_spec.rb
94
96
  - spec/httparty/response_spec.rb
95
97
  - spec/httparty_spec.rb
@@ -1,175 +0,0 @@
1
- require 'time'
2
-
3
- # Copyright (c) 2008 Sam Smoot.
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.
23
-
24
- class Object #:nodoc:
25
- # @return <TrueClass, FalseClass>
26
- #
27
- # @example [].blank? #=> true
28
- # @example [1].blank? #=> false
29
- # @example [nil].blank? #=> false
30
- #
31
- # Returns true if the object is nil or empty (if applicable)
32
- def blank?
33
- nil? || (respond_to?(:empty?) && empty?)
34
- end unless method_defined?(:blank?)
35
- end # class Object
36
-
37
- class Numeric #:nodoc:
38
- # @return <TrueClass, FalseClass>
39
- #
40
- # Numerics can't be blank
41
- def blank?
42
- false
43
- end unless method_defined?(:blank?)
44
- end # class Numeric
45
-
46
- class NilClass #:nodoc:
47
- # @return <TrueClass, FalseClass>
48
- #
49
- # Nils are always blank
50
- def blank?
51
- true
52
- end unless method_defined?(:blank?)
53
- end # class NilClass
54
-
55
- class TrueClass #:nodoc:
56
- # @return <TrueClass, FalseClass>
57
- #
58
- # True is not blank.
59
- def blank?
60
- false
61
- end unless method_defined?(:blank?)
62
- end # class TrueClass
63
-
64
- class FalseClass #:nodoc:
65
- # False is always blank.
66
- def blank?
67
- true
68
- end unless method_defined?(:blank?)
69
- end # class FalseClass
70
-
71
- class String #:nodoc:
72
- # @example "".blank? #=> true
73
- # @example " ".blank? #=> true
74
- # @example " hey ho ".blank? #=> false
75
- #
76
- # @return <TrueClass, FalseClass>
77
- #
78
- # Strips out whitespace then tests if the string is empty.
79
- def blank?
80
- strip.empty?
81
- end unless method_defined?(:blank?)
82
-
83
- def snake_case
84
- return self.downcase if self =~ /^[A-Z]+$/
85
- self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
86
- return $+.downcase
87
- end unless method_defined?(:snake_case)
88
- end # class String
89
-
90
- class Hash #:nodoc:
91
- # @return <String> This hash as a query string
92
- #
93
- # @example
94
- # { :name => "Bob",
95
- # :address => {
96
- # :street => '111 Ruby Ave.',
97
- # :city => 'Ruby Central',
98
- # :phones => ['111-111-1111', '222-222-2222']
99
- # }
100
- # }.to_params
101
- # #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
102
- def to_params
103
- params = self.map { |k,v| normalize_param(k,v) }.join
104
- params.chop! # trailing &
105
- params
106
- end
107
-
108
- # @param key<Object> The key for the param.
109
- # @param value<Object> The value for the param.
110
- #
111
- # @return <String> This key value pair as a param
112
- #
113
- # @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
114
- def normalize_param(key, value)
115
- param = ''
116
- stack = []
117
-
118
- if value.is_a?(Array)
119
- param << value.map { |element| normalize_param("#{key}[]", element) }.join
120
- elsif value.is_a?(Hash)
121
- stack << [key,value]
122
- else
123
- param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
124
- end
125
-
126
- stack.each do |parent, hash|
127
- hash.each do |key, value|
128
- if value.is_a?(Hash)
129
- stack << ["#{parent}[#{key}]", value]
130
- else
131
- param << normalize_param("#{parent}[#{key}]", value)
132
- end
133
- end
134
- end
135
-
136
- param
137
- end
138
-
139
- # @return <String> The hash as attributes for an XML tag.
140
- #
141
- # @example
142
- # { :one => 1, "two"=>"TWO" }.to_xml_attributes
143
- # #=> 'one="1" two="TWO"'
144
- def to_xml_attributes
145
- map do |k,v|
146
- %{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
147
- end.join(' ')
148
- end
149
- end
150
-
151
- class BlankSlate #:nodoc:
152
- instance_methods.each { |m| undef_method m unless m =~ /^__/ }
153
- end
154
-
155
- # 1.8.6 has mistyping of transitive in if statement
156
- require "rexml/document"
157
- module REXML #:nodoc:
158
- class Document < Element #:nodoc:
159
- def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
160
- if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
161
- output = Output.new( output, xml_decl.encoding )
162
- end
163
- formatter = if indent > -1
164
- if transitive
165
- REXML::Formatters::Transitive.new( indent, ie_hack )
166
- else
167
- REXML::Formatters::Pretty.new( indent, ie_hack )
168
- end
169
- else
170
- REXML::Formatters::Default.new( ie_hack )
171
- end
172
- formatter.write( self, output )
173
- end
174
- end
175
- end