lib/frenchy/client.rb in frenchy-0.0.6 vs lib/frenchy/client.rb in frenchy-0.0.7
- old
+ new
@@ -29,10 +29,11 @@
raise error
end
# Issue a non-retryable request with the given path and query parameters
+ def patch(path, params); perform(:patch, path, params); end
def post(path, params); perform(:post, path, params); end
def put(path, params); perform(:put, path, params); end
def delete(path, params); perform(:delete, path, params); end
private
@@ -44,17 +45,31 @@
method: method.to_s.upcase,
url: url,
params: params
}
+ headers = {
+ "User-Agent" => "Frenchy/#{Frenchy::VERSION}",
+ "Accept" => "application/json",
+ }
+
+ body = nil
+
+ case method
+ when :patch, :post, :put
+ headers["Content-Type"] = "application/json"
+ body = JSON.generate(params)
+ params = nil
+ end
+
response = begin
- HTTP.accept(:json).send(method, url, params: params).response
+ HTTP.accept(:json).send(method, url, headers: headers, params: params, body: body).response
rescue => exception
raise Frenchy::ServerError, {request: request, error: exception}
end
case response.code
- when 200
+ when 200, 400
begin
JSON.parse(response.body)
rescue => e
raise Frenchy::InvalidResponse, {request: request, error: exception, status: response.status, body: response.body}
end