store.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. require 'builder'
  18. require 'active_record'
  19. ActiveRecord::Base.establish_connection(
  20. :adapter => 'sqlite3',
  21. :database => 'db/development.sqlite3')
  22. class Product < ActiveRecord::Base
  23. end
  24. class StoreApp
  25. def call(env)
  26. x = Builder::XmlMarkup.new :indent=>2
  27. x.declare! :DOCTYPE, :html
  28. x.html do
  29. x.head do
  30. x.title 'Pragmatic Bookshelf'
  31. end
  32. x.body do
  33. x.h1 'Pragmatic Bookshelf'
  34. Product.all.each do |product|
  35. x.h2 product.title
  36. x << " #{product.description}\n"
  37. x.p product.price
  38. end
  39. end
  40. end
  41. response = Rack::Response.new(x.target!)
  42. response['Content-Type'] = 'text/html'
  43. response.finish
  44. end
  45. end