14.1 Iteration I1: Email Notifications 12.4 Iteration G2: Downloading an eBook
Add the xml format to the controller
edit app/controllers/products_controller.rb
def who_bought
@product = Product.find(params[:id])
@latest_order = @product.orders.order(:updated_at).last
if stale?(@latest_order)
respond_to do |format|
format.xml { render :xml => @product }
format.atom
end
end
end
Fetch the XML, see that there are no orders there
curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.atom
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
<id>tag:localhost,2005:/products/2/who_bought</id>
<link rel="alternate" type="text/html" href="http://localhost:3000"/>
<link rel="self" type="application/atom+xml" href="http://localhost:3000/products/2/who_bought.atom"/>
<title>Who bought Rails, Angular, Postgres, and Bootstrap</title>
<updated>2017-06-02T13:42:00Z</updated>
<entry>
<id>tag:localhost,2005:Order/1</id>
<published>2017-06-02T13:41:58Z</published>
<updated>2017-06-02T13:41:58Z</updated>
<link rel="alternate" type="text/html" href="http://localhost:3000/orders/1"/>
<title>Order 1</title>
<summary type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Shipped to 123 Main St</p>
<table>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
<tr>
<td>Rails, Angular, Postgres, and Bootstrap</td>
<td>1</td>
<td>$45.00</td>
</tr>
<tr>
<th colspan="2">total</th>
<th>$45.00</th>
</tr>
</table>
<p>Paid by Check</p>
</div>
</summary>
<author>
<name>Dave Thomas</name>
<email>customer@example.com</email>
</author>
</entry>
<entry>
<id>tag:localhost,2005:Order/2</id>
<published>2017-06-02T13:42:00Z</published>
<updated>2017-06-02T13:42:00Z</updated>
<link rel="alternate" type="text/html" href="http://localhost:3000/orders/2"/>
<title>Order 2</title>
<summary type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Shipped to 123 Main St</p>
<table>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
<tr>
<td>Rails, Angular, Postgres, and Bootstrap</td>
<td>1</td>
<td>$45.00</td>
</tr>
<tr>
<th colspan="2">total</th>
<th>$45.00</th>
</tr>
</table>
<p>Paid by Check</p>
</div>
</summary>
<author>
<name>Dave Thomas</name>
<email>customer@example.com</email>
</author>
</entry>
</feed>
Include "orders" in the response
edit app/controllers/products_controller.rb
def who_bought
@product = Product.find(params[:id])
@latest_order = @product.orders.order(:updated_at).last
if stale?(@latest_order)
respond_to do |format|
format.xml { render xml: @product.to_xml(include: :orders) }
format.atom
end
end
end
Fetch the xml, see that the orders are included
curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.xml
<?xml version="1.0" encoding="UTF-8"?>
<product>
<id type="integer">2</id>
<title>Rails, Angular, Postgres, and Bootstrap</title>
<description><p>
<em>Powerful, Effective, and Efficient Full-Stack Web Development</em>
As a Rails developer, you care about user experience and performance,
but you also want simple and maintainable code. Achieve all that by
embracing the full stack of web development, from styling with
Bootstrap, building an interactive user interface with AngularJS, to
storing data quickly and reliably in PostgreSQL. Take a holistic view of
full-stack development to create usable, high-performing applications,
and learn to use these technologies effectively in a Ruby on Rails
environment.
</p></description>
<image-url>dcbang.jpg</image-url>
<price type="decimal">45.0</price>
<created-at type="dateTime">2017-06-02T13:40:44Z</created-at>
<updated-at type="dateTime">2017-06-02T13:40:44Z</updated-at>
<orders type="array">
<order>
<id type="integer">1</id>
<name>Dave Thomas</name>
<address>123 Main St</address>
<email>customer@example.com</email>
<pay-type type="integer">Check</pay-type>
<created-at type="dateTime">2017-06-02T13:41:58Z</created-at>
<updated-at type="dateTime">2017-06-02T13:41:58Z</updated-at>
</order>
<order>
<id type="integer">2</id>
<name>Dave Thomas</name>
<address>123 Main St</address>
<email>customer@example.com</email>
<pay-type type="integer">Check</pay-type>
<created-at type="dateTime">2017-06-02T13:42:00Z</created-at>
<updated-at type="dateTime">2017-06-02T13:42:00Z</updated-at>
</order>
</orders>
</product>
Define an HTML view
edit app/views/products/who_bought.html.erb
<h3>People Who Bought <%= @product.title %></h3>
<ul>
<% for order in @product.orders %>
<li>
<%= mail_to order.email, order.name %>
</li>
<% end %>
</ul>
Add the html format to the controller
edit app/controllers/products_controller.rb
def who_bought
@product = Product.find(params[:id])
@latest_order = @product.orders.order(:updated_at).last
if stale?(@latest_order)
respond_to do |format|
format.html
format.xml { render xml: @product.to_xml(include: :orders) }
format.atom
end
end
end
See the (raw) HTML
curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought
<!DOCTYPE html>
<html>
<head>
<title>Pragprog Books Online Store</title>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="YSibgT4uWezt7BlrhbAakn6GLg+0pe4MQoKY6ZqqfXxaPh2W0kG+QrI7L7IRiaTomOZf4JgO4gu0K6aESGHMpQ==" />
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="HbzJC+Ud0ElLj16A/276H/4cG4EiLEKab/sbCfPFtmsmqk8cCXI35xRYaFlrV0RlGHxqbg6HTp2ZUiVkIQ4Hsg==" />
<link rel="stylesheet" media="all" href="/assets/carts.self-ff21c0e6b529ec28d12e6747cc2a800768e948bc5c6591e56988e3fb2ee36f80.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/line_items.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/orders.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/products.self-96b6ee27a05d0434664bb26b7ec7b15982acd6672cafd515e33bde897264a9f1.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/scaffolds.self-54c53249c08b0867d2120e3c07300bb58007c1eac15e9a8c8b54070b5ccf2e54.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/store.self-7f0645eed9bbbc52710a49fc0e19b183aa6ec7ea2795d192f17699950672fc43.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/application.self-396148a417424e0c1a24582cdb25f9e1357806e3ca2923cafa6db7880e18994b.css?body=1" data-turbolinks-track="reload" />
<script src="/assets/jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/jquery-ui/version.self-c8e3d1203da26ea7efdf83c1eabb3f0ba55cb68e463f5ccf0d77bd15ce6a8e61.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/jquery-ui/effect.self-776455da1682afff3a4974146aa96ca840597b879ed3797af0604063527443c6.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/jquery-ui/effects/effect-blind.self-2f8923f5c3073717dad35ac1f9bd4dbc13d2f9c8128f7dbea9819921338271f4.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/carts.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/line_items.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/orders.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/products.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/store.self-819cf990a0c9148fcb1844c453caf30ee5f41f1b53eaad854eacb116ec86ca7d.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/application.self-6e7bd71c4ac009a95ac950347ce34172c3745a8987d2c92c938d884bf9cfc4a0.js?body=1" data-turbolinks-track="reload"></script>
</head>
<body class="products">
<div id="banner">
<img alt="The Pragmatic Bookshelf" src="/assets/logo-a8906fb6c933f00883d2ad0385ae52201c0e1d1f9235c37f4a5762f02ad80936.svg" />
<span class="title"></span>
</div>
<div id="columns">
<div id="side">
<!-- START_HIGHLIGHT -->
<!-- END_HIGHLIGHT -->
<ul>
<li><a href="http://www....">Home</a></li>
<li><a href="http://www..../faq">Questions</a></li>
<li><a href="http://www..../news">News</a></li>
<li><a href="http://www..../contact">Contact</a></li>
</ul>
</div>
<div id="main">
<h3>People Who Bought Rails, Angular, Postgres, and Bootstrap</h3>
<ul>
<li>
<a href="mailto:customer@example.com">Dave Thomas</a>
</li>
<li>
<a href="mailto:customer@example.com">Dave Thomas</a>
</li>
</ul>
</div>
</div>
</body>
</html>
Anything that XML can do, JSON can too...
edit app/controllers/products_controller.rb
def who_bought
@product = Product.find(params[:id])
@latest_order = @product.orders.order(:updated_at).last
if stale?(@latest_order)
respond_to do |format|
format.html
format.xml { render xml: @product.to_xml(include: :orders) }
format.atom
format.json { render json: @product.to_json(include: :orders) }
end
end
end
Fetch the data in JSON format
curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.json
{"id":2,"title":"Rails, Angular, Postgres, and Bootstrap","description":"\u003cp\u003e\n \u003cem\u003ePowerful, Effective, and Efficient Full-Stack Web Development\u003c/em\u003e\n As a Rails developer, you care about user experience and performance,\n but you also want simple and maintainable code. Achieve all that by\n embracing the full stack of web development, from styling with\n Bootstrap, building an interactive user interface with AngularJS, to\n storing data quickly and reliably in PostgreSQL. Take a holistic view of\n full-stack development to create usable, high-performing applications,\n and learn to use these technologies effectively in a Ruby on Rails\n environment.\n \u003c/p\u003e","image_url":"dcbang.jpg","price":"45.0","created_at":"2017-06-02T13:40:44.610Z","updated_at":"2017-06-02T13:40:44.610Z","orders":[{"id":1,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2017-06-02T13:41:58.059Z","updated_at":"2017-06-02T13:41:58.059Z"},{"id":2,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2017-06-02T13:42:00.770Z","updated_at":"2017-06-02T13:42:00.770Z"}]}
Customize the xml
edit app/views/products/who_bought.xml.builder
xml.order_list(:for_product => @product.title) do
for o in @product.orders
xml.order do
xml.name(o.name)
xml.email(o.email)
end
end
end
Change the rendering to use templates
edit app/controllers/products_controller.rb
def who_bought
@product = Product.find(params[:id])
@latest_order = @product.orders.order(:updated_at).last
if stale?(@latest_order)
respond_to do |format|
format.html
format.xml
format.atom
format.json { render json: @product.to_json(include: :orders) }
end
end
end
Fetch the (much streamlined) XML
curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.xml
<order_list for_product="Rails, Angular, Postgres, and Bootstrap">
<order>
<name>Dave Thomas</name>
<email>customer@example.com</email>
</order>
<order>
<name>Dave Thomas</name>
<email>customer@example.com</email>
</order>
</order_list>
Verify that the tests still pass
rake test
Run options: --seed 7233
# Running:
.......................................
Finished in 0.618021s, 63.1046 runs/s, 152.0983 assertions/s.
39 runs, 94 assertions, 0 failures, 0 errors, 0 skips
Commit
git commit -a -m "Orders"
[master bf46cb1] Orders
9 files changed, 101 insertions(+), 44 deletions(-)
git tag iteration-g
14.1 Iteration I1: Email Notifications 12.4 Iteration G2: Downloading an eBook