paperbin 0.1.0 → 0.2.0

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf1cd912977e0f0847d562a7812fc4b5e3399968
4
- data.tar.gz: 124ee2cfe0f4268eaddac9ed35ce692896c7cc14
3
+ metadata.gz: cd1734e70bdbdbb87410b3620a0d73bbc89e520a
4
+ data.tar.gz: db80f4440568848b7009025c23d3caaa472f8ad2
5
5
  SHA512:
6
- metadata.gz: 21307c488260585d7a632144d23635bd9c8faaa070ad5935446f7efd8ff5c4a3ccaba02696275a1d9dbfc598321f9685d719b7cfd888c07f3b439f2f5532b7e3
7
- data.tar.gz: 97468df41f6899cf2aca5fd6024c784a1e2344baf11758da243b56db6f3b94f6e0662963228f415ad2d22412183e7f42dd9428ecf5bb903bad948a5c7c0dd121
6
+ metadata.gz: 5ef75f0bcb767a1047f0c5ea506b8467722f25fb10328e14a95435121e3c64c04e8e5a4f356d4ccc112a309c052c6f5220d9d556a533f92ba84f07b2eecbf6d0
7
+ data.tar.gz: af7f8fdb2076f5457457b5700ed7291c4023e7b0ec760aa044d2cfe7da22dcdcf944b3394a48b07ae1d87e22910cf052d7f8fd97141148bce55cdd261f27120f
data/README.md CHANGED
@@ -17,9 +17,10 @@ Or install it yourself as:
17
17
 
18
18
  $ gem install paperbin
19
19
 
20
- ## Usage
20
+ ### Queues
21
21
 
22
- TODO: Write usage instructions here
22
+ check worker - ENV['PAPERBIN_CHECK_QUEUE'] || 'default'
23
+ write worker - ENV['PAPERBIN_WRITE_QUEUE'] || 'default'
23
24
 
24
25
  ## Contributing
25
26
 
@@ -1,5 +1,6 @@
1
1
  class Paperbin::CheckWorker
2
2
  include Sidekiq::Worker
3
+ sidekiq_options queue: (ENV['PAPERBIN_CHECK_QUEUE'] || 'default')
3
4
 
4
5
  def perform(item_id, item_type)
5
6
  paperbin_handler = Paperbin::Handler.new(item_id, item_type)
@@ -1,5 +1,4 @@
1
1
  class Paperbin::Handler < Struct.new(:id, :type)
2
-
3
2
  require 'zlib'
4
3
  require 'tempfile'
5
4
 
@@ -7,11 +6,16 @@ class Paperbin::Handler < Struct.new(:id, :type)
7
6
  "%012d" % id
8
7
  end
9
8
 
10
- def split_id
9
+ def split
11
10
  formatted_id.scan(/.{4}/)
12
11
  end
13
12
 
14
- def current_path
13
+ def dir_split_id
14
+ split[0...-1]
15
+ end
16
+
17
+ def archive_split_id
18
+ split.last
15
19
  end
16
20
 
17
21
  def options
@@ -29,10 +33,39 @@ class Paperbin::Handler < Struct.new(:id, :type)
29
33
  dirs << options[:path]
30
34
  dirs << item.send(options[:base_scope])
31
35
  dirs << type
32
- dirs += split_id
36
+ dirs += dir_split_id
33
37
  File.join(dirs.map(&:to_s))
34
38
  end
35
39
 
40
+ def old_directory_path
41
+ File.join(directory_path, archive_split_id)
42
+ end
43
+
44
+ def archive_path
45
+ File.join(directory_path, "#{archive_split_id}.zip")
46
+ end
47
+
48
+ def create_archive_file
49
+ unless File.exists?(archive_path)
50
+ Zippy.create archive_path do |z|
51
+ z["created_at.txt"] = Date.today.to_s
52
+ handle_old_files(z)
53
+ end
54
+ end
55
+ end
56
+
57
+ def handle_old_files(z)
58
+ if File.directory?(old_directory_path)
59
+ Dir.new(old_directory_path).each do |file|
60
+ if file =~ /\.gz/
61
+ name = file.split(".").first
62
+ z["#{name}.json"] = Zlib::GzipReader.open(File.join(old_directory_path, file)) {|gz| gz.read }
63
+ end
64
+ end
65
+ FileUtils.remove_dir(old_directory_path)
66
+ end
67
+ end
68
+
36
69
  def versions
