redis_importer 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # RedisImporter
2
2
 
3
- TODO: Write a gem description
3
+ Takes csv files stored on Amazon S3, converts them to objects and then imports those objects into redis, serialized as redis hashes. This gem is the glue between several other gems:
4
+
5
+ * `csv_to_object` (handles the conversion of a csv file to objects)
6
+ * `redis_backed_model` (Gives an object a to_redis command that serializes an object as a series of redis commands)
7
+ * `redis_pipeline` (Imports large sets of commands into a redis instance)
8
+
9
+ If you have those other gems set up correctly, then RedisImporter is straightforward. Tell it where your csv files are stored and you're good to go.
4
10
 
5
11
  ## Installation
6
12
 
@@ -15,10 +21,22 @@ And then execute:
15
21
  Or install it yourself as:
16
22
 
17
23
  $ gem install redis_importer
24
+
25
+ ## Configuration
26
+
27
+ rails g redis_importer:install
28
+
29
+ 1. Edit config/redis_importer.yml to set your remote file-storage method and what directory should store local copies of the files during import.
30
+ 2. Edit config/s3_config.yml to set your access key, secret access key, bucket name, etc.
31
+
32
+ **At this time only Amazon S3 is supported as a remote file-storage method.**
18
33
 
19
34
  ## Usage
20
35
 
21
- TODO: Write usage instructions here
36
+ ```ruby
37
+ @ri = RedisImporter::RedisImporter.new
38
+ @ri.import => true
39
+ ```
22
40
 
23
41
  ## Contributing
24
42
 
@@ -2,8 +2,7 @@ module RedisImporter
2
2
  class RedisImporter
3
3
  include GemConfigurator
4
4
 
5
- attr_reader :files
6
-
5
+ attr_reader :files, :commands, :errors
7
6
  attr_accessor :collection
8
7
 
9
8
  def initialize
@@ -11,14 +10,16 @@ module RedisImporter
11
10
  self.collection = Object::const_get("#{@settings[:storage_method].camelcase}Collection").new()
12
11
 
13
12
  self.files = self.collection.files
13
+ self.commands = []
14
14
  end
15
15
 
16
16
  def import
17
17
  files.each do |file|
18
- if class_exists?(file.to_class_name)
19
- self.local_path = "tmp/#{file.name}"
20
- file.save_to(local_path)
21
- get_redis_commands
18
+ begin
19
+ convert_to_redis_commands(file) if class_exists?(file.to_class_name)
20
+ rescue NameError
21
+ @errors ||= []
22
+ @errors << "#{file.name} is not matched by a class #{file.to_class_name} in the system."
22
23
  end
23
24
  end
24
25
  pipeline
@@ -26,33 +27,43 @@ module RedisImporter
26
27
 
27
28
  private
28
29
 
29
- attr_writer :files
30
- attr_accessor :local_path
30
+ attr_writer :files, :commands
31
31
 
32
32
  def class_exists?(c)
33
- Object::const_defined?(c)
33
+ Module.const_get(c)
34
34
  end
35
-
35
+
36
+ def convert_to_redis_commands(file)
37
+ local_path = local_storage_path(file)
38
+ file.save_to(local_path)
39
+ convert_objects_to_redis_commands(get_objects(local_path))
40
+ end
41
+
36
42
  def default_settings
37
- {:storage_method => 's3'}
43
+ {:storage_method => 's3', :local_storage_directory => 'tmp'}
38
44
  end
39
45
 
40
- def get_objects
46
+ def get_objects(local_path)
41
47
  CsvToObject::CsvToObject.new(local_path).to_objects
42
48
  end
43
49
 
44
- def get_redis_commands
45
- @commands ||= []
46
- get_objects.each do |obj|
47
- @commands << obj.to_redis
50
+ def convert_objects_to_redis_commands(objects)
51
+ objects.each do |obj|
52
+ self.commands << obj.to_redis
48
53
  end
49
54
  end
50
55
 
