13.1 Iteration H1: Email Notifications 12.3 Iteration G3: Downloading an eBook
add activemodel-serializers-xml
edit Gemfile
gem 'activemodel-serializers-xml'
bundle install --local
Resolving dependencies...
Using rake 12.0.0
Using concurrent-ruby 1.0.5
Using i18n 0.8.1
Using minitest 5.10.1
Using thread_safe 0.3.6
Using builder 3.2.3
Using erubi 1.6.0
Using mini_portile2 2.1.0
Using rack 2.0.1
Using nio4r 2.0.0
Using websocket-extensions 0.1.2
Using mime-types-data 3.2016.0521
Using arel 8.0.0
Using public_suffix 2.0.5
Using bundler 1.14.6
Using byebug 9.0.6
Using ffi 1.9.18
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using method_source 0.8.2
Using thor 0.19.4
Using debug_inspector 0.0.2
Using multi_json 1.12.1
Using pg 0.19.0
Using puma 3.8.2
Using rubyzip 1.2.1
Using sass 3.4.23
Using websocket 1.2.4
Using sqlite3 1.3.13
Using turbolinks-source 5.0.0
Using tzinfo 1.2.2
Using nokogiri 1.7.1
Using rack-test 0.6.3
Using sprockets 4.0.0.beta4
Using websocket-driver 0.6.5
Using mime-types 3.1
Using addressable 2.5.0
Using childprocess 0.6.2
Using rb-inotify 0.9.7 from source at `/home/rubys/git/rb-inotify`
Using coffee-script 2.4.1
Using uglifier 3.1.9
Using queue_classic 3.2.0.RC1 from source at `/home/rubys/git/queue_classic`
Using turbolinks 5.0.1
Using activesupport 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using loofah 2.0.3
Using xpath 2.0.0
Using mail 2.6.4
Using selenium-webdriver 3.3.0
Using rails-dom-testing 2.0.2
Using globalid 0.3.7
Using activemodel 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using jbuilder 2.6.3
Using spring 2.0.1
Using rails-html-sanitizer 1.0.3
Using capybara 2.13.0
Using activejob 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using activerecord 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using actionview 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using activemodel-serializers-xml 1.0.1
Using actionpack 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using actioncable 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using actionmailer 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using railties 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using sprockets-rails 3.2.0
Using coffee-rails 4.2.1
Using jquery-rails 4.2.2
Using jquery-ui-rails 6.0.1
Using web-console 3.4.0 from source at `/home/rubys/git/web-console`
Using rails 5.1.0.beta1 from source at `/home/rubys/git/rails`
Using sass-rails 6.0.0.beta1 from source at `/home/rubys/git/sass-rails`
Bundle complete! 19 Gemfile dependencies, 70 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
include xml serializers
edit app/models/product.rb
require 'active_model/serializers/xml'
class Product < ApplicationRecord
include ActiveModel::Serializers::Xml
end
edit app/models/order.rb
require 'active_model/serializers/xml'
class Order < ApplicationRecord
include ActiveModel::Serializers::Xml
end
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-03-20T04:43:17Z</updated>
<entry>
<id>tag:localhost,2005:Order/1</id>
<published>2017-03-20T04:43:17Z</published>
<updated>2017-03-20T04:43:17Z</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-03-20T04:43:17Z</published>
<updated>2017-03-20T04:43:17Z</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-03-20T04:41:22Z</created-at>
<updated-at type="dateTime">2017-03-20T04:41:22Z</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>Check</pay-type>
<created-at type="dateTime">2017-03-20T04:43:17Z</created-at>
<updated-at type="dateTime">2017-03-20T04:43:17Z</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>Check</pay-type>
<created-at type="dateTime">2017-03-20T04:43:17Z</created-at>
<updated-at type="dateTime">2017-03-20T04:43:17Z</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="BWXKHgmJOIgkCSE4GuVj0+PGmLR5qe5bvNbwfNqbq0cynKuaAARuEX7oFtGKOjO0MbMNn7VAttgLygCScZo9jQ==" />
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="hFXN9cHc8DnlnC8zHeEevrDhoBSCKwn0R7urUcZqxzSzrKxxyFGmoL99GNqNPk7ZYpQ1P07CUXfwp1u/bWtR/g==" />
<link rel="stylesheet" media="all" href="/assets/application.debug-627577cd0d1b9e4f17d79719b946e4073b75bb489aa1fe336c8276351387ebbf.css" data-turbolinks-track="reload" />
<script src="/assets/application.debug-d9266926a81cc8064b243215603dd0f66ba7e38a1cfc587cb1e34fdf2b9e7c3f.js" 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-03-20T04:41:22.078Z","updated_at":"2017-03-20T04:41:22.078Z","orders":[{"id":1,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2017-03-20T04:43:17.064Z","updated_at":"2017-03-20T04:43:17.064Z"},{"id":2,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2017-03-20T04:43:17.625Z","updated_at":"2017-03-20T04:43:17.625Z"}]}
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
rails test
Run options: --seed 2216
# Running:
.......................................
Finished in 0.554924s, 70.2798 runs/s, 140.5597 assertions/s.
39 runs, 78 assertions, 0 failures, 0 errors, 0 skips
Commit
git commit -a -m "Orders"
[master cc23595] Orders
7 files changed, 83 insertions(+), 41 deletions(-)
git tag iteration-g
13.1 Iteration H1: Email Notifications 12.3 Iteration G3: Downloading an eBook