syncable_models 0.0.1 → 0.0.2
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/generators/syncable_models/import_config_generator.rb +10 -0
- data/lib/generators/syncable_models/templates/initializer.rb +10 -0
- data/lib/syncable_models/controller.rb +1 -1
- data/lib/syncable_models/importer.rb +74 -0
- data/lib/syncable_models/version.rb +1 -1
- data/lib/syncable_models.rb +2 -0
- data/test/dummy/log/test.log +7 -7
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MmJiMWMzNDUxZmY4NzJjMjBiM2RkYjI1OGMyYWQzMjE5ZjMyYjUyMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmMyNDkxZWQyNjBiNjA2MDg5MTVjZDM4ZTUzMDMzMTNjOTFmNWJkMA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjQxZDVhZjJjMTQ4ZDAzYTAxMGQ0MTJjYzUzZTdkYTE1NmZmZWYzNzEzNjlk
|
10
|
+
ZjFjMTE3OTk0MTk5OGI1NTY2NDMyZDQxNGI1ZGQ1MjY4MTM2ZDQ5NWI4ZTEw
|
11
|
+
MmU1MjA5OWE3ODk1ZTRkMmZlN2Y5MDM2YWU2OTExYmY0NmRlMzI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NmQ1OTNjODExODRlM2MyZDViMTcyODczM2U2ZDQ2MWM4N2VmMTQzMzhjNzE5
|
14
|
+
ZWEwYzk3YjczNDU4YmYwYjc4NmMwYjc3MGYxYzcwYjFkYmQwM2U5OTU0YWM3
|
15
|
+
MTM0YzAwMTI0Njc3OTBjZWVkNzg5MjIwNThiODkzMjg5NzU0NmM=
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module SyncableModels
|
2
|
+
class ImportConfigGenerator < Rails::Generators::Base
|
3
|
+
source_root(File.expand_path(File.dirname(__FILE__)))
|
4
|
+
|
5
|
+
|
6
|
+
def copy_initializer_file
|
7
|
+
copy_file 'templates/initializer.rb', 'config/initializers/syncable_models.rb'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
SyncableModels::Importer.config do |conf|
|
2
|
+
## Define one or several imports here
|
3
|
+
# conf.add_import do |import|
|
4
|
+
# import.api_url = 'http://www.example.com/import_api/'
|
5
|
+
# import.destination = :my_awesome_project
|
6
|
+
|
7
|
+
# import.import_model 'Project', id_key: :uuid
|
8
|
+
# import.import_model 'Team', fetch_path: 'some_teams', sync_path: 'sync_some_teams'
|
9
|
+
# end
|
10
|
+
end
|
@@ -27,7 +27,7 @@ module SyncableModels::Controller
|
|
27
27
|
if params[:destination]
|
28
28
|
count = params[:count].present? ? params[:count].to_i : BATCH_COUNT
|
29
29
|
result = klass.not_synced(params[:destination]).limit(count).map(&:to_import_hash)
|
30
|
-
render json: { status: 200,
|
30
|
+
render json: { status: 200, objects: result }
|
31
31
|
else
|
32
32
|
render_argument_error
|
33
33
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module SyncableModels::Importer
|
4
|
+
class Import
|
5
|
+
attr_accessor :api_url, :destination
|
6
|
+
attr_reader :models
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@models = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def import_model(class_name, args = {})
|
13
|
+
class_name = class_name.to_s if class_name.is_a?(Class)
|
14
|
+
fetch_path = args[:fetch_path] || class_name.underscore.pluralize
|
15
|
+
sync_path = args[:sync_path] || "sync_" + class_name.underscore.pluralize
|
16
|
+
id_key = args[:id_key] || :uuid
|
17
|
+
@models[class_name] = {
|
18
|
+
fetch_path: fetch_path,
|
19
|
+
sync_path: sync_path,
|
20
|
+
id_key: id_key
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def api_url
|
25
|
+
@api_url[-1] == '/' ? @api_url : @api_url + "/"
|
26
|
+
end
|
27
|
+
|
28
|
+
def import
|
29
|
+
@models.each do |model_name, params|
|
30
|
+
fetch_url = self.api_url + params[:fetch_path]
|
31
|
+
sync_url = self.api_url + params[:sync_path]
|
32
|
+
|
33
|
+
conn = Faraday.new(url: fetch_url)
|
34
|
+
response = conn.get '', destination: self.destination
|
35
|
+
|
36
|
+
if response.success?
|
37
|
+
response = JSON.parse(response.body)
|
38
|
+
|
39
|
+
if response['objects'] && response['objects'].count > 0
|
40
|
+
klass = model_name.constantize
|
41
|
+
synced_ids = []
|
42
|
+
|
43
|
+
response['objects'].each do |o|
|
44
|
+
result = klass.from_import_hash(o)
|
45
|
+
synced_ids << o[params[:id_key].to_s] if result
|
46
|
+
end
|
47
|
+
|
48
|
+
if synced_ids.any?
|
49
|
+
conn = Faraday.new(url: sync_url)
|
50
|
+
response = conn.get '', destination: self.destination, ids: synced_ids
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
mattr_accessor :imports
|
59
|
+
@@imports = []
|
60
|
+
|
61
|
+
def self.config
|
62
|
+
yield self
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.add_import
|
66
|
+
import = Import.new
|
67
|
+
yield import
|
68
|
+
@@imports << import
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.import
|
72
|
+
@@imports.each &:import
|
73
|
+
end
|
74
|
+
end
|
data/lib/syncable_models.rb
CHANGED
data/test/dummy/log/test.log
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
[1m[36m (
|
1
|
+
[1m[36m (2.9ms)[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
2
|
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
3
|
-
[1m[36m (
|
3
|
+
[1m[36m (1.8ms)[0m [1mCREATE INDEX "index_syncs_on_destination" ON "syncs" ("destination")[0m
|
4
4
|
[1m[35m (0.1ms)[0m SELECT sql
|
5
5
|
FROM sqlite_master
|
6
6
|
WHERE name='index_syncs_on_destination' AND type='index'
|
@@ -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.6ms)[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
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.1ms)[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.2
|
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-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 4.2.5
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: The gem provides tagged syncing functionality and API methods.
|
56
70
|
email:
|
57
71
|
- serafim.nenarokov@flant.ru
|
@@ -65,10 +79,13 @@ files:
|
|
65
79
|
- app/models/syncable_models/sync.rb
|
66
80
|
- config/routes.rb
|
67
81
|
- db/migrate/20160225141153_create_syncs.rb
|
82
|
+
- lib/generators/syncable_models/import_config_generator.rb
|
83
|
+
- lib/generators/syncable_models/templates/initializer.rb
|
68
84
|
- lib/syncable_models.rb
|
69
85
|
- lib/syncable_models/active_record.rb
|
70
86
|
- lib/syncable_models/controller.rb
|
71
87
|
- lib/syncable_models/engine.rb
|
88
|
+
- lib/syncable_models/importer.rb
|
72
89
|
- lib/syncable_models/version.rb
|
73
90
|
- lib/tasks/syncable_models_tasks.rake
|
74
91
|
- test/dummy/README.rdoc
|