56
+ def local_storage_path(file)
57
+ "#{@settings[:local_storage_directory]}/#{file.name}"
58
+ end
59
+
51
60
  def pipeline
52
- if @commands && !@commands.empty?
61
+ if !self.commands.empty?
53
62
  pipeline = RedisPipeline::RedisPipeline.new
54
- pipeline.add_commands(@commands.flatten)
63
+ pipeline.add_commands(self.commands.flatten)
55
64
  pipeline.execute_commands
65
+ else
66
+ self.commands
56
67
  end
57
68
  end
58
69
  end
@@ -1,3 +1,3 @@
1
1
  module RedisImporter
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -3,12 +3,14 @@ require_relative '../test/lib/person.rb'
3
3
 
4
4
  describe RedisImporter do
5
5
  before(:all) do
6
- credentials = YAML.load_file(File.open('config/s3_config.yml'))['development']
7
- connection = AWS::S3::Base.establish_connection! credentials['connection']
6
+ @configuration = YAML.load_file(File.open('config/redis_importer.yml'))['development']
7
+
8
+ collection = YAML.load_file(File.open('config/s3_config.yml'))['development']
9
+ connection = AWS::S3::Base.establish_connection! collection['connection']
8
10
 
9
- bucket_name = credentials['bucket']
11
+ bucket_name = collection['bucket']
10
12
  Bucket.create(bucket_name)
11
- bucket = Bucket.find(bucket_name)
13
+ @bucket = Bucket.find(bucket_name)
12
14
 
13
15
  test_csv_files = Dir.glob("test/csv/*.csv")
14
16
  test_class_names = test_csv_files.map {|f| File.basename(f).gsub('.csv','').capitalize}
@@ -25,21 +27,21 @@ describe RedisImporter do
25
27
  end
26
28
 
27
29
  it "determines its file storage type from a configuration file" do
28
- @ri.settings[:storage_method].should == 's3'
30
+ @ri.settings[:storage_method].should == @configuration['storage_method']
29
31
  end
30
32
 
31
33
  it "uses its storage method to get a file collection module" do
32
- @ri.collection.class.to_s.should == 'S3Collection'
34
+ @ri.collection.class.to_s.downcase.should == "#{@configuration['storage_method']}Collection".downcase
33
35
  end
34
36
 
35
37
  it "checks to see if each file is matched by a class in the system" do
36
- @ri.should_receive(:class_exists?).twice
38
+ @ri.should_receive(:class_exists?).exactly(@bucket.objects.count).times
37
39
  @ri.import
38
40
  end
39
41
 
40
42
  it "only passes files to csv_to_object that are matched by a class in the system" do
41
- @ri.should_receive(:get_redis_commands).once
42
43
  @ri.import
44
+ @ri.errors.should == ["foo.csv is not matched by a class Foo in the system."]
43
45
  end
44
46
 
45
47
  it "passes the redis commands to the redis pipeline" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-24 00:00:00.000000000Z
12
+ date: 2012-08-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2153402260 !ruby/object:Gem::Requirement
16
+ requirement: &2169532440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2153402260
24
+ version_requirements: *2169532440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: gem_configurator
27
- requirement: &2153401840 !ruby/object:Gem::Requirement
27
+ requirement: &2169532020 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153401840
35
+ version_requirements: *2169532020
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: csv_to_object
38
- requirement: &2153401420 !ruby/object:Gem::Requirement
38
+ requirement: &2155980220 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2153401420
46
+ version_requirements: *2155980220
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: redis_pipeline
49
- requirement: &2153401000 !ruby/object:Gem::Requirement
49
+ requirement: &2155979800 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2153401000
57
+ version_requirements: *2155979800
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: aws-s3
60
- requirement: &2160498080 !ruby/object:Gem::Requirement
60
+ requirement: &2155979300 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.6.3
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *2160498080
68
+ version_requirements: *2155979300
69
69
  description: Creates objects, converts them to Redis commands and executes the redis
70
70
  commands.
71
71
  email: