retailcrm-api 0.0.2 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a899837cb8ddb00dc0f7fdfbb499c7d9aaf1aee6efc60832b518cf0c5c166b44
4
- data.tar.gz: 0f59cfab3576315c86e6e9ddbe5276dd95a320f1ecf298462d832d09f95d9cec
3
+ metadata.gz: 19aca33fa8aabb3a4cd9acffaa7ed004cbd8be0a126ddb8f4c93b162fb60c50a
4
+ data.tar.gz: ea0d5c405d9c6073044a028eea147c207a0bc7ccbb784b02ddd6d6b108e40661
5
5
  SHA512:
6
- metadata.gz: acf05919e79a85406722c780382382d92e70a6f51f6aaf832f8cc09dfe0cbe8bdb2e62d1eec8844c94cd3a1664092c9e8baddf9d2a5e99e69d0808e290c2651b
7
- data.tar.gz: edc3606cd9b6968dadb9fe9ab40069327f6a050dc584c9d34bd984dea718dc0d6d4526e2c81045585169ee03d70e5b980626d991e332d849a063cbcf21fa16f9
6
+ metadata.gz: a10e4ac6b8700f48597a3585a9a0cf5ef2aa646fedddec463fa02c7573f4c1b877335e2bbf6d8e99a76ae10675b4a0c00f858f050d924689774f732f9e45d3f8
7
+ data.tar.gz: 953ae085065c09ab797cef8bdd66cc4465500e43617d8ff1816305ec54755e63f2b47a55a2629eab5fca597d4ad3c200f07bbb2113d9b1418b8514b6ed296568
data/README.markdown CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  API wrapper для RetailCRM
4
4
 
5
- # Установка Ruby
5
+ # Установка
6
6
 
7
7
  ## Ruby
8
8
  $ gem install retailcrm-api
@@ -70,20 +70,105 @@ RetailcrmApi::Request.logger = MyLogger.new
70
70
 
71
71
  ### [Получение списка расходов, удовлетворяющих заданному фильтру](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#get--api-v5-costs)
72
72
  ```ruby
73
- RetailcrmApi::Request.costs.retrieve.body
73
+ params = {
74
+ filter: {
75
+ minSumm: 0
76
+ }
77
+ }
78
+ RetailcrmApi::Request.costs.retrieve(params: params).body
74
79
  # => {:success=>true, :pagination=>{:limit=>20, :totalCount=>296, :currentPage=>1, :totalPageCount=>15}, :costs=>[{:id=>296, :dateFrom=>"2021-07-29", :dateTo=>"2021-07-29", :summ=>13996, :costItem=>"products-purchase-price", :createdAt=>"2021-07-29 00:34:35", :order=>{:id=>36, :number=>"36C"}, :sites=>["b12-skillum-ru"]}]}
75
80
  ```
81
+ ### [Создание расхода](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-costs-create)
82
+ ```ruby
83
+ body = {
84
+ site: "deppa-ru",
85
+ cost: {
86
+ summ: 100,
87
+ costItem: "office-expenses",
88
+ dateFrom: (DateTime.now - 1.day).strftime("%Y-%m-%d"),
89
+ dateTo: DateTime.now.strftime("%Y-%m-%d")
90
+ }
91
+ }
92
+ RetailcrmApi::Request.costs.create(body: body).body
93
+ # => {:success=>true, :id=>297}
94
+ ```
95
+ ### [Пакетное удаление расходов](https://docs.retailcrm.ru/Developers/APIhttps://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-costs-delete)
96
+ ```ruby
97
+ body = {
98
+ ids: [297]
99
+ }
100
+ RetailcrmApi::Request.costs.create(body: body, suffix: "delete").body
101
+ # => {:success=>true, :count=>1, :notRemovedIds=>[]}
102
+ ```
103
+ ### [Пакетная загрузка расходов](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-costs-upload)
104
+ ```ruby
105
+ body = {
106
+ costs: [
107
+ {
108
+ sites: ["deppa-ru"],
109
+ summ: 100,
110
+ costItem: "office-expenses",
111
+ dateFrom: (DateTime.now - 1.day).strftime("%Y-%m-%d"),
112
+ dateTo: DateTime.now.strftime("%Y-%m-%d")
113
+ }
114
+ ]
115
+ }
116
+ RetailcrmApi::Request.costs.create(body: body, suffix: "upload").body
117
+ # => {:success=>true, :uploadedCosts=>[298]}
118
+ ```
119
+ ### [Получение информации о расходе](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#get--api-v5-costs-id)
120
+ ```ruby
121
+ RetailcrmApi::Request.costs(298).retrieve.body
122
+ # => {:success=>true, :cost=>{:id=>298, :dateFrom=>"2021-08-02", :dateTo=>"2021-08-03", :summ=>100, :costItem=>"office-expenses", :createdAt=>"2021-08-03 22:12:34", :sites=>["deppa-ru"]}}
123
+ ```
124
+ ### [Редактирование расхода](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-costs-id-edit)
125
+ ```ruby
126
+ body = {
127
+ site: "deppa-ru",
128
+ cost: {
129
+ summ: 200,
130
+ costItem: "office-expenses",
131
+ dateFrom: (DateTime.now - 1.day).strftime("%Y-%m-%d"),
132
+ dateTo: DateTime.now.strftime("%Y-%m-%d")
133
+ }
134
+ }
135
+ RetailcrmApi::Request.costs(298).update(body: body).body
136
+ # => {:success=>true, :id=>298}
137
+ ```
138
+ ### [Удаление расхода](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-costs-id-delete)
139
+ ```ruby
140
+ RetailcrmApi::Request.costs(298).delete.body
141
+ # => {:success=>true}
142
+ ```
143
+
144
+ ## Пользовательские поля
145
+ ### [Получение списка пользовательских полей, удовлетворяющих заданному фильтру](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#get--api-v5-custom-fields)
146
+ ```ruby
147
+ params = {
148
+ filter: {
149
+ type: "date"
150
+ }
151
+ }
152
+ RetailcrmApi::Request.custom_fields.retrieve(params: params).body
153
+ # =>{:success=>true, :pagination=>{:limit=>20, :totalCount=>0, :currentPage=>1, :totalPageCount=>0}, :customFields=>[]}
154
+ ```
76
155
 
