Permalink
Browse files

Remove cookie adapter; bump version

  • Loading branch information...
1 parent 361a417 commit 523eaffa4299c329696f78f3dda27f231db7fe80 @norman committed Mar 10, 2012
Showing with 11 additions and 275 deletions.
  1. +8 −1 Changelog.md
  2. +1 −2 README.md
  3. +0 −111 extras/cookie_demo.rb
  4. +0 −55 lib/ambry/adapters/cookie.rb
  5. +2 −2 lib/ambry/version.rb
  6. +0 −23 lib/rack/ambry.rb
  7. +0 −81 spec/cookie_adapter_spec.rb
View
@@ -1,6 +1,13 @@
# Ambry Changelog
-## [0.2.4](https://github.com/norman/ambry/tree/0.2.3) - 2011-10-07 ([diff](https://github.com/norman/ambry/compare/0.2.3...0.2.4))
+## [0.3.0](https://github.com/norman/ambry/tree/0.3.0) - NOT RELEASED ([diff](https://github.com/norman/ambry/compare/0.2.4...0.3.0))
+
+### [Norman Clarke](https://github.com/norman)
+
+* Remove cookie adapter; keep Ambry focused on its core mission.
+
+
+## [0.2.4](https://github.com/norman/ambry/tree/0.2.4) - 2011-10-07 ([diff](https://github.com/norman/ambry/compare/0.2.3...0.2.4))
### [Norman Clarke](https://github.com/norman)
View
@@ -6,8 +6,7 @@ library with plain old Ruby objects that are searchable via a fast, simple
database-like API.
It implements Active Model and has generators to integrate nicely with Rails.
-You can store your data in a file, a signed string suitable for storage in a
-cookie, or easily write your own IO adapter.
+You can store your data in either YAML or dump file.
For more info, take a peek at the
[docs](http://rubydoc.info/github/ambry/ambry/master/frames), or read on for some
View
@@ -1,111 +0,0 @@
-$LOAD_PATH << File.expand_path("../../lib", __FILE__)
-$LOAD_PATH.uniq!
-
-require "rubygems"
-require "sinatra"
-require "haml"
-require "babosa"
-require "date"
-require "ambry"
-require "ambry/adapters/cookie"
-require "rack/ambry"
-
-set :session, false
-
-use Rack::Cookies
-use Rack::Ambry, :name => :cookie, :secret => "Sssshhhh! This is a secret."
-
-class Book
- extend Ambry::Model
- field :slug, :title, :author
- use :cookie
-
- def title=(value)
- @slug = value.to_slug.normalize.to_s
- @title = value
- end
-end
-
-get "/" do
- @header = "Books"
- @books = Book.all
- haml :index
-end
-
-get "/books/new" do
- @header = "Add a Book"
- @action = "/books"
- haml :new
-end
-
-get "/books/:slug/edit" do |slug|
- @book = Book.get(slug)
- @action = "/books"
- @header = @book.title
- params[:title] = @book.title
- params[:author] = @book.author
- haml :edit
-end
-
-get "/books/:slug" do |slug|
- @book = Book.get(slug)
- @header = @book.title
- haml :book
-end
-
-post "/books" do
- Book.delete params[:slug] if params[:slug]
- @book = Book.create params if params[:title]
- redirect "/"
-end
-
-__END__
-@@layout
-!!! 5
-%html
- %head
- %meta(http-equiv="Content-Type" content="text/html; charset=utf-8")
- %title Ambry Cookie Adapter Demo
- %body
- %h2= @header
- = yield
-
-@@edit
-= haml(:form, :layout => false)
-
-@@index
-%ul
- - @books.each do |book|
- %li= '<a href="/books/%s">%s</a>' % [book.slug, book.title, book.author]
-%p.controls
- <a href="/books/new">New book</a>
-
-@@new
-= haml(:form, :layout => false)
-%p.controls
- <a href="/">Books</a>
-
-@@book
-by #{@book.author}
-%p.controls
- <a href="/">Books</a>
- <a href="/books/#{@book.slug}/edit">Edit</a>
-
-@@form
-%form(method="post" enctype="utf-8" action=@action)
- %p
- - if @book
- %input#slug{:type => "hidden", :value => @book.slug, :name => "slug"}
- %label(for="title") Title:
- %br
- %input#title{:type => "text", :value => params[:title], :name => "title"}
- %p
- %label(for="author") Author:
- %br
- %input#author{:type => "text", :value => params[:author], :name => "author"}
- %p
- %input(type="submit" value="save it")
-- if @book
- %form{:method => "post", :enctype => "utf-8", :action => "/books"}
- %input#slug{:type => "hidden", :value => @book.slug, :name => "slug"}
- %input(type="submit" value="or delete it")
@@ -1,55 +0,0 @@
-require "active_support"
-require "active_support/message_verifier"
-require "zlib"
-
-module Ambry
- module Adapters
-
- # Ambry's cookie adapter allows you to store a Ambry database inside
- # a zipped and signed string suitable for setting as a cookie. This can be
- # useful for modelling things like basic shopping carts or form wizards.
- # Keep in mind the data is signed, so it can't be tampered with. However,
- # the data is not *encrypted*, so somebody that wanted to could unzip and
- # load the cookie data to see what's inside. So don't send this data
- # client-side if it's at all sensitive.
- class Cookie < Adapter
-
- attr :verifier
- attr_accessor :data
-
- MAX_DATA_LENGTH = 4096
-
- def self.max_data_length
- MAX_DATA_LENGTH
- end
-
- def initialize(options)
- @data = options[:data]
- @verifier = ActiveSupport::MessageVerifier.new(options[:secret])
- super
- end
-
- def export_data
- cookie = verifier.generate(Zlib::Deflate.deflate(Marshal.dump(db)))
- length = cookie.bytesize
- if length > Cookie.max_data_length
- raise(AmbryError, "Data is %s bytes, cannot exceed %s" % [length, Cookie.max_data_length])
- end
- cookie
- end
-
- def import_data
- (!data || data.empty?) ? {} : Marshal.load(Zlib::Inflate.inflate(verifier.verify(data)))
- end
-
- def load_database
- @db = import_data
- @db.map(&:freeze)
- end
-
- def save_database
- @data = export_data
- end
- end
- end
-end
View
@@ -1,8 +1,8 @@
module Ambry
module Version
MAJOR = 0
- MINOR = 2
- TINY = 4
+ MINOR = 3
+ TINY = 0
BUILD = nil
STRING = [MAJOR, MINOR, TINY, BUILD].compact.join('.')
end
View
@@ -1,23 +0,0 @@
-require "rack/contrib"
-
-module Rack
- # Rack::Ambry is a middleware that allows you to store a Ambry datbase
- # in a cookie.
- # @see Ambry::Adapters::Cookie
- class Ambry
- def initialize(app, options = {})
- @app = app
- options = options.call if options.respond_to? :call
- @cookie_name = options.delete(:cookie_name) || "ambry_data"
- @ambry = ::Ambry::Adapters::Cookie.new(options.merge(:sync => true))
- end
-
- def call(env)
- @ambry.data = env["rack.cookies"][@cookie_name]
- @ambry.load_database
- status, headers, body = @app.call(env)
- env["rack.cookies"][@cookie_name] = @ambry.export_data
- [status, headers, body]
- end
- end
-end
@@ -1,81 +0,0 @@
-require File.expand_path("../spec_helper", __FILE__)
-require "ambry/adapters/cookie"
-
-class User
- extend Ambry::Model
- field :email, :name
-end
-
-module CookieAdapterSpecHelpers
- def secret
- "ssssshh... this is a secret!"
- end
-
- def sample_data
- # hash = {"User" => {valid_user[:email] => valid_user}}
- # p ActiveSupport::MessageVerifier.new(secret).generate(Zlib::Deflate.deflate(Marshal.dump(hash)))
- "BAgiTnicY+GoZvNU4gwtTi1is2JzDQHxhLPyUx2KkzNy81P1kvNz2awZQqrZrTjzEnNTPZX4" +
- "vfJTFYLBkiAJK67U3MTMHKyaAJGaGlk=--08913fe1c677e4bb0dd34ef90fb22f9027e587f4"
- end
-
- def valid_user
- @valid_user ||= {:name => "Joe Schmoe", :email => "joe@schmoe.com"}
- end
-
- def load_fixtures
- Ambry.adapters.clear
- @adapter = Ambry::Adapters::Cookie.new \
- :name => :cookie,
- :secret => secret
- User.use :cookie, :sync => true
- end
-end
-
-describe Ambry::Adapters::Cookie do
-
- include CookieAdapterSpecHelpers
-
- before { load_fixtures }
- after { Ambry.adapters.clear }
-
- describe Ambry::Adapters::Cookie do
-
- describe "#initialize" do
- it "should decode signed data if given" do
- adapter = Ambry::Adapters::Cookie.new \
- :secret => secret,
- :data => sample_data
- assert_kind_of Hash, adapter.db["User"]
- assert_equal "joe@schmoe.com", adapter.db["User"].keys.first
- end
-
- it "should load properly with nil or blank data" do
- [nil, ""].each_with_index do |arg, index|
- adapter = Ambry::Adapters::Cookie.new \
- :secret => secret,
- :data => arg,
- :name => :"main_#{index}"
- assert_instance_of Hash, adapter.db
- end
- end
- end
-
- describe "#export_data" do
- it "should encode and sign the database" do
- User.create \
- :name => Faker::Name.name,
- :email => Faker::Internet.email
- refute_nil @adapter.export_data
- end
- end
-
- describe "#save_database" do
- it "should raise a AmbryError if signed data exceeds max data length" do
- Ambry::Adapters::Cookie.stubs(:max_data_length).returns(1)
- assert_raises Ambry::AmbryError do
- User.create valid_user
- end
- end
- end
- end
-end

0 comments on commit 523eaff

Please sign in to comment.