lib/moysklad/client.rb in moysklad-0.4.5 vs lib/moysklad/client.rb in moysklad-0.4.6

- old
+ new

@@ -1,9 +1,10 @@ require 'faraday' require 'faraday/detailed_logger' require 'faraday_curl' require 'faraday/gzip' +require 'faraday/encoding' require_relative 'client/errors' class Moysklad::Client URL ='https://api.moysklad.ru/api/remap/1.2/' @@ -12,51 +13,48 @@ @client = Faraday.new URL do |conn| unless logger.nil? conn.response :detailed_logger, logger conn.request :curl, logger, :info end + conn.request :gzip + conn.response :encoding conn.options.timeout = ENV.fetch('MOYSKLAD_HTTP_TIMEOUT', 120) if Faraday::VERSION.split('.').first.to_i < 2 conn.request(:basic_auth, login, password) else conn.request(:authorization, :basic, login, password) end + + conn.adapter Faraday.default_adapter end end def download(path, filename) - response = client.get path do |req| - req.request :gzip - end + response = client.get path File.open(filename, 'wb') { |fp| fp.write(response.body) } end def get path, params={} logger.debug "Client: GET #{path} #{params}" - result = client.get path, params do |req| - req.request :gzip - end - parse_response result + parse_response client.get path, params 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.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.body = data end @@ -64,11 +62,10 @@ 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'] = '*/*' end parse_response result @@ -82,9 +79,10 @@ Moysklad.logger end def parse_response res body = res.body + body.force_encoding 'utf-8' if res.body.encoding.to_s == "ASCII-8BIT" && res.headers['content-type'].include?('charset=utf-8') Moysklad.logger.debug "Response [#{res.status}] with body #{body}" if res.status == 200 return if body.blank? JSON.parse body