% -*- mode: latex; -*- mustache tags:  
\ifx\wholebook\relax\else

	\documentclass[10pt,twoside,english]{root/support/latex/sbabook/sbabook}
  \usepackage{import}
  \subimport{root/support/latex/}{common.tex}

  \hypersetup{pdfinfo = {
    Title = {{Pharo Mooc}},
    Author = {},
    Keywords = {pharo, smalltalk}}}
  \begin{document}
\fi

\chapter{Challenge 3 - Solution}
Two little challenges:

\begin{itemize}
\item First, the web site \url{http://api.geonames.org/countryInfoJSON} gives information in JSON about countries and you should extract the capitals. You can use the \textcode{'demo'} and \textcode{''} as login and password.
\item Second, the web site \url{http://www.omdbapi.com/} offers an api to get information about movies. For example using \textcode{t} allows one to specify the title of a movie. Define a query to get the poster of \textcode{'The Terminator'}.
\end{itemize}
\subsection{Hints:}
\begin{itemize}
\item Have a look at \textcode{ZnEasy} or \textcode{ZnClient}. 
\item To get the contents of an HTML request, send it the message \textcode{contents}.
\item To parse and convert a string representing JSON into Pharo collection, use the \textcode{STON} class (check the class side).
\item To pass a binding to a request, use the message \textcode{queryAt:put:}.
\item Using the inspector is a good idea to explore objects and query results.
\end{itemize}


\begin{figure}

\begin{center}
\includegraphics[width=0.8\textwidth]{figures/Terminator.png}\caption{The Poster of Terminator in a Pharo Inspector.\label{figures/Terminator.png}}\end{center}
\end{figure}

\subsection{Solutions }\subsubsection{Getting the Capitals}
\begin{displaycode}{smalltalk}
((STON fromString: (
    ZnEasy
        get: 'http://api.geonames.org/countryInfoJSON'
        username: 'demo'
        password: '') contents) at: #geonames) collect: [ : d | d at: #capital ]
\end{displaycode}
\subsubsection{Get poster of The Terminator movie}
\begin{displaycode}{smalltalk}
(ZnEasy getJpeg: ((STON fromString:
    (ZnClient new
        url: 'http://www.omdbapi.com/';
        queryAt: 't' put: 'The Terminator';
        get;
        contents)) at: #Poster)) inspect
\end{displaycode}

	
{]}{]}{]}


\ifx\wholebook\relax\else
\end{document}
\fi