lib/httparty.rb in francxk-httparty-0.1.3 vs lib/httparty.rb in francxk-httparty-0.1.4
- old
+ new
@@ -16,12 +16,22 @@
def self.included(base)
base.extend ClassMethods
end
- AllowedFormats = {:xml => 'text/xml', :json => 'application/json'}
+ # this is the format known by the module
+ AllowedFormats = [:xml, :json]
+ # for each mime types give the format found in AllowedFormat (that can be parsed)
+ MimeTypes = {
+ 'application/xml' => :xml,
+ 'application/atom+xml' => :xml,
+ 'application/rdf+xml' => :xml,
+ 'text/xml' => :xml,
+ 'application/json' => :json,
+ 'text/json' => :json, }
+
module ClassMethods
#
# Set an http proxy
#
# class Twitter
@@ -62,11 +72,11 @@
return @headers if h.blank?
@headers.merge!(h)
end
def format(f)
- raise UnsupportedFormat, "Must be one of: #{AllowedFormats.keys.join(', ')}" unless AllowedFormats.key?(f)
+ raise UnsupportedFormat, "Must be one of: #{AllowedFormats.join(', ')}" unless AllowedFormats.include?(f)
@format = f
end
# TODO: spec out this
def get(path, options={})
@@ -124,11 +134,11 @@
request.initialize_http_header headers.merge(options[:headers] || {})
# note to self: self, do not put basic auth above headers because it removes basic auth
request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
response = http(uri).request(request)
- @format ||= format_from_mimetype(response['content-type'])
+ @format ||= format_from_mimetype(response['content-type'].split(';',2)[0])
case response
when Net::HTTPSuccess
parse_response(response.body)
else
@@ -155,11 +165,11 @@
def normalize_base_uri(str) #:nodoc:
str =~ /^https?:\/\// ? str : "http#{'s' if str.include?(':443')}://#{str}"
end
# Uses the HTTP Content-Type header to determine the format of the response
- # It compares the MIME type returned to the types stored in the AllowedFormats hash
+ # It compares the MIME type returned to the types stored in the MimeType hashto choose format (ie AloowedFormat)
def format_from_mimetype(mimetype) #:nodoc:
- AllowedFormats.each { |k, v| return k if mimetype.include?(v) }
+ MimeTypes[mimetype]
end
end
end
\ No newline at end of file