syncable_models 0.0.8 → 0.0.9

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
- OGIwODg1OTU1NDI2MThhYzNjNDVjZWU1OWUyMzcwMDI2OGY1MmRhZg==
4
+ YzExMWFmOTBmNzJkMjVjZjBlYTY1MjVkMWE4NzVlYzQxYjY1ZmIwNQ==
5
5
  data.tar.gz: !binary |-
6
- MzczYzA4ZDJlODQwMGYwZTE1OGE0MWViOTMwMGZjZmFjZDM2YTJkMw==
6
+ NTk0NjI3ZTliZTVlNGQ5ZWQ2NzMwZDY2NzExOTA4N2Q5Mzg5NDEzMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MzcyMWE4MjEwY2IzOTgwOGViZDMzNWQ2ZDc4OTc5YjlhYmEzZTA3OGVkM2Ji
10
- YjcxYWRjN2RjNzg1MTE2MzJlYmE4ZjM1ZDhkNjVlOTBkOGU5ODE4YTE0ZGYx
11
- MzY5OTQ2MzZlOTBjMWNiZDliMWYyNGFlNTllMDhhMjNkYWQxOTA=
9
+ NTYxODY1NWM0ZDUwYmM4MDdiMTczYzMxOTFjMWFlODg3ZWY3YjVmMDliYjhl
10
+ ZDUyYzM3NWNjNDkyYzMwYjk0NzE4YWQ0NmVmNWU2ZTc4MzA3NGU2NTJmNjU5
11
+ M2NkMTM4MWRmNWNiMTQyNjdmMDdmMDgyNDk5ZjliZmRkNDE3NDk=
12
12
  data.tar.gz: !binary |-
13
- ZmY3YmRiYWZjOTE4OThjODFiZTZiYWEzMjY4ZTNmMGQ0YjAwZmZiYmIyYzRh
14
- ZWQ5OWYxZWQ4NzdiZTY5NzNiMDJiNTE2ZGJjMDRmMWNiMWRmZTg5ZDMxZjkw
15
- MjFmZmRkZWY2MDg3OTM1NDUwNjY1OGY4M2NlZWIwZWJkNzlkODU=
13
+ YWI1OTQxYTg0MmRhNGY5NjE0ZWEyNTE3NTlkNWUyNmIxNDA2MGE3NWM2YmM4
14
+ MjkxNmY1YTUwNTk3YmM2MjkzOGQwZjkyODkwZDU0NGM0ZmUyNzBlZWY2Mzk1
15
+ NzhlMTQwOWE3MTNjMmRiYWZiMzI0YjkxYThmZjgzNmE4NzcyNTk=
data/bin/import ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join((ENV['APP_PATH'] || File.expand_path('.')), 'config', 'environment')
4
+
5
+ ## Importer
6
+ print 'Starting Importer... '
7
+
8
+ interval = SyncableModels::Importer.interval
9
+ timeout = SyncableModels::Importer.timeout
10
+
11
+ importer = Concurrent::TimerTask.new(execution_interval: interval, timeout_interval: timeout, run_now: true) do
12
+ begin
13
+ SyncableModels::Importer.import
14
+ rescue => exc
15
+ Rails.logger.error "[Importer] Error: #{exc.message}\n" \
16
+ " backtrace:\n #{exc.backtrace.join("\n ")}"
17
+ end
18
+ end
19
+ importer.execute
20
+ print "done.\n"
21
+
22
+ # Signal catching
23
+ def shut_down
24
+ puts "\nShutting down gracefully..."
25
+ sleep 1
26
+ end
27
+
28
+ ["TERM", "INT"].each do |sig|
29
+ trap(sig) do
30
+ shut_down
31
+ exit
32
+ end
33
+ end
34
+
35
+
36
+ loop { sleep }
@@ -7,4 +7,7 @@ SyncableModels::Importer.config do |conf|
7
7
  # import.import_model 'Project', id_key: :uuid
8
8
  # import.import_model 'Team', fetch_path: 'some_teams', sync_path: 'sync_some_teams'
9
9
  # end
10
+
11
+ # conf.interval = 5.minutes
12
+ # conf.timeout = 10.seconds
10
13
  end
