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 !
Changed format detection from crappy file extension to awesome response content 
type. [#2 state:resolved]
jnunemaker (author)
Fri Aug 22 18:50:51 -0700 2008
commit  76601ba0c7d6daea37f94c71d01367a3ff684fa5
tree    192335eca4b34adda91317d1b540efaa6de0ae78
parent  b0f1cc2a8d6ab02f2fc7485ad3c25b87319443e4
...
1
 
2
3
4
5
 
 
 
 
6
7
8
...
 
1
2
 
 
 
3
4
5
6
7
8
9
0
@@ -1,8 +1,9 @@
0
-== 0.1.3 2008-08-XX
0
+== 0.1.3 2008-08-22
0
 
0
-* 1 major enhancement:
0
-  * Added : http_proxy key for setting proxy server and port
0
-  * Added : raises exception when http error occurs
0
+* 3 major enhancements:
0
+  * Added http_proxy key for setting proxy server and port (francxk@gmail.com)
0
+  * Now raises exception when http error occurs (francxk@gmail.com)
0
+  * Changed auto format detection from file extension to response content type (Jay Pignata)
0
 
0
 == 0.1.2 2008-08-09
0
 
...
8
9
10
11
12
13
14
...
8
9
10
 
11
12
13
0
@@ -8,7 +8,6 @@ module AAWS
0
     include HTTParty
0
     base_uri 'http://ecs.amazonaws.com'
0
     default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch', :SearchIndex => 'Books'
0
-    format :xml
0
     
0
     def initialize(key)
0
       self.class.default_params :AWSAccessKeyId => key
...
6
7
8
9
10
11
12
...
6
7
8
 
9
10
11
0
@@ -6,7 +6,6 @@ config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))
0
 class Delicious
0
   include HTTParty
0
   base_uri 'https://api.del.icio.us/v1'
0
-  format :xml
0
   
0
   def initialize(u, p)
0
     @auth = {:username => u, :password => p}
...
4
5
6
7
8
9
10
11
...
4
5
6
 
7
8
9
10
0
@@ -4,7 +4,6 @@ require 'pp'
0
 
0
 class Rep
0
   include HTTParty
0
-  format :xml
0
 end
0
 
0
 puts Rep.get('http://whoismyrepresentative.com/whoismyrep.php?zip=46544').inspect
0
\ No newline at end of file
...
12
13
14
 
 
15
16
17
18
19
20
21
 
22
23
24
...
64
65
66
67
68
 
69
70
71
...
111
112
113
114
115
116
117
118
119
...
129
130
131
132
 
 
133
134
 
135
136
137
...
143
144
145
146
 
147
148
 
149
150
151
...
158
159
160
161
162
163
164
165
166
167
 
 
 
 
168
169
170
171
...
12
13
14
15
16
17
18
19
20
 
 
 
21
22
23
24
...
64
65
66
 
 
67
68
69
70
...
110
111
112
 
 
 
113
114
115
...
125
126
127
 
128
129
130
 
131
132
133
134
...
140
141
142
 
143
144
 
145
146
147
148
...
155
156
157
 
 
 
 
 
 
 
158
159
160
161
162
163
164
165
0
@@ -12,13 +12,13 @@ dir = File.expand_path(File.join(File.dirname(__FILE__), 'httparty'))
0
 require dir + '/core_ext'
0
   
0
 module HTTParty
0
+  class UnsupportedFormat < StandardError; end
0
+  
0
   def self.included(base)
0
     base.extend ClassMethods
0
   end
0
   
0
-  class UnsupportedFormat < StandardError; end
0
-  
0
-  AllowedFormats = %w[xml json]
0
+  AllowedFormats = {:xml => 'text/xml', :json => 'application/json'}
0
   
0
   module ClassMethods    
0
     #
0
@@ -64,8 +64,7 @@ module HTTParty
0
     end
0
     
0
     def format(f)
0
-      f = f.to_s
0
-      raise UnsupportedFormat, "Must be one of: #{AllowedFormats.join(', ')}" unless AllowedFormats.include?(f)
0
+      raise UnsupportedFormat, "Must be one of: #{AllowedFormats.keys.join(', ')}" unless AllowedFormats.key?(f)
0
       @format = f
0
     end
0
     
0
@@ -111,9 +110,6 @@ module HTTParty
0
         raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
0
         raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
0
         raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
0
-        # we always want path that begins with /
0
-        path           = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
0
-        @format      ||= format_from_path(path)
0
         uri            = URI.parse("#{base_uri}#{path}")
0
         existing_query = uri.query ? "#{uri.query}&" : ''
0
         uri.query      = if options[:query].blank?
0
@@ -129,9 +125,10 @@ module HTTParty
0
         # note to self: self, do not put basic auth above headers because it removes basic auth
0
         request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
0
         response       = http(uri).request(request)
0
-
0
+        @format      ||= format_from_mimetype(response['content-type'])
0
+        
0
         case response
0
-        when Net::HTTPSuccess     then
0
+        when Net::HTTPSuccess
0
           parse_response(response.body)
0
         else
0
           response.instance_eval { class << self; attr_accessor :body_parsed; end }
0
@@ -143,9 +140,9 @@ module HTTParty
0
       
0
       def parse_response(body) #:nodoc:
0
         case @format
0
-        when 'xml'
0
+        when :xml
0
           Hash.from_xml(body)
0
-        when 'json'
0
+        when :json
0
           ActiveSupport::JSON.decode(body)
0
         else
0
           # just return the response if no format 
0
@@ -158,13 +155,10 @@ module HTTParty
0
         str =~ /^https?:\/\// ? str : "http#{'s' if str.include?(':443')}://#{str}"
0
       end
0
       
0
-      # Returns a format that we can handle from the path if possible. 
0
-      # Just does simple pattern matching on file extention:
0
-      #   /foobar.xml => 'xml'
0
-      #   /foobar.json => 'json'
0
-      def format_from_path(path) #:nodoc:
0
-        ext = File.extname(path)[1..-1]
0
-        !ext.blank? && AllowedFormats.include?(ext) ? ext : nil
0
+      # Uses the HTTP Content-Type header to determine the format of the response
0
+      # It compares the MIME type returned to the types stored in the AllowedFormats hash
0
+      def format_from_mimetype(mimetype) #:nodoc:
0
+        AllowedFormats.each { |k, v| return k if mimetype.include?(v) }
0
       end
0
   end
0
 end
0
\ No newline at end of file

Comments