-
Notifications
You must be signed in to change notification settings - Fork 1
/
lab4.krl
92 lines (86 loc) · 2.66 KB
/
lab4.krl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
ruleset rotten_tomatoes {
meta {
name "lab 3"
author "Jacob Wright"
logging off
use module a169x701 alias CloudRain
use module a41x186 alias SquareTag
}
global {
datasource movie_url <- "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=8j3zn2knpjn27xsrm6g6g3mz";
}
rule watch_rule {
select when web cloudAppSelected
pre {
watch_link = <<
<div id=rotten>
<b>SEARCH</b>
<form id='watched' action="javascript:void(0)">
Movie Title: <input type="text" name="title"><br>
<input type="submit" value="Submit">
</form>
</div>
>>;
}
{
SquareTag:inject_styling();
CloudRain:createLoadPanel("Rotten Tomato", {}, watch_link);
//append('body', watch_link);
watch("#watched", "submit");
}
}
rule div_appender {
select when web cloudAppSelected
pre {
t = ent:title || "";
html_div = << <div id="my_div">#{t}</div> >>;
}
every {
prepend('#rotten', html_div);
}
}
rule clicked_rule {
select when web submit "#watched"
pre {
new_title = event:attr("title") || "Empty Query";
movie_data = datasource:movie_url("&q=" + new_title);
total = movie_data.pick("$.total").decode();
thumbnail = movie_data.pick("$.movies[0].posters.thumbnail");
title = movie_data.pick("$.movies[0].title") || "n/a";
year = movie_data.pick("$.movies[0].year") || "n/a";
synopsis = movie_data.pick("$.movies[0].synopsis") || "n/a";
critic_rating = movie_data.pick("$.movies[0].ratings.critics_score");
audience_rating = movie_data.pick("$.movies[0].ratings.audience_score");
consensus = movie_data.pick("$.movies[0].critics_consensus").encode() || "n/a";
my_html = <<
<img src="#{thumbnail}" alt="movie pic">
<p> <b>Title:</b> #{title} </p>
<p> <b>Year:</b> #{year} </p>
<p> <b>Synopsis:</b> #{synopsis} </p>
<p> <b>Critic Rating:</b> #{critic_rating} </p>
<p> <b>Audience Rating:</b> #{audience_rating} </p>
<p> <b>Critic Consensus: </b> #{consensus} </p>
<hr>
>>;
}
if total > 0 then
replace_inner("#my_div", my_html);
fired {
set ent:title new_title;
} else {
raise explicit event 'title_not_found' with title = new_title;
}
}
rule title_not_found {
select when explicit title_not_found
pre {
title = event:param("title");
my_html = <<
<p> <b>Error in finding #{title}</b> </p>
>>;
}
{
replace_inner("#my_div", my_html);
}
}
}