apisync-rails 0.0.4 → 0.1.1
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 +4 -4
- data/README.md +12 -0
- data/apisync-rails.gemspec +1 -1
- data/lib/apisync/rails/http.rb +27 -6
- data/lib/apisync/rails/model.rb +2 -0
- data/lib/apisync/rails/sync_model_job/sidekiq.rb +5 -1
- data/lib/apisync/rails/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a583b9bcbe4c6e0dafb2d339832e45f00d10940c
|
4
|
+
data.tar.gz: 767adc9866af63552c5bf288280a265e85e27a81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz: '
|
6
|
+
metadata.gz: 0e10811ce56132875c6c302fe78575536f914237ced8a63aad970aea767f4862f4e1b09acdadcee083ddbd4675997060cd7aa746db6f5767e0a983cb4ca84f45
|
7
|
+
data.tar.gz: '08476e75fd97d3068103a344304359fe7ff00eade77e99d63038f333fab71564dce0962272353edd76065df2f6d63d8653388c4ebe9d34f279fd14e7caf38d2f'
|
data/README.md
CHANGED
@@ -100,6 +100,11 @@ class Product < ActiveRecord::Base
|
|
100
100
|
end
|
101
101
|
```
|
102
102
|
|
103
|
+
**Step 3: test on the console:** on the Rails console, try saving the model.
|
104
|
+
The request and response should show up.
|
105
|
+
|
106
|
+
You can turn off these logs, use `Apisync.verbose = false`.
|
107
|
+
|
103
108
|
**Explanation**
|
104
109
|
|
105
110
|
**sync_if** defines if the item should be synchronized. The method with the
|
@@ -132,6 +137,13 @@ over and over.
|
|
132
137
|
By default, `reference_id` is set to whatever **id** value is. You can
|
133
138
|
customize it, e.g `attribute :reference_id, from: :my_custom_id`.
|
134
139
|
|
140
|
+
### Failed requests
|
141
|
+
|
142
|
+
A `warning` will be logged (`Rails.logger.warn`) whenever a request fails.
|
143
|
+
|
144
|
+
In asynchronous cases (e.g Sidekiq), an exception will be raised
|
145
|
+
(`Apisync::RequestFailed`) so jobs can be retried.
|
146
|
+
|
135
147
|
### Note on callbacks
|
136
148
|
|
137
149
|
This gem uses Rails' callbacks to trigger synchronization.
|
data/apisync-rails.gemspec
CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
|
24
24
|
spec.add_dependency "activesupport", ">= 4.2"
|
25
25
|
spec.add_dependency "activerecord", ">= 4.2"
|
26
|
-
spec.add_dependency "apisync", ">= 0.
|
26
|
+
spec.add_dependency "apisync", ">= 0.2.3"
|
27
27
|
|
28
28
|
spec.add_development_dependency "bundler", "~> 1.15"
|
29
29
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/apisync/rails/http.rb
CHANGED
@@ -7,10 +7,10 @@ class Apisync
|
|
7
7
|
|
8
8
|
if ::Rails.respond_to?(:gem_version)
|
9
9
|
rails_version = ::Rails.gem_version.to_s
|
10
|
-
elsif Rails::VERSION.is_a?(String)
|
11
|
-
rails_version = Rails::VERSION
|
12
|
-
elsif Rails::VERSION::STRING
|
13
|
-
rails_version = Rails::VERSION::STRING
|
10
|
+
elsif ::Rails::VERSION.is_a?(String)
|
11
|
+
rails_version = ::Rails::VERSION
|
12
|
+
elsif ::Rails::VERSION::STRING
|
13
|
+
rails_version = ::Rails::VERSION::STRING
|
14
14
|
end
|
15
15
|
|
16
16
|
headers["X-Framework"] = "Ruby on Rails #{rails_version}"
|
@@ -23,8 +23,29 @@ class Apisync
|
|
23
23
|
headers["X-TooManyRequests-Attempts"] = too_many_requests_attempts.to_s
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
if Apisync.logger.nil?
|
27
|
+
Apisync.logger = ::Rails.logger
|
28
|
+
end
|
29
|
+
|
30
|
+
if Apisync.verbose.nil?
|
31
|
+
verbose = ::Rails.env.development?
|
32
|
+
else
|
33
|
+
verbose = Apisync.verbose
|
34
|
+
end
|
35
|
+
|
36
|
+
client = Apisync.new(verbose: verbose)
|
37
|
+
response = client.inventory_items.save(attributes: attrs, headers: headers)
|
38
|
+
|
39
|
+
unless response.success?
|
40
|
+
reference_id_msg = ""
|
41
|
+
if attrs["reference_id"].present?
|
42
|
+
reference_id_msg = "with reference_id '#{attrs[:reference_id]}' "
|
43
|
+
end
|
44
|
+
|
45
|
+
::Rails.logger.warn "[APISync] Request #{reference_id_msg}failed: #{response.body}"
|
46
|
+
end
|
47
|
+
|
48
|
+
response
|
28
49
|
end
|
29
50
|
end
|
30
51
|
end
|
data/lib/apisync/rails/model.rb
CHANGED
@@ -15,13 +15,17 @@ class Apisync
|
|
15
15
|
end
|
16
16
|
|
17
17
|
begin
|
18
|
-
Apisync::Rails::Http.post(
|
18
|
+
response = Apisync::Rails::Http.post(
|
19
19
|
attributes,
|
20
20
|
request_concurrency: :asynchronous,
|
21
21
|
concurrency_lib: "Sidekiq #{::Sidekiq::VERSION}",
|
22
22
|
too_many_requests_attempts: attempt.to_s
|
23
23
|
)
|
24
24
|
|
25
|
+
unless response.success?
|
26
|
+
raise Apisync::RequestFailed, "[apisync] Request failed: #{response.body}"
|
27
|
+
end
|
28
|
+
|
25
29
|
# When there are too many requests and ApiSync's API cannot take it,
|
26
30
|
# this algorithm will push this job to be retried in the future.
|
27
31
|
rescue Apisync::TooManyRequests
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apisync-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexandre de Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.
|
47
|
+
version: 0.2.3
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.
|
54
|
+
version: 0.2.3
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: bundler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|