syncable_models 0.0.7 → 0.0.8
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 +8 -8
- data/lib/syncable_models/controller.rb +28 -17
- data/lib/syncable_models/version.rb +1 -1
- data/lib/syncable_models_importer.rb +13 -3
- data/test/dummy/log/test.log +10 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OGIwODg1OTU1NDI2MThhYzNjNDVjZWU1OWUyMzcwMDI2OGY1MmRhZg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MzczYzA4ZDJlODQwMGYwZTE1OGE0MWViOTMwMGZjZmFjZDM2YTJkMw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzcyMWE4MjEwY2IzOTgwOGViZDMzNWQ2ZDc4OTc5YjlhYmEzZTA3OGVkM2Ji
|
10
|
+
YjcxYWRjN2RjNzg1MTE2MzJlYmE4ZjM1ZDhkNjVlOTBkOGU5ODE4YTE0ZGYx
|
11
|
+
MzY5OTQ2MzZlOTBjMWNiZDliMWYyNGFlNTllMDhhMjNkYWQxOTA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZmY3YmRiYWZjOTE4OThjODFiZTZiYWEzMjY4ZTNmMGQ0YjAwZmZiYmIyYzRh
|
14
|
+
ZWQ5OWYxZWQ4NzdiZTY5NzNiMDJiNTE2ZGJjMDRmMWNiMWRmZTg5ZDMxZjkw
|
15
|
+
MjFmZmRkZWY2MDg3OTM1NDUwNjY1OGY4M2NlZWIwZWJkNzlkODU=
|
@@ -4,23 +4,28 @@ module SyncableModels::Controller
|
|
4
4
|
BATCH_COUNT = 50
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
7
|
+
def sync_for(subject, class_name: nil, sync_method: nil, fetch_method: nil)
|
8
|
+
klass = class_name || subject.to_s.singularize.camelize
|
9
|
+
klass = klass.constantize unless klass.is_a?(Class)
|
10
|
+
|
11
|
+
sync_method = sync_method || "sync_#{subject}"
|
12
|
+
fetch_method = fetch_method || "#{subject}"
|
13
|
+
|
14
|
+
class_eval """
|
15
|
+
def #{sync_method}
|
16
|
+
set_synced #{klass.name}
|
17
|
+
end
|
18
|
+
|
19
|
+
def #{fetch_method}
|
20
|
+
fetch #{klass.name}
|
21
|
+
end
|
22
|
+
"""
|
23
|
+
end
|
24
|
+
|
25
|
+
def authorize_by_key(key=nil)
|
26
|
+
define_method(:api_key){ key }
|
27
|
+
before_action :do_authorize_by_key
|
28
|
+
end
|
24
29
|
end
|
25
30
|
|
26
31
|
def fetch(klass)
|
@@ -45,4 +50,10 @@ module SyncableModels::Controller
|
|
45
50
|
def render_argument_error
|
46
51
|
return render json: { status: 400, message: 'Not enough arguments' }
|
47
52
|
end
|
53
|
+
|
54
|
+
def do_authorize_by_key
|
55
|
+
if params[:key] != api_key
|
56
|
+
return render json: { status: 401, message: 'Unauthorized'}
|
57
|
+
end
|
58
|
+
end
|
48
59
|
end
|
@@ -2,7 +2,7 @@ require 'faraday'
|
|
2
2
|
|
3
3
|
module SyncableModelsImporter
|
4
4
|
class Import
|
5
|
-
attr_accessor :api_url, :destination
|
5
|
+
attr_accessor :api_url, :api_key, :destination
|
6
6
|
attr_reader :models
|
7
7
|
|
8
8
|
def initialize
|
@@ -25,29 +25,39 @@ module SyncableModelsImporter
|
|
25
25
|
@api_url[-1] == '/' ? @api_url : @api_url + "/"
|
26
26
|
end
|
27
27
|
|
28
|
+
def params_with_api_key(params)
|
29
|
+
params.merge!(key: api_key) if api_key
|
30
|
+
params
|
31
|
+
end
|
32
|
+
|
28
33
|
def import
|
29
34
|
@models.each do |model_name, params|
|
30
35
|
fetch_url = self.api_url + params[:fetch_path]
|
31
36
|
sync_url = self.api_url + params[:sync_path]
|
32
37
|
|
33
38
|
conn = Faraday.new(url: fetch_url)
|
34
|
-
response = conn.get '', destination:
|
39
|
+
response = conn.get '', params_with_api_key(destination: destination)
|
35
40
|
|
36
41
|
if response.success?
|
37
42
|
response = JSON.parse(response.body)
|
38
43
|
|
44
|
+
if response["status"].to_i == 401
|
45
|
+
puts "[SyncableModelsImporter] Wrong api key!"
|
46
|
+
end
|
47
|
+
|
39
48
|
if response['objects'] && response['objects'].count > 0
|
40
49
|
klass = model_name.constantize
|
41
50
|
synced_ids = []
|
42
51
|
|
43
52
|
response['objects'].each do |o|
|
44
53
|
result = klass.from_import_hash(o)
|
54
|
+
puts "[SyncableModelsImporter] Importing #{model_name} (id=#{o[params[:id_key].to_s]}): #{ result ? 'OK' : 'FAIL' }"
|
45
55
|
synced_ids << o[params[:id_key].to_s] if result
|
46
56
|
end
|
47
57
|
|
48
58
|
if synced_ids.any?
|
49
59
|
conn = Faraday.new(url: sync_url)
|
50
|
-
response = conn.get '', destination: self.destination, ids: synced_ids
|
60
|
+
response = conn.get '', params_with_api_key(destination: self.destination, ids: synced_ids)
|
51
61
|
end
|
52
62
|
end
|
53
63
|
end
|
data/test/dummy/log/test.log
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
[1m[36m (2.
|
2
|
-
[1m[35m (0.
|
3
|
-
[1m[36m (
|
4
|
-
[1m[35m (0.
|
1
|
+
[1m[36m (2.0ms)[0m [1mCREATE TABLE "syncs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "subject_id" integer, "subject_type" varchar, "destination" varchar, "created_at" datetime, "updated_at" datetime) [0m
|
2
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
3
|
+
[1m[36m (1.7ms)[0m [1mCREATE INDEX "index_syncs_on_destination" ON "syncs" ("destination")[0m
|
4
|
+
[1m[35m (0.1ms)[0m SELECT sql
|
5
5
|
FROM sqlite_master
|
6
6
|
WHERE name='index_syncs_on_destination' AND type='index'
|
7
7
|
UNION ALL
|
@@ -9,14 +9,14 @@
|
|
9
9
|
FROM sqlite_temp_master
|
10
10
|
WHERE name='index_syncs_on_destination' AND type='index'
|
11
11
|
|
12
|
-
[1m[36m (
|
13
|
-
[1m[35m (
|
14
|
-
[1m[36m (
|
12
|
+
[1m[36m (1.6ms)[0m [1mCREATE INDEX "index_syncs_on_subject_id_and_subject_type_and_destination" ON "syncs" ("subject_id", "subject_type", "destination")[0m
|
13
|
+
[1m[35m (1.6ms)[0m CREATE TABLE "schema_migrations" ("version" varchar NOT NULL)
|
14
|
+
[1m[36m (1.8ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
15
15
|
[1m[35m (0.1ms)[0m SELECT version FROM "schema_migrations"
|
16
|
-
[1m[36m (
|
16
|
+
[1m[36m (1.3ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20160225141153')[0m
|
17
17
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
18
|
-
[1m[35m (0.
|
18
|
+
[1m[35m (0.1ms)[0m begin transaction
|
19
19
|
------------------------------
|
20
20
|
SyncableModelsTest: test_truth
|
21
21
|
------------------------------
|
22
|
-
[1m[36m (0.
|
22
|
+
[1m[36m (0.0ms)[0m [1mrollback transaction[0m
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syncable_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Serafim Nenarokov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|