@@ -0,0 +1,88 @@
1
+ require 'faraday'
2
+
3
+ module SyncableModels
4
+ module Importer
5
+ class Import
6
+ attr_accessor :api_url, :api_key, :destination
7
+ attr_reader :models
8
+
9
+ def initialize
10
+ @models = {}
11
+ end
12
+
13
+ def import_model(class_name, args = {})
14
+ class_name = class_name.to_s if class_name.is_a?(Class)
15
+ fetch_path = args[:fetch_path] || class_name.underscore.pluralize
16
+ sync_path = args[:sync_path] || "sync_" + class_name.underscore.pluralize
17
+ id_key = args[:id_key] || :uuid
18
+ @models[class_name] = {
19
+ fetch_path: fetch_path,
20
+ sync_path: sync_path,
21
+ id_key: id_key
22
+ }
23
+ end
24
+
25
+ def api_url
26
+ @api_url[-1] == '/' ? @api_url : @api_url + "/"
27
+ end
28
+
29
+ def params_with_api_key(params)
30
+ params.merge!(key: api_key) if api_key
31
+ params
32
+ end
33
+
34
+ def import
35
+ @models.each do |model_name, params|
36
+ fetch_url = self.api_url + params[:fetch_path]
37
+ sync_url = self.api_url + params[:sync_path]
38
+
39
+ conn = Faraday.new(url: fetch_url)
40
+ response = conn.get '', params_with_api_key(destination: destination)
41
+
42
+ if response.success?
43
+ response = JSON.parse(response.body)
44
+
45
+ if response["status"].to_i == 401
46
+ puts "[SyncableModels::Importer] Wrong api key!"
47
+ end
48
+
49
+ if response['objects'] && response['objects'].count > 0
50
+ klass = model_name.constantize
51
+ synced_ids = []
52
+
53
+ response['objects'].each do |o|
54
+ result = klass.from_import_hash(o)
55
+ puts "[SyncableModels::Importer] Importing #{model_name} (id=#{o[params[:id_key].to_s]}): #{ result ? 'OK' : 'FAIL' }"
56
+ synced_ids << o[params[:id_key].to_s] if result
57
+ end
58
+
59
+ if synced_ids.any?
60
+ conn = Faraday.new(url: sync_url)
61
+ response = conn.get '', params_with_api_key(destination: self.destination, ids: synced_ids)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ mattr_accessor :imports, :interval, :timeout
70
+ @@imports = []
71
+ @@interval = 5.minutes
72
+ @@timeout = 10.seconds
73
+
74
+ def self.config
75
+ yield self
76
+ end
77
+
78
+ def self.add_import
79
+ import = Import.new
80
+ yield import
81
+ @@imports << import
82
+ end
83
+
84
+ def self.import
85
+ @@imports.each &:import
86
+ end
87
+ end
88
+ end
@@ -1,3 +1,3 @@
1
1
  module SyncableModels
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -1,10 +1,7 @@
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
+ require "syncable_models/importer"
5
5
 
6
6
  module SyncableModels
7
7
  end
8
-
9
- module SyncableModelsImporter
10
- end
@@ -1,7 +1,7 @@
1
-  (2.0ms) 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
+  (3.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
3
   (1.7ms) CREATE INDEX "index_syncs_on_destination" ON "syncs" ("destination")
4
-  (0.1ms) SELECT sql
4
+  (0.2ms) 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
-  (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.8ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
12
+  (1.7ms) CREATE INDEX "index_syncs_on_subject_id_and_subject_type_and_destination" ON "syncs" ("subject_id", "subject_type", "destination")
13
+  (1.8ms) 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
16
   (1.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20160225141153')
17
- ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
18
-  (0.1ms) begin transaction
17
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
18
+  (0.2ms) 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.8
4
+ version: 0.0.9
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-18 00:00:00.000000000 Z
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -69,7 +69,8 @@ dependencies:
69
69
  description: The gem provides tagged syncing functionality and API methods.
70
70
  email:
71
71
  - serafim.nenarokov@flant.ru
72
- executables: []
72
+ executables:
73
+ - import
73
74
  extensions: []
74
75
  extra_rdoc_files: []
75
76
  files:
@@ -77,6 +78,7 @@ files:
77
78
  - README.md
78
79
  - Rakefile
79
80
  - app/models/syncable_models/sync.rb
81
+ - bin/import
80
82
  - config/routes.rb
81
83
  - db/migrate/20160225141153_create_syncs.rb
82
84
  - lib/generators/syncable_models/import_config_generator.rb
@@ -85,8 +87,8 @@ files:
85
87
  - lib/syncable_models/active_record.rb
86
88
  - lib/syncable_models/controller.rb
87
89
  - lib/syncable_models/engine.rb
90
+ - lib/syncable_models/importer.rb
88
91
  - lib/syncable_models/version.rb
89
- - lib/syncable_models_importer.rb
90
92
  - lib/tasks/syncable_models_tasks.rake
91
93
  - test/dummy/README.rdoc
92
94
  - test/dummy/Rakefile
@@ -1,84 +0,0 @@
1
- require 'faraday'
2
-
3
- module SyncableModelsImporter
4
- class Import
5
- attr_accessor :api_url, :api_key, :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 params_with_api_key(params)
29
- params.merge!(key: api_key) if api_key
30
- params
31
- end
32
-
33
- def import
34
- @models.each do |model_name, params|
35
- fetch_url = self.api_url + params[:fetch_path]
36
- sync_url = self.api_url + params[:sync_path]
37
-
38
- conn = Faraday.new(url: fetch_url)
39
- response = conn.get '', params_with_api_key(destination: destination)
40
-
41
- if response.success?
42
- response = JSON.parse(response.body)
43
-
44
- if response["status"].to_i == 401
45
- puts "[SyncableModelsImporter] Wrong api key!"
46
- end
47
-
48
- if response['objects'] && response['objects'].count > 0
49
- klass = model_name.constantize
50
- synced_ids = []
51
-
52
- response['objects'].each do |o|
53
- result = klass.from_import_hash(o)
54
- puts "[SyncableModelsImporter] Importing #{model_name} (id=#{o[params[:id_key].to_s]}): #{ result ? 'OK' : 'FAIL' }"
55
- synced_ids << o[params[:id_key].to_s] if result
56
- end
57
-
58
- if synced_ids.any?
59
- conn = Faraday.new(url: sync_url)
60
- response = conn.get '', params_with_api_key(destination: self.destination, ids: synced_ids)
61
- end
62
- end
63
- end
64
- end
65
- end
66
- end
67
-
68
- mattr_accessor :imports
69
- @@imports = []
70
-
71
- def self.config
72
- yield self
73
- end
74
-
75
- def self.add_import
76
- import = Import.new
77
- yield import
78
- @@imports << import
79
- end
80
-
81
- def self.import
82
- @@imports.each &:import
83
- end
84
- end