Sha256: 329f006f2913905ab6c338018790d63ec93aef0f7e2a93906b98e64bea027ccf

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

require 'url_store/version'
require 'url_store/compact_encoder'
require 'url_store/railtie' if defined?(::Rails) && ::Rails::VERSION::MAJOR >= 3

class UrlStore
  SECRET = 'asdkasjlwqjdqaccxnjkasdfh2313'

  # (convert to base64url <-> RFC4648) and '|'
  # which is not url-safe if you ask ERB/CGI, but browsers accept it
  IN = '+/='
  OUT = '-_|'

  @@defaults = {}
  def self.defaults=(x); @@defaults=x; end

  def self.encode(data)
    new.encode(data)
  end

  def self.decode(string)
    new.decode(string)
  end

  def initialize(options={})
    @options = @@defaults.merge(options)
  end

  def encode(data)
    string = encoder.encode(data)
    string.to_s.tr(IN,OUT)
  end

  def decode(string)
    string = string.to_s.tr(OUT,IN) # convert to base64url <-> RFC4648
    encoder.decode(string)
  end

  private

  def encoder
    options = {:secret => SECRET}.merge(@options)
    if options[:secret] == SECRET
      warn "WARNING: You are using the default secret! Set your own with UrlStore.defaults = {:secret => 'something'}"
    end
    UrlStore::CompactEncoder.new(options)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
url_store-0.4.0 lib/url_store.rb
url_store-0.3.5 lib/url_store.rb
url_store-0.3.4 lib/url_store.rb