37
70
  Version.where(item_type: type, item_id: id)
38
71
  end
@@ -40,6 +73,7 @@ class Paperbin::Handler < Struct.new(:id, :type)
40
73
  def save_versions
41
74
  return true unless item
42
75
  create_directory
76
+ create_archive_file
43
77
  generate_files
44
78
  Paperbin::CheckWorker.perform_async(id, type)
45
79
  end
@@ -49,11 +83,19 @@ class Paperbin::Handler < Struct.new(:id, :type)
49
83
  end
50
84
 
51
85
  def md5_file(version)
52
- File.join(directory_path, "#{version.id}.md5")
86
+ "#{version.id}.md5"
87
+ end
88
+
89
+ def md5_file_path(version)
90
+ File.join(directory_path, "#{md5_file(version)}")
91
+ end
92
+
93
+ def json_file(version)
94
+ "#{version.id}.json"
53
95
  end
54
96
 
55
- def gz_file(version, checked = false)
56
- File.join(directory_path, "#{version.id}.gz#{checked ? '' : '.unchecked'}")
97
+ def json_file_path(version, checked = false)
98
+ File.join(directory_path, "#{json_file(version)}#{checked ? '' : '.unchecked'}")
57
99
  end
58
100
 
59
101
  def files_exist?(*args)
@@ -61,8 +103,8 @@ class Paperbin::Handler < Struct.new(:id, :type)
61
103
  end
62
104
 
63
105
  def md5_valid?(version)
64
- record_md5 = File.read(md5_file(version))
65
- data = Zlib::GzipReader.open(gz_file(version)) {|gz| gz.read }
106
+ record_md5 = File.read(md5_file_path(version))
107
+ data = File.open(json_file_path(version), "r") { |f| f.read }
66
108
  check_md5 = Digest::MD5.hexdigest(data)
67
109
  record_md5 == check_md5
68
110
  end
@@ -71,29 +113,33 @@ class Paperbin::Handler < Struct.new(:id, :type)
71
113
  # remove records from db expcet the lastest one
72
114
  version.delete unless version == last_item
73
115
  # rename file extension
74
- File.rename(gz_file(version), gz_file(version, true))
75
- options[:callback].call(gz_file(version, true)) if options[:callback]
116
+ File.rename(json_file_path(version), json_file_path(version, true))
117
+ Zippy.open(archive_path) do |z|
118
+ z["#{json_file(version).to_s}"] = File.open(json_file_path(version, true)) { |f| f.read }
119
+ end
120
+ File.delete(md5_file_path(version))
121
+ File.delete(json_file_path(version, true))
122
+ options[:callback].call(json_file_path(version, true)) if options[:callback]
76
123
  end
77
124
 
78
125
  def generate_files
79
126
  versions.each do |version|
80
- write_gz_file version
127
+ write_json_file version
81
128
  write_md5_file version
82
129
  end
83
130
  end
84
131
 
85
- def write_gz_file(version)
86
- path = gz_file version
132
+ def write_json_file(version)
133
+ path = json_file_path version
87
134
  unless files_exist?(path)
88
- Zlib::GzipWriter.open(path) { |gz| gz.write string_data(version) }
89
-
135
+ File.open(path, "w") { |f| f.write(string_data(version)) }
90
136
  timestamp = version.created_at.to_time
91
137
  File.utime timestamp, timestamp, path
92
138
  end
93
139
  end
94
140
 
95
141
  def write_md5_file(version)
96
- File.open(md5_file(version), "w") do |file|
142
+ File.open(md5_file_path(version), "w") do |file|
97
143
  md5 = Digest::MD5.hexdigest(string_data(version))
98
144
  file.write md5
99
145
  end
@@ -105,18 +151,18 @@ class Paperbin::Handler < Struct.new(:id, :type)
105
151
 
106
152
  def check_versions
107
153
  versions.each_with_index do |version, index|
108
- # check both file exist or not
109
- next unless files_exist?(md5_file(version), gz_file(version))
110
-
111
- if md5_valid?(version)
112
- process_valid_records(version, versions.last)
113
- else
114
- # remove both files
115
- [gz_file(version), md5_file(version)].each do |f|
116
- File.delete(f)
117
- end
118
- raise Errno::ENOENT
154
+ # check both file exist or not
155
+ next unless files_exist?(md5_file_path(version), json_file_path(version))
156
+
157
+ if md5_valid?(version)
158
+ process_valid_records(version, versions.last)
159
+ else
160
+ # remove both files
161
+ [json_file_path(version), md5_file_path(version)].each do |f|
162
+ File.delete(f)
119
163
  end
164
+ raise Errno::ENOENT
165
+ end
120
166
  end
121
167
  rescue Errno::ENOENT
122
168
  # lodge worker unless valid
@@ -1,3 +1,3 @@
1
1
  module Paperbin
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  class Paperbin::WriteWorker
2
2
  include Sidekiq::Worker
3
+ sidekiq_options queue: (ENV['PAPERBIN_WRITE_QUEUE'] || 'default')
3
4
 
4
5
  def perform(item_id, item_type)
5
6
  paperbin_handler = Paperbin::Handler.new(item_id, item_type)
data/lib/paperbin.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'rails'
2
- require 'sidekiq/worker'
2
+ require 'sidekiq'
3
+ require 'zippy'
4
+ require 'fileutils'
3
5
 
4
6
  module Paperbin
5
7
  class Railtie < Rails::Railtie
data/paperbin.gemspec CHANGED
@@ -26,4 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_runtime_dependency "paper_trail"
27
27
  spec.add_runtime_dependency "railties", "~> 3.2"
28
28
  spec.add_runtime_dependency "activesupport", "~> 3.2"
29
+ spec.add_runtime_dependency "zippy"
30
+ spec.add_runtime_dependency "zip"
29
31
  end
@@ -1,7 +1,6 @@
1
- require "spec_helper"
1
+ require "./spec_helper"
2
2
 
3
3
  describe Paperbin::Handler do
4
-
5
4
  let(:item) { double(organisation_id: "org1") }
6
5
  let(:target_id) { '123456789' }
7
6
  let(:target_type) { 'Client' }
@@ -17,19 +16,26 @@ describe Paperbin::Handler do
17
16
  handler.formatted_id.should == "000123456789"
18
17
  end
19
18
 
20
- it "split formatted_id into 3 sections" do
21
- handler.split_id.should == ['0001', '2345', '6789']
19
+ it "split formatted_id into 2 sections" do
20
+ handler.dir_split_id.should == ['0001', '2345']
22
21
  end
23
22
 
24
- it 'return options' do
25
- handler.options.should == {path: '/path', base_scope: 'organisation_id'}
23
+ it "give proper file name" do
24
+ handler.archive_split_id.should == '6789'
26
25
  end
27
26
 
28
- it "return directory" do
29
- handler.directory_path.should == "/path/org1/Client/0001/2345/6789"
27
+ it 'return options' do
28
+ handler.options.should == {path: '/path', base_scope: 'organisation_id'}
30
29
  end
31
30
 
32
31
  context 'directories' do
32
+ it "return directory" do
33
+ handler.directory_path.should == "/path/org1/Client/0001/2345"
34
+ end
35
+
36
+ it "return old directory" do
37
+ handler.old_directory_path.should == "/path/org1/Client/0001/2345/6789"
38
+ end
33
39
 
