francxk-httparty 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/httparty.gemspec +1 -1
- data/lib/httparty.rb +15 -5
- data/lib/httparty/version.rb +1 -1
- metadata +1 -1
data/History.txt
CHANGED
data/httparty.gemspec
CHANGED
data/lib/httparty.rb
CHANGED
@@ -18,7 +18,17 @@ module HTTParty
|
|
18
18
|
base.extend ClassMethods
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
# this is the format known by the module
|
22
|
+
AllowedFormats = [:xml, :json]
|
23
|
+
|
24
|
+
# for each mime types give the format found in AllowedFormat (that can be parsed)
|
25
|
+
MimeTypes = {
|
26
|
+
'application/xml' => :xml,
|
27
|
+
'application/atom+xml' => :xml,
|
28
|
+
'application/rdf+xml' => :xml,
|
29
|
+
'text/xml' => :xml,
|
30
|
+
'application/json' => :json,
|
31
|
+
'text/json' => :json, }
|
22
32
|
|
23
33
|
module ClassMethods
|
24
34
|
#
|
@@ -64,7 +74,7 @@ module HTTParty
|
|
64
74
|
end
|
65
75
|
|
66
76
|
def format(f)
|
67
|
-
raise UnsupportedFormat, "Must be one of: #{AllowedFormats.
|
77
|
+
raise UnsupportedFormat, "Must be one of: #{AllowedFormats.join(', ')}" unless AllowedFormats.include?(f)
|
68
78
|
@format = f
|
69
79
|
end
|
70
80
|
|
@@ -126,7 +136,7 @@ module HTTParty
|
|
126
136
|
request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
|
127
137
|
response = http(uri).request(request)
|
128
138
|
|
129
|
-
@format ||= format_from_mimetype(response['content-type'])
|
139
|
+
@format ||= format_from_mimetype(response['content-type'].split(';',2)[0])
|
130
140
|
|
131
141
|
case response
|
132
142
|
when Net::HTTPSuccess
|
@@ -157,9 +167,9 @@ module HTTParty
|
|
157
167
|
end
|
158
168
|
|
159
169
|
# Uses the HTTP Content-Type header to determine the format of the response
|
160
|
-
# It compares the MIME type returned to the types stored in the
|
170
|
+
# It compares the MIME type returned to the types stored in the MimeType hashto choose format (ie AloowedFormat)
|
161
171
|
def format_from_mimetype(mimetype) #:nodoc:
|
162
|
-
|
172
|
+
MimeTypes[mimetype]
|
163
173
|
end
|
164
174
|
end
|
165
175
|
end
|
data/lib/httparty/version.rb
CHANGED