lib/moysklad/client.rb in moysklad-0.4.4 vs lib/moysklad/client.rb in moysklad-0.4.5
- old
+ new
@@ -1,8 +1,9 @@
require 'faraday'
require 'faraday/detailed_logger'
require 'faraday_curl'
+require 'faraday/gzip'
require_relative 'client/errors'
class Moysklad::Client
URL ='https://api.moysklad.ru/api/remap/1.2/'
@@ -22,50 +23,55 @@
end
end
end
def download(path, filename)
- response = client.get path
+ response = client.get path do |req|
+ req.request :gzip
+ end
File.open(filename, 'wb') { |fp| fp.write(response.body) }
end
def get path, params={}
logger.debug "Client: GET #{path} #{params}"
- parse_response client.get path, params
+ result = client.get path, params do |req|
+ req.request :gzip
+ end
+ parse_response result
end
def post path, data
logger.debug "Client: POST #{path}"
result = client.post do |req|
req.url path
+ req.request :gzip
req.headers['Content-Type'] = 'application/json'
req.headers['Accept'] = '*/*'
- req.headers['Accept-Encoding'] = 'gzip'
req.body = data
end
parse_response result
end
def put path, data
logger.debug "Client: PUT #{path}"
result = client.put do |req|
+ req.request :gzip
req.url path
req.headers['Content-Type'] = 'application/json'
req.headers['Accept'] = '*/*'
- req.headers['Accept-Encoding'] = 'gzip'
req.body = data
end
parse_response result
end
def delete path
logger.debug "Client: DELETE #{path}"
result = client.delete do |req|
+ req.request :gzip
req.url path
req.headers['Content-Type'] = 'application/json'
req.headers['Accept'] = '*/*'
- req.headers['Accept-Encoding'] = 'gzip'
end
parse_response result
end
private
@@ -75,10 +81,10 @@
def logger
Moysklad.logger
end
def parse_response res
- body = (res.body || '').force_encoding('utf-8')
+ body = res.body
Moysklad.logger.debug "Response [#{res.status}] with body #{body}"
if res.status == 200
return if body.blank?
JSON.parse body