From 8a70a8ef40531def06c950bfb4902abc774b5b31 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sat, 6 Dec 2008 22:47:01 -0500 Subject: [PATCH] Removed active support. Added json. Added some core extensions. --- Rakefile | 2 +- lib/core_extensions.rb | 80 +++++++++++++++++++++++++++++++++++ lib/httparty.rb | 4 +- lib/httparty/request.rb | 5 ++- spec/httparty/request_spec.rb | 2 +- spec/spec_helper.rb | 6 +-- 6 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 lib/core_extensions.rb diff --git a/Rakefile b/Rakefile index 38429b8..518ef07 100644 --- a/Rakefile +++ b/Rakefile @@ -13,7 +13,7 @@ Echoe.new(ProjectName, HTTParty::Version) do |p| p.url = "http://#{ProjectName}.rubyforge.org" p.author = "John Nunemaker" p.email = "nunemaker@gmail.com" - p.extra_deps = [['activesupport', '>= 2.1']] + p.extra_deps = [['json', '~> 1.1']] p.need_tar_gz = false p.docs_host = WebsitePath end diff --git a/lib/core_extensions.rb b/lib/core_extensions.rb new file mode 100644 index 0000000..b6e6897 --- /dev/null +++ b/lib/core_extensions.rb @@ -0,0 +1,80 @@ +# Copyright (c) 2008 Sam Smoot. +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +class Object + # @return + # + # @example [].blank? #=> true + # @example [1].blank? #=> false + # @example [nil].blank? #=> false + # + # Returns true if the object is nil or empty (if applicable) + def blank? + nil? || (respond_to?(:empty?) && empty?) + end +end # class Object + +class Numeric + # @return + # + # Numerics can't be blank + def blank? + false + end +end # class Numeric + +class NilClass + # @return + # + # Nils are always blank + def blank? + true + end +end # class NilClass + +class TrueClass + # @return + # + # True is not blank. + def blank? + false + end +end # class TrueClass + +class FalseClass + # False is always blank. + def blank? + true + end +end # class FalseClass + +class String + # @example "".blank? #=> true + # @example " ".blank? #=> true + # @example " hey ho ".blank? #=> false + # + # @return + # + # Strips out whitespace then tests if the string is empty. + def blank? + strip.empty? + end +end # class String diff --git a/lib/httparty.rb b/lib/httparty.rb index 1b5d1ea..d21d223 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -5,8 +5,8 @@ require 'net/https' require 'rubygems' gem 'json', '>= 1.1.3' require 'json' -require 'active_support' require 'module_level_inheritable_attributes' +require 'core_extensions' module HTTParty AllowedFormats = {:xml => 'text/xml', :json => 'application/json', :html => 'text/html'} @@ -18,7 +18,7 @@ module HTTParty base.instance_variable_set("@default_options", {}) end - module ClassMethods + module ClassMethods def default_options @default_options end diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index 371d5fb..b949cf3 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -5,8 +5,11 @@ module HTTParty # Makes it so uri is sure to parse stuff like google.com without the http def self.normalize_base_uri(url) #:nodoc: use_ssl = (url =~ /^https/) || url.include?(':443') - url.chop! if url.ends_with?('/') + ends_with_slash = url =~ /\/$/ + + url.chop! if ends_with_slash url.gsub!(/^https?:\/\//i, '') + "http#{'s' if use_ssl}://#{url}" end diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index de5c972..9945549 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -84,7 +84,7 @@ describe HTTParty::Request do def setup_ok_response @ok = Net::HTTPOK.new("1.1", 200, "Content for you") - @ok.stub!(:body).and_return({"foo" => "bar"}.to_xml) + @ok.stub!(:body).and_return('bar') @http.should_receive(:request).and_return(@redirect, @ok) @request.options[:format] = :xml end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f435b41..19c2e29 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,9 +7,9 @@ def file_fixture(filename) open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename.to_s}")).read end -def stub_http_response_with(fixture_name) - format = fixture_name.split('.').last.intern - data = file_fixture(fixture_name) +def stub_http_response_with(filename) + format = filename.split('.').last.intern + data = file_fixture(filename) http = Net::HTTP.new('localhost', 80) response = Net::HTTPOK.new("1.1", 200, "Content for you")