transloadit-rails 0.9.2 → 1.0.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.
- data/README.md +102 -63
- data/app/helpers/transloadit_helper.rb +5 -4
- data/lib/transloadit/generators.rb +1 -1
- data/lib/transloadit/rails/engine.rb +70 -0
- data/lib/transloadit/rails/{controller_extensions.rb → params_decoder.rb} +4 -4
- data/lib/transloadit/rails/version.rb +2 -2
- data/lib/transloadit/rails/view_helper.rb +15 -0
- data/lib/transloadit/rails.rb +2 -65
- data/lib/transloadit-rails.rb +1 -1
- data/transloadit-rails.gemspec +1 -1
- metadata +6 -5
- data/lib/transloadit/rails/form_helper.rb +0 -15
data/README.md
CHANGED
@@ -9,19 +9,25 @@ you to automate uploading files through the Transloadit REST API.
|
|
9
9
|
|
10
10
|
## Install
|
11
11
|
|
12
|
-
|
12
|
+
```bash
|
13
|
+
$ gem install transloadit-rails
|
14
|
+
```
|
13
15
|
|
14
16
|
## Getting started
|
15
17
|
|
16
18
|
To get started, you need to add the 'transloadit-rails' gem to your Rails
|
17
19
|
project's Gemfile.
|
18
20
|
|
19
|
-
|
21
|
+
```bash
|
22
|
+
$ echo "gem 'transloadit-rails'" >> Gemfile
|
23
|
+
```
|
20
24
|
|
21
25
|
Now update your bundle and run the generator.
|
22
26
|
|
23
|
-
|
24
|
-
|
27
|
+
```bash
|
28
|
+
$ bundle install
|
29
|
+
$ rails g transloadit:install
|
30
|
+
```
|
25
31
|
|
26
32
|
## Configuration
|
27
33
|
|
@@ -29,21 +35,23 @@ Edit `config/transloadit.yml`. It has an `auth` section for your transloadit
|
|
29
35
|
credentials and a `templates` section to define or refer to existing
|
30
36
|
templates.
|
31
37
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
38
|
+
```yaml
|
39
|
+
auth:
|
40
|
+
key : '4d2e...'
|
41
|
+
secret: '8ad1...' # optional, but highly recommended
|
42
|
+
|
43
|
+
templates:
|
44
|
+
# template identified by template_id
|
45
|
+
s3_store: '4d2e...'
|
46
|
+
|
47
|
+
# template defined inline
|
48
|
+
image_resize:
|
49
|
+
steps:
|
50
|
+
resize:
|
51
|
+
robot : '/image/resize'
|
52
|
+
width : 320
|
53
|
+
height: 200
|
54
|
+
```
|
47
55
|
|
48
56
|
## Usage
|
49
57
|
|
@@ -51,14 +59,27 @@ Refer to the templates with the `transloadify` helper. This requires jQuery,
|
|
51
59
|
and loads the [Transloadit jQuery plugin](https://github.com/transloadit/jquery-sdk).
|
52
60
|
It also uses JavaScript to ensure your form is encoded as `multipart/form-data`.
|
53
61
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
+
```erb
|
63
|
+
<%= form_for :upload, :html => { :id => 'upload' } do |form| %>
|
64
|
+
<%= transloadit :s3_store %>
|
65
|
+
<%= form.label :file, 'File to upload' %>
|
66
|
+
<%= form.file_field :file %>
|
67
|
+
<%= form.submit %>
|
68
|
+
<% end %>
|
69
|
+
|
70
|
+
<%= transloadit_jquerify :upload %>
|
71
|
+
```
|
72
|
+
|
73
|
+
If you want to use the automatic transload parameter decoding, you have to include
|
74
|
+
the Transloadit::Rails::ParamsDecoder module into your controller
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class YourController
|
78
|
+
include Transloadit::Rails::ParamsDecoder
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
that way the param[:transloadit] is automatically decoded for you, if it exists
|
62
83
|
|
63
84
|
## Tutorial
|
64
85
|
|
@@ -67,77 +88,95 @@ setup rails project.
|
|
67
88
|
|
68
89
|
If you haven't already done so, go ahead and install Rails.
|
69
90
|
|
70
|
-
|
91
|
+
```bash
|
92
|
+
$ gem install rails
|
93
|
+
```
|
71
94
|
|
72
95
|
With rails installed, let's create a new app called 'my_app'.
|
73
96
|
|
74
|
-
|
75
|
-
|
97
|
+
```bash
|
98
|
+
$ rails new my_app
|
99
|
+
$ cd my_app
|
100
|
+
```
|
76
101
|
|
77
102
|
In order to use transloadit in this app, we need to add the gem to our Gemfile
|
78
103
|
and bundle things up.
|
79
104
|
|
80
|
-
|
81
|
-
|
105
|
+
```bash
|
106
|
+
$ echo "gem 'transloadit-rails'" >> Gemfile
|
107
|
+
$ bundle install
|
108
|
+
```
|
82
109
|
|
83
110
|
With that in place, it's time to generate our transloadit configuration, as
|
84
111
|
well as a basic UploadsController and a dummy Upload model.
|
85
112
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
113
|
+
```bash
|
114
|
+
$ rails g transloadit:install
|
115
|
+
$ rails g controller uploads new create
|
116
|
+
$ rails g model upload
|
117
|
+
$ rake db:migrate
|
118
|
+
```
|
90
119
|
|
91
120
|
The controller generator we just executed has probably put two GET routes into
|
92
121
|
your `config/routes.rb`. We don't want those, so lets go ahead an overwrite
|
93
122
|
them with this.
|
94
123
|
|
95
|
-
|
96
|
-
|
97
|
-
|
124
|
+
```ruby
|
125
|
+
MyApp::Application.routes.draw do
|
126
|
+
resources :uploads
|
127
|
+
end
|
128
|
+
```
|
98
129
|
|
99
130
|
Next we need to configure our `config/transloadit.yml` file. For this tutorial,
|
100
131
|
just put in your credentials, and define an image resize step as indicated
|
101
132
|
below:
|
102
133
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
134
|
+
```yaml
|
135
|
+
auth:
|
136
|
+
key : '4d2e...'
|
137
|
+
secret: '8ad1...'
|
138
|
+
|
139
|
+
templates:
|
140
|
+
image_resize:
|
141
|
+
steps:
|
142
|
+
resize:
|
143
|
+
robot : '/image/resize'
|
144
|
+
format: 'jpg'
|
145
|
+
width : 320
|
146
|
+
height: 200
|
147
|
+
```
|
115
148
|
|
116
149
|
Alright, time to create our upload form. In order to do that, please open
|
117
150
|
`app/views/uploads/new.html.erb`, and put the following code in:
|
118
151
|
|
119
|
-
|
152
|
+
```erb
|
153
|
+
<%= javascript_include_tag '//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js' %>
|
120
154
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
155
|
+
<h1>Upload an image</h1>
|
156
|
+
<%= form_for Upload.new, :html => { :id => 'upload' } do |form| %>
|
157
|
+
<%= transloadit :image_resize %>
|
158
|
+
<%= form.label :file, 'File to upload' %>
|
159
|
+
<%= form.file_field :file %>
|
160
|
+
<%= form.submit %>
|
161
|
+
<% end %>
|
128
162
|
|
129
|
-
|
163
|
+
<%= transloadit_jquerify :upload, :wait => true %>
|
164
|
+
```
|
130
165
|
|
131
166
|
With this in place, we can modify the `app/views/uploads/create.html.erb` view
|
132
167
|
to render the uploaded and resized image:
|
133
168
|
|
134
|
-
|
135
|
-
|
169
|
+
```erb
|
170
|
+
<h1>Resized upload image</h1>
|
171
|
+
<%= image_tag params[:transloadit][:results][:resize].first[:url] %>
|
172
|
+
```
|
136
173
|
|
137
174
|
That's it. If you've followed the steps closely, you should now be able to
|
138
175
|
try your first upload. Don't forget do start your rails server first:
|
139
176
|
|
140
|
-
|
177
|
+
```bash
|
178
|
+
$ rails server
|
179
|
+
```
|
141
180
|
|
142
181
|
Then go to http://localhost:3000/uploads/new, and upload an image. If you did
|
143
182
|
everything right, you should see the uploaded and resized file as soon as the
|
@@ -5,12 +5,13 @@ module TransloaditHelper
|
|
5
5
|
#
|
6
6
|
def transloadit_jquerify(id, options = {})
|
7
7
|
javascript_tag %{
|
8
|
-
$(
|
8
|
+
$(function() {
|
9
9
|
var script = '//assets.transloadit.com/js/jquery.transloadit2.js';
|
10
|
-
|
10
|
+
|
11
11
|
$.getScript(script, function() {
|
12
|
-
$('##{id}')
|
13
|
-
|
12
|
+
$('##{id}')
|
13
|
+
.attr('enctype', 'multipart/form-data')
|
14
|
+
.transloadit(#{options.to_json});
|
14
15
|
});
|
15
16
|
});
|
16
17
|
}
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'transloadit/rails'
|
2
|
+
require 'transloadit/generators'
|
3
|
+
require 'rails'
|
4
|
+
|
5
|
+
class Transloadit
|
6
|
+
module Rails
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
CONFIG_PATH = 'config/transloadit.yml'
|
9
|
+
|
10
|
+
autoload :ParamsDecoder, 'transloadit/rails/params_decoder'
|
11
|
+
autoload :ViewHelper, 'transloadit/rails/view_helper'
|
12
|
+
|
13
|
+
initializer 'transloadit-rails.action_controller' do |app|
|
14
|
+
ActiveSupport.on_load :action_controller do
|
15
|
+
helper TransloaditHelper
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
initializer 'transloadit-rails.action_view' do |app|
|
20
|
+
ActiveSupport.on_load :action_view do
|
21
|
+
include Transloadit::Rails::ViewHelper
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
initializer 'transloadit.configure' do |app|
|
26
|
+
self.class.application = app
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.configuration
|
30
|
+
YAML.load_file self.application.root.join(CONFIG_PATH)
|
31
|
+
end
|
32
|
+
|
33
|
+
class << self
|
34
|
+
attr_accessor :application
|
35
|
+
|
36
|
+
extend ActiveSupport::Memoizable
|
37
|
+
memoize :configuration unless ::Rails.env.development?
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Returns the Transloadit authentication object.
|
42
|
+
#
|
43
|
+
def self.transloadit
|
44
|
+
Transloadit.new(
|
45
|
+
:key => self.configuration['auth']['key'],
|
46
|
+
:secret => self.configuration['auth']['secret']
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
#
|
51
|
+
# Creates an assembly for the named template.
|
52
|
+
#
|
53
|
+
def self.template(name, options = {})
|
54
|
+
template = self.configuration['templates'].try(:fetch, name.to_s)
|
55
|
+
|
56
|
+
self.transloadit.assembly case template
|
57
|
+
when String then { :template_id => template }.merge(options)
|
58
|
+
when Hash then template .merge(options)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
# Signs a request to the Transloadit API.
|
64
|
+
#
|
65
|
+
def self.sign(params)
|
66
|
+
Transloadit::Request.send :_hmac, self.transloadit.secret, params
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'transloadit/rails'
|
2
2
|
|
3
|
-
module Transloadit::Rails::
|
3
|
+
module Transloadit::Rails::ParamsDecoder
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
included do
|
7
7
|
before_filter :decode_transloadit_json
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def decode_transloadit_json
|
11
11
|
return unless params[:transloadit].present?
|
12
|
-
|
12
|
+
|
13
13
|
# wrap transloadit params in a HashWithIndifferentAccess
|
14
14
|
params[:transloadit] = ActiveSupport::HashWithIndifferentAccess.new(
|
15
15
|
ActiveSupport::JSON.decode params[:transloadit]
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'transloadit/rails/engine'
|
2
|
+
|
3
|
+
module Transloadit::Rails::ViewHelper
|
4
|
+
#
|
5
|
+
# Inserts hidden fields specifying and signing the template for Transloadit
|
6
|
+
# to process.
|
7
|
+
#
|
8
|
+
def transloadit(template, options = {})
|
9
|
+
params = Transloadit::Rails::Engine.template(template, options).to_json
|
10
|
+
signature = Transloadit::Rails::Engine.sign(params)
|
11
|
+
|
12
|
+
hidden_field_tag(:params, params, :id => nil) +
|
13
|
+
hidden_field_tag(:signature, signature, :id => nil)
|
14
|
+
end
|
15
|
+
end
|
data/lib/transloadit/rails.rb
CHANGED
@@ -1,66 +1,3 @@
|
|
1
1
|
require 'transloadit'
|
2
|
-
require 'transloadit/
|
3
|
-
require 'rails'
|
4
|
-
|
5
|
-
class Transloadit::Rails < Rails::Engine
|
6
|
-
CONFIG_PATH = 'config/transloadit.yml'
|
7
|
-
|
8
|
-
autoload :ControllerExtensions, 'transloadit/rails/controller_extensions'
|
9
|
-
autoload :FormHelper, 'transloadit/rails/form_helper'
|
10
|
-
|
11
|
-
config.to_prepare do
|
12
|
-
# add the two helpers to the view stack
|
13
|
-
class ActionController::Base
|
14
|
-
include Transloadit::Rails::ControllerExtensions
|
15
|
-
helper TransloaditHelper
|
16
|
-
end
|
17
|
-
|
18
|
-
class ActionView::Helpers::FormBuilder
|
19
|
-
include Transloadit::Rails::FormHelper
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
initializer 'transloadit.configure' do |app|
|
24
|
-
self.class.application = app
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.configuration
|
28
|
-
YAML.load_file self.application.root.join(CONFIG_PATH)
|
29
|
-
end
|
30
|
-
|
31
|
-
class << self
|
32
|
-
attr_accessor :application
|
33
|
-
|
34
|
-
extend ActiveSupport::Memoizable
|
35
|
-
memoize :configuration unless Rails.env.development?
|
36
|
-
end
|
37
|
-
|
38
|
-
#
|
39
|
-
# Returns the Transloadit authentication object.
|
40
|
-
#
|
41
|
-
def self.transloadit
|
42
|
-
Transloadit.new(
|
43
|
-
:key => self.configuration['auth']['key'],
|
44
|
-
:secret => self.configuration['auth']['secret']
|
45
|
-
)
|
46
|
-
end
|
47
|
-
|
48
|
-
#
|
49
|
-
# Creates an assembly for the named template.
|
50
|
-
#
|
51
|
-
def self.template(name, options = {})
|
52
|
-
template = self.configuration['templates'].try(:fetch, name.to_s)
|
53
|
-
|
54
|
-
self.transloadit.assembly case template
|
55
|
-
when String then { :template_id => template }.merge(options)
|
56
|
-
when Hash then template .merge(options)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
#
|
61
|
-
# Signs a request to the Transloadit API.
|
62
|
-
#
|
63
|
-
def self.sign(params)
|
64
|
-
Transloadit::Request.send :_hmac, self.transloadit.secret, params
|
65
|
-
end
|
66
|
-
end
|
2
|
+
require 'transloadit/rails/engine'
|
3
|
+
require 'transloadit/rails/version'
|
data/lib/transloadit-rails.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'transloadit/rails'
|
1
|
+
require 'transloadit/rails'
|
data/transloadit-rails.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.require_paths = %w{ lib }
|
23
23
|
|
24
24
|
gem.add_dependency 'transloadit'
|
25
|
-
gem.add_dependency '
|
25
|
+
gem.add_dependency 'railties', '~> 3'
|
26
26
|
|
27
27
|
gem.add_development_dependency 'rake'
|
28
28
|
gem.add_development_dependency 'minitest' # needed for < 1.9.2
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: transloadit-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 1.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Stephen
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-
|
14
|
+
date: 2011-06-09 00:00:00 -04:00
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
type: :runtime
|
27
27
|
version_requirements: *id001
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
|
-
name:
|
29
|
+
name: railties
|
30
30
|
prerelease: false
|
31
31
|
requirement: &id002 !ruby/object:Gem::Requirement
|
32
32
|
none: false
|
@@ -111,9 +111,10 @@ files:
|
|
111
111
|
- lib/transloadit-rails.rb
|
112
112
|
- lib/transloadit/generators.rb
|
113
113
|
- lib/transloadit/rails.rb
|
114
|
-
- lib/transloadit/rails/
|
115
|
-
- lib/transloadit/rails/
|
114
|
+
- lib/transloadit/rails/engine.rb
|
115
|
+
- lib/transloadit/rails/params_decoder.rb
|
116
116
|
- lib/transloadit/rails/version.rb
|
117
|
+
- lib/transloadit/rails/view_helper.rb
|
117
118
|
- test/generators/transloadit/test_install_generator.rb
|
118
119
|
- test/test_helper.rb
|
119
120
|
- transloadit-rails.gemspec
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'transloadit/rails'
|
2
|
-
|
3
|
-
module Transloadit::Rails::FormHelper
|
4
|
-
#
|
5
|
-
# Inserts hidden fields specifying and signing the template for Transloadit
|
6
|
-
# to process.
|
7
|
-
#
|
8
|
-
def transloadit(template, options = {})
|
9
|
-
params = Transloadit::Rails.template(template, options).to_json
|
10
|
-
signature = Transloadit::Rails.sign(params)
|
11
|
-
|
12
|
-
@template.hidden_field_tag(:params, params, :id => nil) +
|
13
|
-
@template.hidden_field_tag(:signature, signature, :id => nil)
|
14
|
-
end
|
15
|
-
end
|