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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDBiNTZlY2I3YTI0YTc1ZmFhYjRiMDEwZWY4NWNiNmJhNGQ2NjczOQ==
4
+ OGIwODg1OTU1NDI2MThhYzNjNDVjZWU1OWUyMzcwMDI2OGY1MmRhZg==
5
5
  data.tar.gz: !binary |-
6
- MDZiNzZlOTc1YzRkYzYzN2ZhNzM3ZTgwOTAzY2UwODMxYjAzMzQ3MA==
6
+ MzczYzA4ZDJlODQwMGYwZTE1OGE0MWViOTMwMGZjZmFjZDM2YTJkMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDQ5NjUwZGM4ODM0ZjE1MDkyNzRlYmYzNDMyYmUxMDk1ZWI1YzQ3MDZlYWYy
10
- MzEwYTcyOTEyMDY4OGEzOWYxNDc2YmU1NmY5OWY1MmY1YjgyMDFhODcxZGI0
11
- MDM4YmI5NWFlNDc1ZjE1ZmM5ZWRjZTE5OTFlMDdmMzViYjMxNjc=
9
+ MzcyMWE4MjEwY2IzOTgwOGViZDMzNWQ2ZDc4OTc5YjlhYmEzZTA3OGVkM2Ji
10
+ YjcxYWRjN2RjNzg1MTE2MzJlYmE4ZjM1ZDhkNjVlOTBkOGU5ODE4YTE0ZGYx
11
+ MzY5OTQ2MzZlOTBjMWNiZDliMWYyNGFlNTllMDhhMjNkYWQxOTA=
12
12
  data.tar.gz: !binary |-
13
- MDkyNzRjYzYwYTllMDFjY2QyZjczMGExMmY5ZWUwMTY1NmI5NTkzOWYwYjU2
14
- ZDEzNTk4OTE0OWM1MjZlMmM2YThlZWE0NDdkYTZjZTFhYmVhYWM3MTIzMmFi
15
- N2U2Y2VhMTY2NzRlNjg5NGFhNzZmOGU4YjdiMzUyYjkwOTNmMWI=
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
- 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
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
@@ -1,3 +1,3 @@
1
1
  module SyncableModels
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  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: self.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
@@ -1,7 +1,7 @@
1
-  (2.8ms) 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
-  (0.2ms) select sqlite_version(*)
3
-  (2.4ms) CREATE INDEX "index_syncs_on_destination" ON "syncs" ("destination")
4
-  (0.2ms) SELECT sql
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) 
2
+  (0.1ms) select sqlite_version(*)
3
+  (1.7ms) CREATE INDEX "index_syncs_on_destination" ON "syncs" ("destination")
4
+  (0.1ms) 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
-  (2.3ms) CREATE INDEX "index_syncs_on_subject_id_and_subject_type_and_destination" ON "syncs" ("subject_id", "subject_type", "destination")
13
-  (3.8ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL)
14
-  (2.2ms) 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.8ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
15
15
   (0.1ms) SELECT version FROM "schema_migrations"
16
-  (2.7ms) 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
-  (0.2ms) begin transaction
18
+  (0.1ms) begin transaction
19
19
  ------------------------------
20
20
  SyncableModelsTest: test_truth
21
21
  ------------------------------
22
-  (0.1ms) rollback transaction
22
+  (0.0ms) 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.7
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-17 00:00:00.000000000 Z
11
+ date: 2016-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord