apisync-rails 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -1
- data/lib/apisync/active_record_extension.rb +0 -1
- data/lib/apisync/rails/model.rb +50 -18
- data/lib/apisync/rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34ddbfb8791d1f24a61b9c7944494f006c2b543a
|
4
|
+
data.tar.gz: ec710fff7c676929cfd11d70d958aa1b56ca768d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd78a4b8aed59f148678befaa4e9e27eb08f32448aedb1a5cfff6783ca6eb74354f3dc494c959043a8f31ca96a73dc80b6af4b9dda46c3416530653835cb2d14
|
7
|
+
data.tar.gz: '091e34cb46df93b256f10b2811548facbd597d02d75dcdbe29fc61fc9d64c760691c3d97601f271923fd993785f3cfae812b1ec023fb0d89a69d5e04b1bc5742'
|
data/README.md
CHANGED
@@ -6,7 +6,8 @@ ApiSync.
|
|
6
6
|
* **DSL:** built-in DSL for ActiveRecord models that pushes your data
|
7
7
|
automatically to apisync.io.
|
8
8
|
* **Sidekiq:** when Sidekiq is present, this gem will push data asynchronously.
|
9
|
-
Otherwise, it will fallback to pushing data synchronously.
|
9
|
+
Otherwise, it will fallback to pushing data synchronously. We strongly recommend
|
10
|
+
using Sidekiq so errors are handled automatically for you.
|
10
11
|
|
11
12
|
If you're not using Rails with ActiveRecord, please use
|
12
13
|
[apisync-ruby](https://github.com/apisync/apisync-ruby) instead.
|
@@ -138,6 +139,20 @@ If you're bypassing methods like `after_commit`,
|
|
138
139
|
no data will be sent to ApiSync. For example, `update_attribute` doesn't
|
139
140
|
perform validations checks, so please use `update_attributes` instead.
|
140
141
|
|
142
|
+
### TooManyRequests
|
143
|
+
|
144
|
+
When too many simultaneous requests are being made, a 429 status code will be
|
145
|
+
returned from the server to indicate to the client to slow down.
|
146
|
+
|
147
|
+
When using Sidekiq, this lib's algorithm will automatically throttle (slow down)
|
148
|
+
the requests. When not using any queue gem, `Apisync::TooManyRequests` exception
|
149
|
+
will be raised and you will have to catch it, therefore
|
150
|
+
**we strongly recommend using Sidekiq**.
|
151
|
+
|
152
|
+
Without Sidekiq, an ActiveRecord's `after_commit` callback is used so the exception
|
153
|
+
shouldn't prevent your record from being saved to the database, but you have
|
154
|
+
to rescue it manually.
|
155
|
+
|
141
156
|
## Development
|
142
157
|
|
143
158
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/apisync/rails/model.rb
CHANGED
@@ -18,6 +18,7 @@ class Apisync
|
|
18
18
|
def initialize(model)
|
19
19
|
@model = model
|
20
20
|
@attributes = {}
|
21
|
+
@payload = {}
|
21
22
|
@should_sync = true
|
22
23
|
end
|
23
24
|
|
@@ -27,67 +28,98 @@ class Apisync
|
|
27
28
|
|
28
29
|
def attribute(attr_name, from: nil, value: nil)
|
29
30
|
@attributes.delete(attr_name)
|
30
|
-
@attributes[attr_name] =
|
31
|
+
@attributes[attr_name] = { attr_name: attr_name, from: from, value: value }
|
31
32
|
end
|
32
33
|
|
33
34
|
def custom_attribute(attr_name, from: nil, value: nil, name: nil)
|
34
35
|
@attributes[:custom_attributes] ||= []
|
35
36
|
@attributes[:custom_attributes] << {
|
36
|
-
|
37
|
-
|
38
|
-
value:
|
37
|
+
attr_name: attr_name,
|
38
|
+
from: from,
|
39
|
+
value: value,
|
40
|
+
name: name
|
39
41
|
}
|
40
42
|
end
|
41
43
|
|
42
44
|
def sync
|
43
45
|
if sync?
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
payload = generate_payload
|
47
|
+
payload = set_reference_id(payload)
|
48
|
+
validate!(payload)
|
49
|
+
log_warnings(payload)
|
47
50
|
|
48
51
|
if defined?(::Sidekiq)
|
49
52
|
Apisync::Rails::SyncModelJob::Sidekiq.perform_async(
|
50
53
|
@model.class.name,
|
51
54
|
@model.id,
|
52
|
-
|
55
|
+
payload
|
53
56
|
)
|
54
57
|
else
|
55
58
|
Apisync::Rails::Http.post(
|
56
|
-
|
59
|
+
payload,
|
57
60
|
request_concurrency: :synchronous
|
58
61
|
)
|
59
62
|
end
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
63
|
-
def validate!
|
66
|
+
def validate!(payload)
|
64
67
|
return unless sync?
|
65
68
|
|
66
69
|
REQUIRED_ATTRS.each do |attr, message|
|
67
|
-
if
|
68
|
-
raise MissingAttribute, "Please specify #{attr}. #{message}"
|
70
|
+
if payload[attr].blank?
|
71
|
+
raise MissingAttribute, "Please specify '#{attr}'. #{message}"
|
69
72
|
end
|
70
73
|
end
|
71
74
|
end
|
72
75
|
|
73
|
-
def log_warnings
|
76
|
+
def log_warnings(payload)
|
74
77
|
WARNING_ATTRS.each do |attr, message|
|
75
|
-
if
|
76
|
-
::Rails.logger.warn "Please specify #{attr}. #{message}"
|
78
|
+
if payload[attr].blank?
|
79
|
+
::Rails.logger.warn "Please specify '#{attr}'. #{message}"
|
77
80
|
end
|
78
81
|
end
|
79
82
|
end
|
80
83
|
|
81
84
|
private
|
82
85
|
|
86
|
+
def generate_payload
|
87
|
+
@payload = {}
|
88
|
+
@attributes.each do |attr, properties|
|
89
|
+
if attr == :custom_attributes
|
90
|
+
custom_attrs = []
|
91
|
+
properties.each do |custom_attr|
|
92
|
+
from = custom_attr[:from]
|
93
|
+
value = custom_attr[:value]
|
94
|
+
attr_name = custom_attr[:attr_name]
|
95
|
+
name = custom_attr[:name]
|
96
|
+
|
97
|
+
custom_attrs << {
|
98
|
+
name: localized_name(name),
|
99
|
+
identifier: attr_name.to_s,
|
100
|
+
value: attr_value(attr_name, from: from, value: value)
|
101
|
+
}
|
102
|
+
end
|
103
|
+
@payload[:custom_attributes] = custom_attrs
|
104
|
+
else
|
105
|
+
from = properties[:from]
|
106
|
+
value = properties[:value]
|
107
|
+
@payload[attr] = attr_value(attr, from: from, value: value)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
@payload
|
112
|
+
end
|
113
|
+
|
83
114
|
def sync?
|
84
115
|
@should_sync
|
85
116
|
end
|
86
117
|
|
87
|
-
def set_reference_id
|
88
|
-
if
|
89
|
-
|
118
|
+
def set_reference_id(payload)
|
119
|
+
if payload[:reference_id].blank? && @model.id.present?
|
120
|
+
payload[:reference_id] = @model.id.to_s
|
90
121
|
end
|
122
|
+
payload
|
91
123
|
end
|
92
124
|
|
93
125
|
def attr_value(attr_name, from:, value:)
|
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.0.
|
4
|
+
version: 0.0.4
|
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: 2017-10-
|
11
|
+
date: 2017-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|