store.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. require 'builder'
  10. require 'active_record'
  11. ActiveRecord::Base.establish_connection(
  12. adapter: 'sqlite3',
  13. database: 'db/development.sqlite3')
  14. class Product < ActiveRecord::Base
  15. end
  16. class StoreApp
  17. def call(env)
  18. x = Builder::XmlMarkup.new :indent=>2
  19. x.declare! :DOCTYPE, :html
  20. x.html do
  21. x.head do
  22. x.title 'Pragmatic Bookshelf'
  23. end
  24. x.body do
  25. x.h1 'Pragmatic Bookshelf'
  26. Product.all.each do |product|
  27. x.h2 product.title
  28. x << " #{product.description}\n"
  29. x.p product.price
  30. end
  31. end
  32. end
  33. response = Rack::Response.new(x.target!)
  34. response['Content-Type'] = 'text/html'
  35. response.finish
  36. end
  37. end