lib/zephyr.rb in zephyr-1.0.5 vs lib/zephyr.rb in zephyr-1.0.6
- old
+ new
@@ -43,11 +43,11 @@
end
def default_headers
{
'Accept' => 'application/json;q=0.7, */*;q=0.5',
- 'User-Agent' => 'lib-http.rb',
+ 'User-Agent' => 'zephyr',
}
end
# Performs a HEAD request to the specified resource.
#
@@ -85,19 +85,11 @@
# The same thing as #get, but decodes the response entity as JSON (if it's
# application/json) and adds it under the :json key in the returned hash.
def get_json(expected_statuses, timeout, path_components, headers={}, yajl_opts={})
response = get(expected_statuses, timeout, path_components, headers)
-
- content_type = response[:headers]['content-type']
- content_type = content_type.first if content_type.respond_to?(:first)
-
- if content_type.to_s.strip.match /^application\/json/
- response[:json] = Yajl::Parser.parse(response[:body], yajl_opts)
- end
-
- response
+ create_json_response(response, yajl_opts)
end
# Performs a PUT request to the specified resource.
#
# A request to /users/#{@user.id}/things?q=woof with an Content-Type header of
@@ -117,11 +109,12 @@
end
# The same thing as #put, but encodes the entity as JSON and specifies
# "application/json" as the request entity content type.
def put_json(expected_statuses, timeout, path_components, entity, headers={})
- put(expected_statuses, timeout, path_components, Yajl::Encoder.encode(entity), headers.merge("Content-Type" => "application/json"))
+ response = put(expected_statuses, timeout, path_components, Yajl::Encoder.encode(entity), headers.merge("Content-Type" => "application/json"))
+ create_json_response(response)
end
# Performs a POST request to the specified resource.
#
# A request to /users/#{@user.id}/things?q=woof with an Content-Type header of
@@ -141,17 +134,18 @@
end
# The same thing as #post, but encodes the entity as JSON and specifies
# "application/json" as the request entity content type.
def post_json(expected_statuses, timeout, path_components, entity, headers={})
- post(
- expected_statuses,
- timeout,
- path_components,
- Yajl::Encoder.encode(entity),
- headers.merge("Content-Type" => "application/json")
- )
+ response = post(
+ expected_statuses,
+ timeout,
+ path_components,
+ Yajl::Encoder.encode(entity),
+ headers.merge("Content-Type" => "application/json")
+ )
+ create_json_response(response)
end
# Performs a DELETE request to the specified resource.
#
# A request to /users/#{@user.id}/things?q=woof which is expecting a 204 No
@@ -306,9 +300,21 @@
h.each_capitalized do |k,v|
arr << "#{k}: #{v}"
end
end
end
+
+ def create_json_response(response, yajl_opts = {})
+ return response if response.nil? || !response.key?(:headers) || !response[:headers].key?('content-type')
+ content_type = response[:headers]['content-type']
+ content_type = content_type.first if content_type.respond_to?(:first)
+
+ if content_type.to_s.strip.match /^application\/json/
+ response[:json] = Yajl::Parser.parse(response[:body], yajl_opts)
+ end
+ response
+ end
+
end
# Represents headers from an HTTP request or response.
# Used internally by HTTP backends for processing headers.
class Headers