public
Rubygem
Description: Makes http fun! Also, makes consuming restful web services dead easy.
Homepage:
Clone URL: git://github.com/jnunemaker/httparty.git
Click here to lend your support to: httparty and make a donation at www.pledgie.com !
Cleaned up base uri normalization.
jnunemaker (author)
Sat Nov 08 08:18:25 -0800 2008
commit  bcc38bcf32218e3a7c5d754582b1c04002f0119b
tree    d30c53f2fbab5817477694fb20dedf3557a40cd0
parent  3101b5318b9476a9c7937f86581cddb623c820ee
...
33
34
35
36
37
38
39
40
...
150
151
152
153
154
155
156
157
158
159
160
 
 
 
 
 
161
162
163
...
33
34
35
 
 
36
37
38
...
148
149
150
 
151
152
153
154
155
 
 
156
157
158
159
160
161
162
163
0
@@ -33,8 +33,6 @@ module HTTParty
0
 
0
     def base_uri(base_uri=nil)
0
       return @base_uri unless base_uri
0
-      # don't want this to ever end with /
0
-      base_uri = base_uri.ends_with?('/') ? base_uri.chop : base_uri
0
       @base_uri = normalize_base_uri(base_uri)
0
     end
0
     
0
@@ -150,14 +148,16 @@ module HTTParty
0
         when :json
0
           ActiveSupport::JSON.decode(body)
0
         else
0
-          # just return the response if no format 
0
           body
0
         end
0
       end
0
     
0
       # Makes it so uri is sure to parse stuff like google.com with the http
0
-      def normalize_base_uri(str) #:nodoc:
0
-        str =~ /^https?:\/\// ? str : "http#{'s' if str.include?(':443')}://#{str}"
0
+      def normalize_base_uri(url) #:nodoc:
0
+        use_ssl = (url =~ /^https/) || url.include?(':443')
0
+        url.chop! if url.ends_with?('/')
0
+        url.gsub!(/^https?:\/\//i, '')
0
+        "http#{'s' if use_ssl}://#{url}"
0
       end
0
       
0
       # Uses the HTTP Content-Type header to determine the format of the response
...
13
14
15
16
 
17
18
19
20
 
21
22
23
...
28
29
30
31
 
 
 
 
 
 
 
32
33
34
...
13
14
15
 
16
17
18
19
 
20
21
22
23
...
28
29
30
 
31
32
33
34
35
36
37
38
39
40
0
@@ -13,11 +13,11 @@ end
0
 describe HTTParty do
0
   
0
   describe "base uri" do
0
-    it "should be gettable" do
0
+    it "should have reader" do
0
       Foo.base_uri.should == 'http://api.foo.com/v1'
0
     end
0
     
0
-    it 'should be setable' do
0
+    it 'should have writer' do
0
       Foo.base_uri('http://api.foobar.com')
0
       Foo.base_uri.should == 'http://api.foobar.com'
0
     end
0
@@ -28,7 +28,13 @@ describe HTTParty do
0
     end
0
     
0
     it "should add https if not present for ssl requests" do
0
-      FooWithHttps.base_uri.should == 'https://api.foo.com/v1:443'
0
+      Foo.base_uri('api.foo.com/v1:443')
0
+      Foo.base_uri.should == 'https://api.foo.com/v1:443'
0
+    end
0
+    
0
+    it "should not remove https for ssl requests" do
0
+      Foo.base_uri('https://api.foo.com/v1:443')
0
+      Foo.base_uri.should == 'https://api.foo.com/v1:443'
0
     end
0
   end
0
   

Comments