active_record_json_importer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94296a7f918537dacf92dadfcfed11163b961987
4
+ data.tar.gz: a10213a96d93e2d2b743ee58598ad351faf6eaff
5
+ SHA512:
6
+ metadata.gz: 8b85657cbd9895f6f6bff779bb6f6b9db3b21d6b8c7b5b8b7b4d610f022a0b7ce42759e90df20675ec0c35a31f5e3af4a78fc8dfcc618000990a73fa5b6bc4f1
7
+ data.tar.gz: 20065eda8f9e49b9df0e3caaa1b02e0aca1cae88dd62165715e77ef74006c48e34d98db6dd1b2bfa06c1846c20b94550e9c2e5b8977f2662d5596601a8347ae4
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
4
+ --order rand
@@ -0,0 +1 @@
1
+ activerecord-json-importer
@@ -0,0 +1 @@
1
+ 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gem 'pry'
3
+ # Specify your gem's dependencies in simplest_benchmark.gemspec
4
+ gemspec
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-json-importer (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (4.2.5)
10
+ activesupport (= 4.2.5)
11
+ builder (~> 3.1)
12
+ activerecord (4.2.5)
13
+ activemodel (= 4.2.5)
14
+ activesupport (= 4.2.5)
15
+ arel (~> 6.0)
16
+ activerecord-import (0.10.0)
17
+ activerecord (>= 3.0)
18
+ activesupport (4.2.5)
19
+ i18n (~> 0.7)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.3, >= 0.3.4)
23
+ tzinfo (~> 1.1)
24
+ arel (6.0.3)
25
+ builder (3.2.2)
26
+ coderay (1.1.0)
27
+ diff-lcs (1.2.5)
28
+ i18n (0.7.0)
29
+ json (1.8.3)
30
+ method_source (0.8.2)
31
+ minitest (5.8.3)
32
+ pry (0.10.3)
33
+ coderay (~> 1.1.0)
34
+ method_source (~> 0.8.1)
35
+ slop (~> 3.4)
36
+ rake (10.4.2)
37
+ rspec (3.4.0)
38
+ rspec-core (~> 3.4.0)
39
+ rspec-expectations (~> 3.4.0)
40
+ rspec-mocks (~> 3.4.0)
41
+ rspec-core (3.4.1)
42
+ rspec-support (~> 3.4.0)
43
+ rspec-expectations (3.4.0)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.4.0)
46
+ rspec-its (1.2.0)
47
+ rspec-core (>= 3.0.0)
48
+ rspec-expectations (>= 3.0.0)
49
+ rspec-mocks (3.4.0)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.4.0)
52
+ rspec-support (3.4.1)
53
+ slop (3.6.0)
54
+ sqlite3 (1.3.11)
55
+ thread_safe (0.3.5)
56
+ tzinfo (1.2.2)
57
+ thread_safe (~> 0.1)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ activerecord-import (~> 0.8)
64
+ activerecord-json-importer!
65
+ activesupport (~> 4.2)
66
+ bundler (~> 1.6)
67
+ pry
68
+ rake (~> 10.0)
69
+ rspec (~> 3.0)
70
+ rspec-its (~> 1.0)
71
+ sqlite3 (~> 1.3)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 carlosgab83
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,89 @@
1
+ # simplest_benchmark
2
+ ####Benchmark your code fastest, simplest and get pragmatic results.
3
+ - Perfectly formated for get into an spreadsheet and analyze it.
4
+ - Inegrated with Rails logger
5
+ - Not Rails ? don't worry, it creates a cutom simplest_benchmark.log log file
6
+ - only two methods: sb_begin, and sb_end
7
+
8
+ It's so simple:
9
+
10
+ ####Installation
11
+ `gem install simplest_benchmark`
12
+
13
+ or in your Gemfile:
14
+
15
+ `gem 'simplest_benchmark'`
16
+
17
+ ####Usage
18
+
19
+ ##### Simple use
20
+ ```ruby
21
+ require 'simplest_benchmark'
22
+
23
+ class MySlowClass
24
+ include SimplestBenchmark
25
+
26
+ def slow_method
27
+ var = get_some_value
28
+ sb_begin('heavy work')
29
+ some_hard_work
30
+ sb_end('heavy work')
31
+ end
32
+ end
33
+ ```
34
+
35
+ ##### Nested use
36
+
37
+ ```ruby
38
+ require 'simplest_benchmark'
39
+
40
+ class MySlowClass
41
+ include SimplestBenchmark
42
+
43
+ def slow_method
44
+ var = get_some_value
45
+ sb_begin('heavy work')
46
+ some_hard_work
47
+ sb_end('heavy_work')
48
+ end
49
+
50
+ def some_hard_work
51
+ sb_begin('heavy work 1')
52
+ hard_work_part1
53
+ sb_end('heavy work 1')
54
+
55
+ sb_begin('heavy work 2')
56
+ hard_work_part2
57
+ sb_end('heavy work 2')
58
+ end
59
+ end
60
+ ```
61
+
62
+ ####Results
63
+
64
+ Results go to Rails log file, or to simplest_benchmark.log file.
65
+ You can simply do
66
+
67
+ `cat log/development.log | grep DEBUG`
68
+
69
+ and obtain something like:
70
+ ```
71
+ DEBUG 2015-11-19 17:13:24 -0300 1447964004.003463 BEGIN 0 heavy work
72
+ DEBUG 2015-11-19 17:13:24 -0300 1447964004.003463 BEGIN 1 heavy work 1
73
+ DEBUG 2015-11-19 17:13:33 -0300 1447964013.4338706 END 1 heavy work 1 11.3775455951690674
74
+ DEBUG 2015-11-19 17:13:33 -0300 1447964004.003463 BEGIN 1 heavy work 2
75
+ DEBUG 2015-11-19 17:13:38 -0300 1447964013.4338706 END 1 heavy work 2 5.38402085203944020
76
+ DEBUG 2015-11-19 17:13:38 -0300 1448652456.6792688 END 0 heavy work 17.622838973999023
77
+ ```
78
+ - field1: DEBUG text
79
+ - field2: timestamp of event
80
+ - field3: timestamp in float format
81
+ - field4: BEGIN or END event
82
+ - field5: level of deepness: For nested usage, each nested sb_begin gonna +1 this number
83
+ - field6: label identifier
84
+ - field7: amount of seconds transcurred between last sb_begin and current sb_end call.
85
+
86
+ it is all. Enjoy.
87
+
88
+
89
+
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'active_record_json_importer'
3
+ s.version = '0.0.1'
4
+ s.date = '2015-09-30'
5
+ s.summary = "Simple and fast way to bulk import massive active records into database. It uses the awesome activerecord-import gem"
6
+ s.description = "Importing records from a json file go slow?. No problem, use me. It uses the awesome activerecord-import gem. Documentation and code in https://github.com/carlosgab83/active_record_json_importer/"
7
+ s.authors = ["Carlos Ochoa"]
8
+ s.email = 'carlosgab83@gmail.com'
9
+ s.files = `git ls-files -z`.split("\x0")
10
+ s.homepage = 'http://rubygems.org/gems/active_record_json_importer'
11
+ s.license = 'MIT'
12
+
13
+ s.files = `git ls-files -z`.split("\x0")
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+ s.require_paths = ["lib"]
16
+
17
+
18
+ s.add_development_dependency "bundler", "~> 1.6"
19
+ s.add_development_dependency "rake", "~> 10.0"
20
+ s.add_development_dependency "rspec", "~> 3.0"
21
+ s.add_development_dependency "rspec-its", "~> 1.0"
22
+ s.add_development_dependency "activerecord-import", "~> 0.8"
23
+ s.add_development_dependency "activesupport", "~> 4.2"
24
+ s.add_development_dependency "sqlite3", "~> 1.3"
25
+ end
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+
3
+ module ActiveRecordJsonImporter
4
+
5
+ class Importer
6
+
7
+ attr_reader :active_record_class, :options, :bulk_input_file
8
+ attr_accessor :records_at_once
9
+
10
+ RECORDS_AT_ONCE = 5000
11
+
12
+ def initialize(options = {})
13
+ @options = options
14
+ initialize_active_record_class
15
+ initialize_bulk_input_file
16
+ initialize_records_at_once
17
+ end
18
+
19
+ def import
20
+ records = []
21
+ bulk_input_file.each_with_index do |line, index|
22
+ records << JSON.parse(line)
23
+ if (index % records_at_once == 0)
24
+ import_records records
25
+ records = []
26
+ end
27
+ end
28
+ bulk_input_file.close
29
+ import_records records if records.any?
30
+ return true
31
+ end
32
+
33
+ private
34
+
35
+ def initialize_active_record_class
36
+ obj = options[:active_record_class]
37
+ msg = "invalid active_record_class."
38
+ if obj.present? and obj.ancestors.include? ActiveRecord::Base
39
+ @active_record_class = obj
40
+ else
41
+ fail RuntimeError, "#{msg} #{obj}"
42
+ end
43
+ rescue
44
+ raise RuntimeError, "#{msg} #{obj}"
45
+ end
46
+
47
+ def initialize_records_at_once
48
+ @records_at_once = options[:records_at_once] || RECORDS_AT_ONCE
49
+ end
50
+
51
+ def initialize_bulk_input_file
52
+ @bulk_input_file = File.open(options[:file], "r")
53
+ end
54
+
55
+ def import_records records
56
+ active_records = records.map{|record| active_record_class.new record}
57
+ active_record_class.import(active_records, validate: false)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe ActiveRecordJsonImporter::Importer do
4
+
5
+ context "with valid ActiveRecord class" do
6
+
7
+ before(:all) do
8
+ @table_name = "pools"
9
+ @file_name = "input.json"
10
+ @invalid_file_name = "invalid_input.json"
11
+
12
+ create_test_table_and_activerecord(@table_name)
13
+
14
+ @active_record_class = @table_name.singularize.camelize.constantize
15
+
16
+ create_valid_json_file(@file_name)
17
+ create_invalid_json_file(@invalid_file_name)
18
+ end
19
+
20
+ after(:all) do
21
+ drop_test_table(@table_name)
22
+ delete_json_file(@file_name)
23
+ delete_json_file(@invalid_file_name)
24
+ end
25
+
26
+ context "with valid input" do
27
+ it "imports the file" do
28
+ importer = ActiveRecordJsonImporter::Importer.new(file: @file_name,
29
+ active_record_class: @active_record_class)
30
+ expect(importer.import).to be_truthy
31
+ expect(@active_record_class.count).to eq(2)
32
+ end
33
+ end
34
+
35
+ context "with invalid input" do
36
+ it "raises an Exception" do
37
+ importer = ActiveRecordJsonImporter::Importer.new(file: @invalid_file_name,
38
+ active_record_class: @active_record_class)
39
+ expect{importer.import}.to raise_exception(JSON::ParserError)
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ context "with invalid ActiveRecord class" do
46
+ it "raises an Exception" do
47
+ expect{importer = ActiveRecordJsonImporter::Importer.new(file: @invalid_file_name,
48
+ active_record_class: "any object")}.to raise_exception(RuntimeError, "invalid active_record_class. any object")
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ RSpec.configure do |config|
2
+ end
3
+
4
+ require 'spec_helper'
5
+ require 'active_record'
6
+ require 'active_support'
7
+ require 'activerecord-import'
8
+
9
+ Dir["./spec/support/**/*.rb"].sort.each { |f| require f; puts f }
10
+ Dir["./lib/**/*.rb"].sort.each { |f| require f; puts f }
11
+
12
+ ActiveRecord::Migration.maintain_test_schema!
13
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ # The idea is re-utilization of this methods in any spec
4
+
5
+ def create_arec(table_name, &block)
6
+ klass = Class.new(ActiveRecord::Base){self.table_name = table_name}
7
+ klass.class_eval &block
8
+ klass
9
+ end
10
+
11
+ def create_test_table_and_activerecord(table_name)
12
+ # don't output all the migration activity
13
+ ActiveRecord::Migration.verbose = false
14
+
15
+ # execute the migration, creating tables
16
+ ActiveRecord::Schema.define(:version => 1) do
17
+
18
+ create_table table_name.to_sym do |t|
19
+ t.integer :a1
20
+ t.text :a2
21
+ end
22
+
23
+ end#migration
24
+
25
+ Kernel.const_set(table_name.singularize.camelize, create_arec(table_name) {}) unless defined? @table11.singularize.camelize.constantize == "constant"
26
+ end
27
+
28
+ def drop_test_table(table_name)
29
+ ActiveRecord::Migration.verbose = false
30
+ # execute the migration, creating tables
31
+ ActiveRecord::Schema.define(:version => 2) do
32
+ drop_table "#{table_name}".to_sym
33
+ end
34
+ end
35
+
36
+ def create_valid_json_file(file_name)
37
+ File.open(@file_name,'w') do |f|
38
+ f.write '{"a1": 1, "a2": "pool1"}' + "\n"
39
+ f.write '{"a1": 2, "a2": "pool2"}' + "\n"
40
+ end
41
+ end
42
+
43
+ def create_invalid_json_file(file_name)
44
+ File.open(file_name,'w') do |f|
45
+ f.write '"a1" 1 "a2" "pool1"'
46
+ end
47
+ end
48
+
49
+ def delete_json_file(file_name)
50
+ FileUtils.rm(file_name)
51
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_json_importer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Ochoa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord-import
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.3'
111
+ description: Importing records from a json file go slow?. No problem, use me. It uses
112
+ the awesome activerecord-import gem. Documentation and code in https://github.com/carlosgab83/active_record_json_importer/
113
+ email: carlosgab83@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".rspec"
119
+ - ".ruby-gemset"
120
+ - ".ruby-version"
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - active_record_json_importer.gemspec
127
+ - lib/active_record_json_importer.rb
128
+ - spec/lib/active_record_json_importer_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/support/ar_helper.rb
131
+ homepage: http://rubygems.org/gems/active_record_json_importer
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.6
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Simple and fast way to bulk import massive active records into database.
155
+ It uses the awesome activerecord-import gem
156
+ test_files:
157
+ - spec/lib/active_record_json_importer_spec.rb
158
+ - spec/spec_helper.rb
159
+ - spec/support/ar_helper.rb