Class: Moysklad::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/moysklad/client.rb,
lib/moysklad/client/errors.rb,
lib/moysklad/client/errors.rb

Defined Under Namespace

Classes: BadGatewayError, Error, Errors, HtmlParsedError, InternalServerError, JsonParsedError, MethodNotAllowedError, NoResourceFound, ParsedError, ParsingError, ResourceForbidden, UnauthorizedError, UnknownError

Constant Summary collapse

URL =
'https://api.moysklad.ru/api/remap/1.2/'

Instance Method Summary collapse

Constructor Details

#initialize(login: nil, password: nil, logger: nil) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/moysklad/client.rb', line 12

def initialize login: nil, password: nil, logger: nil
  @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, , password)
    else
      conn.request(:authorization, :basic, , password)
    end

    conn.adapter Faraday.default_adapter
  end
end

Instance Method Details

#delete(path) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/moysklad/client.rb', line 59

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

#get(path, params = {}) ⇒ Object



32
33
34
35
# File 'lib/moysklad/client.rb', line 32

def get path, params={}
  logger.debug "Client: GET #{path} #{params}"
  parse_response client.get path, params
end

#post(path, data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/moysklad/client.rb', line 37

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

#put(path, data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/moysklad/client.rb', line 48

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