paperclip_lambda 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d35b731f6e0c7d95080480d60f84b2a1c976ffd
4
+ data.tar.gz: b979c19571f3f31ae84a1fa3266982e52950ccc5
5
+ SHA512:
6
+ metadata.gz: 52971c7e9024b8a343caae85efed17283d1bb0c4ed37962f55fe4c5ff9921daf103664d8abc25acd4d4c4b158a78f079ac8dd3c0f754d8e0201d65f4163d5647
7
+ data.tar.gz: 3c8d718cb930080120d5bff4cb2d39e6d0a5121b6f6a022ee1e240f1425371aa31efdf0b1701303e54f3b70afe571c6eb35b0cface8a8ce38d2f1faed42922a9
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kreeti Technologies Pvt. Ltd.
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.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # paperclip_lambda
@@ -0,0 +1,51 @@
1
+ module PaperclipLambda
2
+ module Attachment
3
+ def self.included(base)
4
+ base.send :include, InstanceMethods
5
+ base.send(:alias_method, :save_without_lambda, :save)
6
+ base.send(:alias_method, :save, :save_with_lambda)
7
+
8
+ base.send(:alias_method, :destroy_without_lambda, :destroy)
9
+ base.send(:alias_method, :destroy, :destroy_with_lambda)
10
+ end
11
+
12
+ module InstanceMethods
13
+ def save_with_lambda
14
+ was_dirty = @dirty
15
+
16
+ save_without_lambda.tap do
17
+ if was_dirty
18
+ instance.prepare_enqueueing_for(name)
19
+ end
20
+ end
21
+ end
22
+
23
+ def process_update_in_lambda(options = {})
24
+ lambda_definitions = instance.class.paperclip_definitions[name][:lambda]
25
+ attributes_hash = { }
26
+
27
+ lambda_definitions[:attributes].each do |attribute|
28
+ attributes_hash[attribute] = instance.send(attribute)
29
+ end
30
+
31
+ payload = {
32
+ path: path,
33
+ old_path: options[:old_path],
34
+ bucket: instance.send(name).options[:bucket],
35
+ attributes: attributes_hash
36
+ }
37
+
38
+ PaperclipLambda::Client.new(lambda_definitions[:function_name], payload)
39
+
40
+ if instance.respond_to?(:"#{name}_processing?")
41
+ instance.send("#{name}_processing=", false)
42
+ instance.class.where(instance.class.primary_key => instance.id).update_all({ "#{name}_processing" => false })
43
+ end
44
+ end
45
+
46
+ def destroy_with_lambda
47
+ instance.prepare_enqueueing_for_deletion(name)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,14 @@
1
+ require 'aws-sdk'
2
+
3
+ module PaperclipLambda
4
+ class Client
5
+ attr_reader :errors
6
+
7
+ def initialize(function_name, payload = {})
8
+ lambda = ::Aws::Lambda::Client.new
9
+ lambda.invoke(function_name: function_name, payload: payload.to_json, invocation_type: "Event")
10
+ rescue ::Aws::Lambda::Errors::ServiceError => e
11
+ @errors = e
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require 'aws-sdk'
2
+
3
+ module PaperclipLambda
4
+ class LambdaJob < ActiveJob::Base
5
+ def perform(klass, object_id, attachment_name, options = {})
6
+ if instance = klass.constantize.find_by_id(object_id)
7
+ instance.send(attachment_name).process_update_in_lambda(options)
8
+ else
9
+ klass.constantize.process_delete_in_lambda(attachment_name, options)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require 'paperclip'
2
+
3
+ module PaperclipLambda
4
+ class Railtie < Rails::Railtie
5
+ initializer "paperclip_lambda.insert_into_active_record" do |app|
6
+ ActiveSupport.on_load :active_record do
7
+ PaperclipLambda::Railtie.insert
8
+ end
9
+ end
10
+ end
11
+
12
+ class Railtie
13
+ def self.insert
14
+ ActiveRecord::Base.send(:include, PaperclipLambda::Glue)
15
+ Paperclip::Attachment.send(:include, PaperclipLambda::Attachment)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,92 @@
1
+ require 'paperclip_lambda/client'
2
+ require 'paperclip_lambda/attachment'
3
+ require 'paperclip_lambda/lambda_job'
4
+ require 'paperclip_lambda/railtie' if defined?(Rails)
5
+
6
+ module PaperclipLambda
7
+ module Glue
8
+ def self.included(base)
9
+ base.extend(ClassMethods)
10
+ base.send :include, InstanceMethods
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def process_in_lambda(name, function_name, options = { })
16
+ paperclip_definitions[name][:lambda] = { }
17
+ paperclip_definitions[name][:lambda][:function_name] = function_name
18
+ paperclip_definitions[name][:lambda][:attributes] = options[:attributes] || []
19
+
20
+ if respond_to?(:after_commit)
21
+ after_commit :enqueue_lambda_processing
22
+ else
23
+ after_save :enqueue_lambda_processing
24
+ end
25
+ end
26
+
27
+ def process_delete_in_lambda(attachment_name, options = {})
28
+ PaperclipLambda::Client.new(paperclip_definitions[:"#{attachment_name}"][:lambda][:function_name], options)
29
+ end
30
+
31
+ def paperclip_definitions
32
+ @paperclip_definitions ||= if respond_to? :attachment_definitions
33
+ attachment_definitions
34
+ end
35
+ end
36
+ end
37
+
38
+ module InstanceMethods
39
+ def enqueue_lambda_processing
40
+ mark_enqueue_lambda_processing
41
+
42
+ (@_attachment_for_lambda_processing || []).each do |name|
43
+ enqueue_post_processing_for(name)
44
+ end
45
+
46
+ (@_attachment_for_lambda_deleting || []).each do |name_and_path|
47
+ unless name_and_path.blank?
48
+ enqueue_delete_processing_for(name_and_path)
49
+ end
50
+ end
51
+
52
+ @_attachment_for_lambda_processing = []
53
+ @_attachment_for_lambda_deleting = []
54
+ end
55
+
56
+ def mark_enqueue_lambda_processing
57
+ unless @_attachment_for_lambda_processing.blank? # catches nil and empty arrays
58
+ updates = @_attachment_for_lambda_processing.collect{|n| "#{n}_processing = :true" }.join(", ")
59
+ updates = ActiveRecord::Base.send(:sanitize_sql_array, [updates, {:true => true}])
60
+ self.class.where(:id => id).update_all(updates)
61
+ end
62
+ end
63
+
64
+ def enqueue_post_processing_for(name)
65
+ attachment_changed = previous_changes[name.to_s + "_updated_at"]
66
+ file_array = previous_changes[name.to_s + "_file_name"]
67
+
68
+ if attachment_changed && attachment_changed.first.present? && (file_array.uniq.length == file_array.length)
69
+ old_path = send(name).path.split('/')
70
+ old_path = old_path.tap(&:pop).concat([file_array.first]).join('/')
71
+ PaperclipLambda::LambdaJob.perform_later(self.class.name, id, name.to_s, { old_path: old_path })
72
+ else
73
+ PaperclipLambda::LambdaJob.perform_later(self.class.name, id, name.to_s)
74
+ end
75
+ end
76
+
77
+ def enqueue_delete_processing_for(name_and_path)
78
+ name, old_path = name_and_path
79
+ PaperclipLambda::LambdaJob.perform_later(self.class.name, id, name.to_s, { bucket: send(name).options[:bucket], old_path: old_path })
80
+ end
81
+
82
+ def prepare_enqueueing_for(name)
83
+ @_attachment_for_lambda_processing ||= []
84
+ @_attachment_for_lambda_processing << name
85
+ end
86
+
87
+ def prepare_enqueueing_for_deletion(name)
88
+ @_attachment_for_lambda_deleting ||= []
89
+ @_attachment_for_lambda_deleting << [name, send(name).path]
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'paperclip_lambda'
5
+ s.version = '0.0.2'
6
+ s.date = '2016-06-17'
7
+ s.summary = "Paperclip Lambda"
8
+ s.add_runtime_dependency "aws-sdk", ["~> 2"]
9
+ s.add_runtime_dependency "paperclip", ["4.2.1"]
10
+ s.description = "Process your uploaded images through aws lambda"
11
+ s.authors = ["Santanu Bhattacharya"]
12
+ s.email = 'eng@kreeti.com'
13
+ s.files = `git ls-files`.split("\n")
14
+ s.homepage = 'http://rubygems.org/gems/paperclip_lambda'
15
+ s.license = 'MIT'
16
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip_lambda
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Santanu Bhattacharya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: paperclip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.1
41
+ description: Process your uploaded images through aws lambda
42
+ email: eng@kreeti.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - LICENSE
49
+ - README.md
50
+ - lib/paperclip_lambda.rb
51
+ - lib/paperclip_lambda/attachment.rb
52
+ - lib/paperclip_lambda/client.rb
53
+ - lib/paperclip_lambda/lambda_job.rb
54
+ - lib/paperclip_lambda/railtie.rb
55
+ - paperclip_lambda.gemspec
56
+ homepage: http://rubygems.org/gems/paperclip_lambda
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.4.6
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Paperclip Lambda
80
+ test_files: []