77
156
  ## Клиенты
78
157
  ### [Получение списка клиентов, удовлетворяющих заданному фильтру](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#get--api-v5-customers)
79
158
  ```ruby
80
- RetailcrmApi::Request.customers.retrieve.body
81
- #=> {:success=>true, :pagination=>{:limit=>20, :totalCount=>0, :currentPage=>1, :totalPageCount=>0}, :customers=>[]}
159
+ params = {
160
+ limit: 100,
161
+ filter: {
162
+ sex: "male"
163
+ }
164
+ }
165
+ RetailcrmApi::Request.customers.retrieve(params: params).body
166
+ #=> {:success=>true, :pagination=>{:limit=>100, :totalCount=>0, :currentPage=>1, :totalPageCount=>0}, :customers=>[]}
82
167
  ```
83
168
  ### [Создание клиента](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-customers-create)
84
169
  ```ruby
85
170
  body = {
86
- site: 'deppa',
171
+ site: 'deppa-ru',
87
172
  customer: {
88
173
  externalId: 1,
89
174
  firstName: 'Павел',
@@ -92,4 +177,47 @@ body = {
92
177
  }
93
178
  RetailcrmApi::Request.customers.create(body: body).body
94
179
  #=> {:success=>true, :id=>54}
180
+ ```
181
+ ### [Пакетная загрузка клиентов](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-customers-upload)
182
+ ```ruby
183
+ body = {
184
+ site: 'deppa-ru',
185
+ customers: [
186
+ {
187
+ externalId: 3,
188
+ firstName: 'Имя',
189
+ lastName: 'Фамилия'
190
+ },
191
+ {
192
+ externalId: 4,
193
+ firstName: 'Имя2',
194
+ lastName: 'Фамилия2'
195
+ }
196
+ ]
197
+ }
198
+ RetailcrmApi::Request.customers.create(body: body, suffix: "upload").body
199
+ ```
200
+
201
+ ## Корпоративные клиенты
202
+ ### [Получение списка корпоративных клиентов, удовлетворяющих заданному фильтру](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#get--api-v5-customers-corporate)
203
+ ```ruby
204
+ RetailcrmApi::Request.customers_corporate.retrieve.body
205
+ # => {:success=>true, :pagination=>{:limit=>20, :totalCount=>0, :currentPage=>1, :totalPageCount=>0}, :customersCorporate=>[]}
206
+ ```
207
+
208
+ ### [Пакетная загрузка корпоративных клиентов](https://docs.retailcrm.ru/Developers/API/APIVersions/APIv5#post--api-v5-customers-corporate-upload)
209
+ ```ruby
210
+ body = {
211
+ site: 'deppa-ru"',
212
+ customersCorporate: [
213
+ {
214
+ externalId: 3,
215
+ nickName: 'test1'
216
+ },
217
+ {
218
+ externalId: 4,
219
+ nickName: 'test2'
220
+ }
221
+ ]
222
+ }
95
223
  ```
@@ -9,11 +9,16 @@ module RetailcrmApi
9
9
  validate_api_key
10
10
  begin
11
11
  response = self.rest_client(suffix).post do |request|
12
- configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
12
+ configure_request(request: request, params: params, headers: headers, body: body)
13
13
  end
14
14
  parse_response(response)
15
15
  rescue => e
16
- handle_error(e)
16
+ if e.response.dig(:status) == 403 && first_time
17
+ sleep(0.3.second)
18
+ self.post(params: params, headers: headers, suffix: suffix, body: body, first_time: false)
19
+ else
20
+ handle_error(e)
21
+ end
17
22
  end
18
23
  end
19
24
 
@@ -26,7 +31,12 @@ module RetailcrmApi
26
31
  end
27
32
  parse_response(response)
28
33
  rescue => e
29
- handle_error(e)
34
+ if e.response.dig(:status) == 403 && first_time
35
+ sleep(0.3.second)
36
+ self.get(params: params, headers: headers, first_time: false)
37
+ else
38
+ handle_error(e)
39
+ end
30
40
  end
31
41
  end
32
42
 
@@ -96,11 +106,14 @@ module RetailcrmApi
96
106
  def configure_request(request: nil, params: nil, headers: nil, body: nil)
97
107
  if request
98
108
  request.params.merge!(params) if params
99
- request.headers['Content-Type'] = 'application/json'
109
+ request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
100
110
  request.headers['X-API-KEY'] = "#{self.api_key}"
101
111
  request.headers['User-Agent'] = "RetailCrmApi/#{RetailcrmApi::VERSION} Ruby gem"
102
112
  request.headers.merge!(headers) if headers
103
- request.body = body if body
113
+ if body
114
+ body.each { |k, v| body[k] = MultiJson.dump(v) }
115
+ request.body = URI.encode_www_form(body)
116
+ end
104
117
  request.options.timeout = self.timeout
105
118
  request.options.open_timeout = self.open_timeout
106
119
  end
@@ -6,8 +6,6 @@ module RetailcrmApi
6
6
  DEFAULT_OPEN_TIMEOUT = 60
7
7
 
8
8
  def initialize(api_key: nil, api_endpoint: nil, timeout: nil, open_timeout: nil, proxy: nil, faraday_adapter: nil, symbolize_keys: false, debug: false, logger: nil, test: false)
9
- p "Request initialize #{api_key}"
10
-
11
9
  @path_parts = []
12
10
  @api_key = api_key || self.class.api_key || ENV['RETAILCRM_API_KEY']
13
11
  @api_key = @api_key.strip if @api_key
@@ -25,7 +23,7 @@ module RetailcrmApi
25
23
  end
26
24
 
27
25
  def method_missing(method, *args)
28
- @path_parts << method.to_s.downcase
26
+ @path_parts << method.to_s.gsub("_", "-").downcase
29
27
  @path_parts << args if args.length > 0
30
28
  @path_parts.flatten!
31
29
  self
@@ -47,14 +45,14 @@ module RetailcrmApi
47
45
  @path_parts.join('/')
48
46
  end
49
47
 
50
- def create(params: nil, headers: nil, body: {})
51
- APIRequest.new(builder: self).post(params: params, headers: headers, suffix: 'create', body: body)
48
+ def create(params: nil, headers: nil, body: {}, suffix: 'create')
49
+ APIRequest.new(builder: self).post(params: params, headers: headers, suffix: suffix, body: body)
52
50
  ensure
53
51
  reset
54
52
  end
55
53
 
56
54
  def update(params: nil, headers: nil, body: {})
57
- APIRequest.new(builder: self).post(params: params, headers: headers, suffix: 'update', body: body)
55
+ APIRequest.new(builder: self).post(params: params, headers: headers, suffix: 'edit', body: body)
58
56
  ensure
59
57
  reset
60
58
  end
@@ -65,8 +63,8 @@ module RetailcrmApi
65
63
  reset
66
64
  end
67
65
 
68
- def delete(params: nil, headers: nil, suffix: 'delete')
69
- APIRequest.new(builder: self).post(params: params, headers: headers)
66
+ def delete(params: nil, headers: nil)
67
+ APIRequest.new(builder: self).post(params: params, headers: headers, suffix: 'delete')
70
68
  ensure
71
69
  reset
72
70
  end
@@ -1,3 +1,3 @@
1
1
  module RetailcrmApi
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: retailcrm-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Osetrov
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 3.2.22
79
+ rubygems_version: 3.1.4
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: RetailCRM API