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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDRkNjRiOThjNmRiY2VlN2MyODY4OTNmYjRkM2UxODY1Y2E5YTUwYg==
4
+ MmJiMWMzNDUxZmY4NzJjMjBiM2RkYjI1OGMyYWQzMjE5ZjMyYjUyMg==
5
5
  data.tar.gz: !binary |-
6
- Mzc0Njc4NGQyMmY1ZDFiODAwNWU2ZDAxYzNkNjA5MDliNWE4OTQ1Yg==
6
+ YmMyNDkxZWQyNjBiNjA2MDg5MTVjZDM4ZTUzMDMzMTNjOTFmNWJkMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmExNDRlNDA3YjQ1NjE3NjE0NTEzM2Q5MmIwMTEyOGM2ZDM2MTRmZWQzODA2
10
- N2IwM2VlNTBlYWZlOWQzY2RhZmJiZjYxNmQ5OGNhY2U0MjU4NjEzOTVjMGMz
11
- ZTIzYzc2MzNkOTA1MTcxM2JkNmIwYmRiODQ0ZmZkNDgxMjg5MTg=
9
+ YjQxZDVhZjJjMTQ4ZDAzYTAxMGQ0MTJjYzUzZTdkYTE1NmZmZWYzNzEzNjlk
10
+ ZjFjMTE3OTk0MTk5OGI1NTY2NDMyZDQxNGI1ZGQ1MjY4MTM2ZDQ5NWI4ZTEw
11
+ MmU1MjA5OWE3ODk1ZTRkMmZlN2Y5MDM2YWU2OTExYmY0NmRlMzI=
12
12
  data.tar.gz: !binary |-
13
- YjljYWU5NWU4YzFlOGU1Y2I1NTVhNTZmNGJhMWY5YWNmOTAyMTlmNjk3NjVk
14
- YzFkM2RkYmQyOTg3OWUxMDk3ZDQzYTY0MmQ4YTBiYmM3OGY1NjhiZjAyNThj
15
- MDRlZjQ1YzFkY2U0MzY5MTk4NTM5YzczOTdmMTgxMzViNmE1MDA=
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, clients: result }
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
@@ -1,3 +1,3 @@
1
1
  module SyncableModels
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  require "syncable_models/engine"
2
2
  require "syncable_models/active_record"
3
3
  require "syncable_models/controller"
4
+ require "syncable_models/importer"
4
5
 
5
6
  module SyncableModels
7
+
6
8
  end
@@ -1,6 +1,6 @@
1
-  (5457.2ms) CREATE TABLE "syncs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "subject_id" integer, "subject_type" varchar, "destination" varchar, "created_at" datetime, "updated_at" datetime) 
1
+  (2.9ms) CREATE TABLE "syncs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "subject_id" integer, "subject_type" varchar, "destination" varchar, "created_at" datetime, "updated_at" datetime) 
2
2
   (0.1ms) select sqlite_version(*)
3
-  (1212.3ms) CREATE INDEX "index_syncs_on_destination" ON "syncs" ("destination")
3
+  (1.8ms) CREATE INDEX "index_syncs_on_destination" ON "syncs" ("destination")
4
4
   (0.1ms) 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
-  (59.3ms) CREATE INDEX "index_syncs_on_subject_id_and_subject_type_and_destination" ON "syncs" ("subject_id", "subject_type", "destination")
13
-  (439.5ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL)
14
-  (38.6ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
12
+  (1.6ms) CREATE INDEX "index_syncs_on_subject_id_and_subject_type_and_destination" ON "syncs" ("subject_id", "subject_type", "destination")
13
+  (1.6ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL)
14
+  (1.6ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
15
15
   (0.1ms) SELECT version FROM "schema_migrations"
16
-  (39.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20160225141153')
16
+  (1.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20160225141153')
17
17
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
18
18
   (0.1ms) begin transaction
19
19
  ------------------------------
20
20
  SyncableModelsTest: test_truth
21
21
  ------------------------------
22
-  (0.0ms) rollback transaction
22
+  (0.1ms) rollback transaction
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.1
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-03 00:00:00.000000000 Z
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