Sha256: 8011cc8f17fe75620403755cea0a46ae4edd1b24dcada4d5e5d6cc22640004b8
Contents?: true
Size: 1.77 KB
Versions: 2
Compression:
Stored size: 1.77 KB
Contents
require 'faraday' require_relative 'client/errors' class Moysklad::Client # URL = 'https://online.moysklad.ru' URL ='https://online.moysklad.ru/api/remap/1.1/' def initialize login: nil, password: nil @client = Faraday.new URL do |conn| if Faraday::VERSION.split('.').first.to_i < 2 conn.request(:basic_auth, login, password) else conn.request(:authorization, :basic, login, password) end end end def download(path, filename) response = client.get path 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 end def post path, data logger.debug "Client: POST #{path}" result = client.post do |req| req.url path 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.url path req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = '*/*' req.body = data end parse_response result end def delete path logger.debug "Client: DELETE #{path}" result = client.delete do |req| req.url path req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = '*/*' end parse_response result end private attr_reader :client def logger Moysklad.logger end def parse_response res Moysklad.logger.debug "Response [#{res.status}]: #{res.body}" if res.status == 200 return if res.body.blank? JSON.parse res.body else Moysklad::Client::Errors.build res end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
moysklad-0.3.3 | lib/moysklad/client.rb |
moysklad-0.3.2 | lib/moysklad/client.rb |