Class: S3FormPresenter::Form
- Inherits:
-
Object
- Object
- S3FormPresenter::Form
- Defined in:
- lib/s3_form_presenter.rb
Constant Summary collapse
- HIDDEN_FIELD_NAMES =
:key, :access_key, :secret_key, :acl, :redirect_url, :policy, :signature
- ACCESSOR_FIELDS =
HIDDEN_FIELD_NAMES - [:policy, :signature]
- RENAMED_FIELDS =
{:redirect_url => "success_action_redirect", :access_key => "AWSAccessKeyId"}
- REQUIRED_ATTRIBUTES =
[:bucket] + HIDDEN_FIELD_NAMES
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#extra_form_attributes ⇒ Object
Returns the value of attribute extra_form_attributes.
-
#inner_content ⇒ Object
Returns the value of attribute inner_content.
Instance Method Summary collapse
- #footer ⇒ Object
- #header ⇒ Object
- #hidden_field(name, value) ⇒ Object
- #hidden_fields ⇒ Object
-
#initialize(key, redirect_url, options = {}, &block) ⇒ Form
constructor
A new instance of Form.
- #policy ⇒ Object
- #policy_object ⇒ Object
- #signature ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(key, redirect_url, options = {}, &block) ⇒ Form
Returns a new instance of Form.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/s3_form_presenter.rb', line 13 def initialize(key, redirect_url, ={}, &block) @key = key @access_key = [:access_key] || ENV["AWS_ACCESS_KEY_ID"] @secret_key = [:secret_key] || ENV["AWS_SECRET_ACCESS_KEY"] @bucket = [:bucket] || ENV["AWS_S3_BUCKET"] @acl = [:acl] || :private @extra_form_attributes = [:extra_form_attributes] @redirect_url = redirect_url if block_given? @inner_content = block.call else @inner_content = %Q(<input name="file" type="file"><input type="submit" value="Upload File" class="btn btn-primary">) end generate_hidden_field_accessors end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
11 12 13 |
# File 'lib/s3_form_presenter.rb', line 11 def bucket @bucket end |
#extra_form_attributes ⇒ Object
Returns the value of attribute extra_form_attributes.
11 12 13 |
# File 'lib/s3_form_presenter.rb', line 11 def extra_form_attributes @extra_form_attributes end |
#inner_content ⇒ Object
Returns the value of attribute inner_content.
11 12 13 |
# File 'lib/s3_form_presenter.rb', line 11 def inner_content @inner_content end |
Instance Method Details
#footer ⇒ Object
33 34 35 |
# File 'lib/s3_form_presenter.rb', line 33 def %Q(</form>) end |
#header ⇒ Object
29 30 31 |
# File 'lib/s3_form_presenter.rb', line 29 def header %Q(<form action="https://#{bucket}.s3.amazonaws.com/" method="post" enctype="multipart/form-data"#{extra_form_attributes}>) end |
#hidden_field(name, value) ⇒ Object
43 44 45 46 |
# File 'lib/s3_form_presenter.rb', line 43 def hidden_field(name, value) name = RENAMED_FIELDS[name] || name %Q(<input type="hidden" name="#{name}" value="#{value}">) end |
#hidden_fields ⇒ Object
37 38 39 40 41 |
# File 'lib/s3_form_presenter.rb', line 37 def hidden_fields HIDDEN_FIELD_NAMES.map do |field| hidden_field(field, send(field)) end end |
#policy ⇒ Object
57 58 59 |
# File 'lib/s3_form_presenter.rb', line 57 def policy Base64.encode64(policy_object.to_json).gsub(/\n|\r/, '') end |
#policy_object ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/s3_form_presenter.rb', line 65 def policy_object { "expiration" => policy_expiration, "conditions" => [ {"bucket" => bucket}, ["starts-with", "$key", "#{key}"], {"acl" => acl}, {"success_action_redirect" => redirect_url} ] } end |
#signature ⇒ Object
61 62 63 |
# File 'lib/s3_form_presenter.rb', line 61 def signature Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_key, policy)).gsub("\n","") end |
#to_html ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/s3_form_presenter.rb', line 48 def to_html validate_required content = "" content += header content += hidden_fields.join content += inner_content content += end |