34
40
  it 'generate directory when no exists' do
35
41
  Dir.stub(exists?: false)
@@ -42,7 +48,6 @@ describe Paperbin::Handler do
42
48
  FileUtils.should_not_receive(:mkdir_p)
43
49
  handler.create_directory
44
50
  end
45
-
46
51
  end
47
52
 
48
53
  context 'generate_files' do
@@ -57,7 +62,7 @@ describe Paperbin::Handler do
57
62
  end
58
63
 
59
64
  it 'create correct Gzip files' do
60
- Zlib::GzipWriter.should_receive(:open).twice
65
+ File.should_receive(:open).exactly(4).times
61
66
  expect(File).to receive(:utime).twice.with(
62
67
  timestamp, timestamp, an_instance_of(String)
63
68
  )
@@ -76,6 +81,7 @@ describe Paperbin::Handler do
76
81
  handler.stub files_exist?: true
77
82
  File.stub rename: true, delete: true
78
83
  Paperbin::WriteWorker.stub perform_async: true
84
+ Zippy.stub :open
79
85
  end
80
86
 
81
87
  subject(:check_versions) { handler.check_versions }
@@ -109,7 +115,48 @@ describe Paperbin::Handler do
109
115
  check_versions
110
116
  end
111
117
  end
112
-
113
118
  end
114
119
 
120
+ context 'old folders' do
121
+ let(:timestamp) { Time.now }
122
+ let(:version_1) { double(id: 1, to_json: "json", created_at: timestamp) }
123
+ let(:version_2) { double(id: 2, to_json: "json", created_at: timestamp) }
124
+ let(:file) { double(write: true) }
125
+
126
+ before(:each) do
127
+ handler.stub(versions: [version_1, version_2])
128
+ handler.stub(:old_directory_path).and_return("./tmp1")
129
+ handler.stub(:directory_path).and_return("./tmp2")
130
+ FileUtils.mkdir_p(handler.old_directory_path)
131
+ FileUtils.mkdir_p(handler.directory_path)
132
+ Zlib::GzipWriter.open(File.join(handler.old_directory_path, "12.gz")) { |gz| gz.write "Very important json" }
133
+ handler.create_directory
134
+ handler.create_archive_file
135
+ end
136
+
137
+ after(:each) do
138
+ FileUtils.remove_dir(handler.old_directory_path) if File.directory?(handler.old_directory_path)
139
+ FileUtils.remove_dir(handler.directory_path) if File.directory?(handler.directory_path)
140
+ end
141
+
142
+ it 'should create archive file' do
143
+ File.exists?(handler.archive_path).should be_true
144
+ end
145
+
146
+ it 'archive should not be empty' do
147
+ Zippy.list(handler.archive_path).detect { |f| f == "12.json" }.should be_true
148
+ end
149
+
150
+ it "json file should be correct" do
151
+ data = ""
152
+ Zippy.open(handler.archive_path) do |z|
153
+ data = z["12.json"]
154
+ end
155
+ data.should == "Very important json"
156
+ end
157
+
158
+ it "should remove old directory" do
159
+ File.directory?(handler.old_directory_path).should be_false
160
+ end
161
+ end
115
162
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperbin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - morhekil
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2014-01-23 00:00:00.000000000 Z
16
+ date: 2014-03-03 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: bundler
@@ -113,6 +113,34 @@ dependencies:
113
113
  - - ~>
114
114
  - !ruby/object:Gem::Version
115
115
  version: '3.2'
116
+ - !ruby/object:Gem::Dependency
117
+ name: zippy
118
+ requirement: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ type: :runtime
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ - !ruby/object:Gem::Dependency
131
+ name: zip
132
+ requirement: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ type: :runtime
138
+ prerelease: false
139
+ version_requirements: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
116
144
  description: Paperbin removes paper_trail versions and move them to filesystem
117
145
  email:
118
146
  - kotlarek.krzysztof@gmail.com