public class JsoupMachine extends Object
RecipeRepository
is to
create a new AssemblyRecipe
. This object collects the output from the other methods here
so that it can be passed back to the frontend. The raw HTML is filtered to eliminate things like
videos and ads, and the plain text of the remaining elements is added to the AssemblyRecipe and
passed back to the UI. The class
attribute of the Strings the user selects are used to
sort through the filtered HTML elements. Everything with a matching class
attribute is
collected in one of two lists. Raw ingredients are separated using Regex into a measurement
quantity, measurement unit, and ingredient name, then converted to an Ingredient
. Recipe
instructions along with their position in the list create Step
s. The completed
AssemblyRecipe is then passed back to the UI for further editing by the user.Modifier and Type | Method and Description |
---|---|
protected void |
buildIngredients()
Creates an Ingredient by separating the ingredient String into a quantity, unit, and name.
|
protected void |
buildSteps()
Creates a Step out of an instruction String and its position (starting at 1) in the list.
|
protected String |
getClass(String text,
String type)
This method searches the body of the HTML
Document for the text of the given String , and returns the "class" HTML attribute of the first element containing that text. |
protected List<String> |
getClassContents(String klass)
this method takes an HTML "class" attribute and compiles as
Element s
the contents of each matching HTML element. |
static JsoupMachine |
getInstance() |
Single<List<String>> |
prepare(org.jsoup.nodes.Document doc)
This is the first method called in the processing chain.
|
AssemblyRecipe |
process(String ingredient,
String instruction)
This method sets up two threads; one extracts ingredient text and the other extracts
instruction text.
|
void |
setDocument(org.jsoup.nodes.Document document) |
public static JsoupMachine getInstance()
public Single<List<String>> prepare(org.jsoup.nodes.Document doc)
AssemblyRecipe
using the URL and title of the document, then initiates a callable that filters
HTML elements.doc
- is received from RecipeRepository
after a successful connection attempt.public AssemblyRecipe process(String ingredient, String instruction)
ingredient
- received from user input on the SelectionFragment.instruction
- received from user input on the SelectionFragment.protected String getClass(String text, String type)
Document
for the text of the given String
, and returns the "class" HTML attribute of the first element containing that text. Runs
once on each text parameter. This method needs to be improved to handle no or multiple matches
and request additional input from the user.text
- is either the ingredient or instruction text input by the userString
.protected List<String> getClassContents(String klass)
Element
s
the contents of each matching HTML element. Runs once on each text parameter.klass
- the values returned by getClass(String, String)
String
.protected void buildSteps()
protected void buildIngredients()
public void setDocument(org.jsoup.nodes.Document document)
document
- is passed from the RecipeRepository after a successful connection.