Files
EWF/examples/widgetapp/autocompletion/google_autocompletion.e
Jocelyn Fiat 48f5cb78d5 Minor changes
- using http_client library instead of libcurl directly
 - using implicit conversion to JSON_STRING to improve code readability
 - use ARRAYED_LIST instead of LINKED_LIST .. for performance.
 - cosmetic .. but still a lot of feature clauses are missing, comments, assertions ...
2013-09-20 10:27:00 +02:00

61 lines
1.3 KiB
Plaintext

note
description: "Summary description for {GOOGLE_AUTOCOMPLETION}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
GOOGLE_AUTOCOMPLETION
inherit
WSF_AUTOCOMPLETION
create
make
feature {NONE} -- Initialization
make
do
template := "{{=value}}";
end
feature -- Implementation
autocompletion (input: STRING): JSON_ARRAY
local
cl: LIBCURL_HTTP_CLIENT
sess: HTTP_CLIENT_SESSION
l_json: detachable READABLE_STRING_8
o: JSON_OBJECT
json_parser: JSON_PARSER
query_str: STRING
do
query_str := input
query_str.replace_substring_all (" ", "+")
create cl.make
sess := cl.new_session ("http://google.com")
if attached sess.get ("/complete/search?client=chrome&q=" + query_str, Void) as resp and then not resp.error_occurred then
l_json := resp.body
end
create Result.make_array
if l_json /= Void and then not l_json.is_empty then
create json_parser.make_parser (l_json)
if attached {JSON_ARRAY} json_parser.parse_json as data and then attached {JSON_ARRAY} data.i_th (2) as list then
across
1 |..| list.count as c
loop
if attached {JSON_STRING} list.i_th (c.item) as row then
create o.make
o.put (create {JSON_STRING}.make_json (row.unescaped_string_32), "value")
Result.add (o)
end
end
end
end
end
end