rails-archiver 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc1b521101d84755190144096b2d1d97ac2e66e7
4
- data.tar.gz: bcf461951a3da9969bcbf8ab3b32edb776f20e6f
3
+ metadata.gz: b0d76121c6ff0a8777277b47da964fc253096491
4
+ data.tar.gz: ee9dcbe2c0d1ec468ba44dc2e86b4f5f5b3c9f17
5
5
  SHA512:
6
- metadata.gz: 5d286d61a14be69f8d076e3a9e3cb0ffcda2492df14cd79945ee0ef16e282b73ed699ab81865def9fdc96be52e32f4d578a72aadebf65fb304ae27a5bea1541e
7
- data.tar.gz: 7b53b02be218bedaba1eaef692d9a0a4ac05fda3135d39fec9f628d449a9e590e881a6d9c1c620e2226bc0559ddd81b9f00fb770059427c09807dfcc6b8ec015
6
+ metadata.gz: eaf86068fff7d987a7098be77ec1cc25653525f973cc8abe4a97210a108feb2050d8f2ebba3bd8f35bae34c8f30c878aa1c3d7dc172ed2fd1e24d620001fb031
7
+ data.tar.gz: 14b088a8db577c782628458dadc86e40db6b551cb2d31a8bdff9391973469c1624dba5d82ba2c826dc42750a1ae946d2c9cc2a34a358fc83879c63d7f3ad32f1
@@ -15,7 +15,7 @@ module RailsArchiver
15
15
  end
16
16
  end
17
17
 
18
- attr_accessor :transport
18
+ attr_accessor :transport, :archive_location
19
19
 
20
20
  # Create a new Archiver with the given model.
21
21
  # @param model [ActiveRecord::Base] the model to archive or unarchive.
@@ -41,7 +41,7 @@ module RailsArchiver
41
41
  @hash = {}
42
42
  _visit_association(@model)
43
43
  @logger.info('Completed loading data')
44
- @transport.store_archive(@hash)
44
+ @archive_location = @transport.store_archive(@hash)
45
45
  if @model.attribute_names.include?('archived')
46
46
  @model.update_attribute(:archived, true)
47
47
  end
@@ -5,7 +5,8 @@ module RailsArchiver
5
5
  class Base
6
6
 
7
7
  # @param model [ActiveRecord::Base] the model we will be working with.
8
- def initialize(model, logger=nil)
8
+ # @param logger [Logger]
9
+ def initialize(model=nil, logger=nil)
9
10
  @model = model
10
11
  @options = {}
11
12
  @logger = logger || ::Logger.new(STDOUT)
@@ -27,8 +28,11 @@ module RailsArchiver
27
28
 
28
29
  # To be implemented by subclasses. Retrieve the archive that was previously
29
30
  # created.
31
+ # @param location [String] if given, the location of the archive (e.g.
32
+ # S3 key). Otherwise will be figured out from the existing model
33
+ # in the database.
30
34
  # @return [Hash] the retrieved hash.
31
- def retrieve_archive
35
+ def retrieve_archive(location=nil)
32
36
  raise NotImplementedError
33
37
  end
34
38
 
@@ -8,7 +8,7 @@ module RailsArchiver
8
8
  'some-key-here'
9
9
  end
10
10
 
11
- def retrieve_archive
11
+ def retrieve_archive(location=nil)
12
12
  @options[:json]
13
13
  end
14
14
 
@@ -1,5 +1,7 @@
1
1
  require 'aws-sdk'
2
2
  require 'securerandom'
3
+ require 'tmpdir'
4
+
3
5
  # Transport that stores to S3. Uses an archived_s3_key attribute.
4
6
  module RailsArchiver
5
7
  module Transport
@@ -38,14 +40,26 @@ module RailsArchiver
38
40
  @logger.info("Uploading file to #{s3_key}")
39
41
  _save_archive_to_s3(s3_key, filename)
40
42
  end
43
+ if @model.respond_to?(:archived_s3_key)
44
+ @model.update_attribute(:archived_s3_key, s3_key)
45
+ end
41
46
  s3_key
42
- @model.update_attribute(:archived_s3_key, s3_key)
43
47
  end
44
48
 
45
- def retrieve_archive
49
+ # @param location [String]
50
+ def retrieve_archive(location=nil)
46
51
  Dir.mktmpdir do |dir|
47
- filename = "#{dir}/#{@model.id}.json"
48
- _get_archive_from_s3(@model.archived_s3_key, "#{filename}.gz")
52
+ s3_key = location
53
+ filename = nil
54
+ if @model
55
+ if @model.respond_to?(:archived_s3_key)
56
+ s3_key ||= @model.archived_s3_key
57
+ end
58
+ filename = "#{dir}/#{@model.id}.json"
59
+ else
60
+ filename = File.basename(s3_key)
61
+ end
62
+ _get_archive_from_s3(s3_key, "#{filename}.gz")
49
63
  @logger.info('Unzipping file')
50
64
  gunzip("#{filename}.gz")
51
65
  @logger.info('Parsing JSON')
@@ -27,10 +27,13 @@ module RailsArchiver
27
27
  end
28
28
 
29
29
  # Unarchive a model.
30
- def unarchive
30
+ # @param location [String] if given, uses the given location to unarchive.
31
+ # Otherwise uses the existing model in the database (e.g. attribute on the
32
+ # model) depending on the transport being used.
33
+ def unarchive(location=nil)
31
34
  @errors = []
32
35
  @logger.info('Downloading JSON file')
33
- hash = @transport.retrieve_archive
36
+ hash = @transport.retrieve_archive(location)
34
37
  @logger.info("Loading #{@model.class.name}")
35
38
  load_classes(hash)
36
39
  @model.reload
@@ -1,18 +1,18 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rails-archiver'
3
3
  s.require_paths = %w(. lib lib/rails-archiver)
4
- s.version = '0.1.2'
5
- s.date = '2018-05-03'
4
+ s.version = '0.1.3'
5
+ s.date = '2018-05-28'
6
6
  s.summary = 'Fully archive a Rails model'
7
7
  s.description = <<-EOF
8
- EOF
8
+ EOF
9
9
  s.authors = ['Daniel Orner']
10
10
  s.email = 'daniel.orner@wishabi.com'
11
- s.files = `git ls-files`.split($/)
11
+ s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
12
12
  s.homepage = 'https://github.com/dorner/rails-archiver'
13
- s.license = 'MIT'
13
+ s.license = 'MIT'
14
14
 
15
15
  s.add_dependency 'rails', '>= 3.0'
16
- s.add_dependency 'aws-sdk', '~> 2.6'
16
+ s.add_development_dependency 'aws-sdk-s3', '~> 1.13'
17
17
 
18
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-archiver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-03 00:00:00.000000000 Z
11
+ date: 2018-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: aws-sdk
28
+ name: aws-sdk-s3
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.6'
34
- type: :runtime
33
+ version: '1.13'
34
+ type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.6'
40
+ version: '1.13'
41
41
  description: ''
42
42
  email: daniel.orner@wishabi.com
43
43
  executables: []