Skip to content

Commit

Permalink
Renames build_url back to sign
Browse files Browse the repository at this point in the history
This allows it to be backwards compatible with the existing gem, as
discussed in 58bits/cloudfront-signer#5

## Usage:

```ruby
# In a controller, before serving the images
before_action :set_cookie

def set_cookie
  signed_params = AWS::CF::Signer.signed_params(
                    nil,
                    resource: 'http://images.example.com/*',
                    expires: 1.day.from_now
                  )

  signed_params.each do |name, value|
    cookies["CloudFront-#{name}"] = { value: value,
                                      domain: :all,
                                      httponly: true }
  end
end
```
  • Loading branch information
leonelgalan committed Apr 28, 2015
1 parent d6c350f commit fedcc31
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/cloudfront-signer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def self.is_configured?
#
# Returns a String
def self.sign_url(subject, policy_options = {})
self.build_url(subject, {:remove_spaces => true}, policy_options)
self.sign(subject, {:remove_spaces => true}, policy_options)
end


Expand All @@ -139,37 +139,37 @@ def self.sign_url(subject, policy_options = {})
#
# Returns a String
def self.sign_url_safe(subject, policy_options = {})
self.build_url(subject, {:remove_spaces => true, :html_escape => true}, policy_options)
self.sign(subject, {:remove_spaces => true, :html_escape => true}, policy_options)
end

# Public: Sign a stream path part or filename (spaces are allowed in stream paths
# and so are not removed).
#
# Returns a String
def self.sign_path(subject, policy_options ={})
self.build_url(subject, {:remove_spaces => false}, policy_options)
self.sign(subject, {:remove_spaces => false}, policy_options)
end

# Public: Sign a stream path or filename and HTML encode the result.
#
# Returns a String
def self.sign_path_safe(subject, policy_options ={})
self.build_url(subject, {:remove_spaces => false, :html_escape => true}, policy_options)
self.sign(subject, {:remove_spaces => false, :html_escape => true}, policy_options)
end

# Public: Builds a signed url or stream resource name with optional configuration and
# policy options
#
# Returns a String
def self.build_url(subject, configuration_options = {}, policy_options = {})
def self.sign(subject, configuration_options = {}, policy_options = {})
# If the url or stream path already has a query string parameter - append to that.
separator = subject =~ /\?/ ? '&' : '?'

if configuration_options[:remove_spaces]
subject.gsub!(/\s/, "%20")
end

result = subject + separator + self.sign(subject, policy_options).collect{ |k,v| "#{k}=#{v}" }.join('&')
result = subject + separator + self.signed_params(subject, policy_options).collect{ |k,v| "#{k}=#{v}" }.join('&')

if configuration_options[:html_escape]
return html_encode(result)
Expand All @@ -182,7 +182,7 @@ def self.build_url(subject, configuration_options = {}, policy_options = {})
# It returns raw params to be used in urls or cookies
#
# Returns a Hash
def self.sign(subject, policy_options = {})
def self.signed_params(subject, policy_options = {})
result = {}

if policy_options[:policy_file]
Expand Down

0 comments on commit fedcc31

Please sign in to comment.