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 ...
This commit is contained in:
2013-09-20 10:27:00 +02:00
parent 1da678f726
commit 48f5cb78d5
22 changed files with 201 additions and 161 deletions
+15 -11
View File
@@ -18,18 +18,21 @@ feature {NONE}
make_from_json (json: JSON_OBJECT)
do
if attached {JSON_STRING} json.item (create {JSON_STRING}.make_json ("title")) as a_title then
title := a_title.unescaped_string_32
if attached {JSON_STRING} json.item ("title") as l_title then
title := l_title.unescaped_string_32
end
if attached {JSON_STRING} json.item (create {JSON_STRING}.make_json ("content")) as a_content then
content := a_content.unescaped_string_32
if attached {JSON_STRING} json.item ("content") as l_content then
content := l_content.unescaped_string_32
end
if attached {JSON_OBJECT} json.item (create {JSON_STRING}.make_json ("image")) as img and then attached {JSON_STRING} img.item (create {JSON_STRING}.make_json ("url")) as a_image then
image := a_image.item
if
attached {JSON_OBJECT} json.item ("image") as img and then
attached {JSON_STRING} img.item ("url") as l_image
then
image := l_image.item
end
end
feature
feature -- Access
title: detachable STRING
@@ -37,13 +40,14 @@ feature
image: detachable STRING
get (field: STRING): detachable ANY
item (a_field: READABLE_STRING_GENERAL): detachable ANY
-- <Precursor>
do
if field.is_equal ("title") then
if a_field.same_string ("title") then
Result := title
elseif field.is_equal ("content") then
elseif a_field.same_string ("content") then
Result := content
elseif field.is_equal ("image") then
elseif a_field.same_string ("image") then
Result := image
end
end
@@ -47,26 +47,28 @@ feature
data: ITERABLE [GOOGLE_NEWS]
local
list: LINKED_LIST [GOOGLE_NEWS]
l_result: INTEGER
l_curl_string: CURL_STRING
l_json: detachable READABLE_STRING_8
json_parser: JSON_PARSER
query_str: STRING
cl: LIBCURL_HTTP_CLIENT
sess: HTTP_CLIENT_SESSION
do
curl_handle := curl_easy.init
create list.make
row_count := 0
if curl_handle /= default_pointer then
create l_curl_string.make_empty
query_str := query.out
query_str.replace_substring_all (" ", "+")
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_url, "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=" + query_str + "&rsz=" + page_size.out + "&start=" + (page_size * (page - 1)).out)
curl_easy.set_write_function (curl_handle)
curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_writedata, l_curl_string.object_id)
l_result := curl_easy.perform (curl_handle)
-- Always cleanup
curl_easy.cleanup (curl_handle)
create json_parser.make_parser (l_curl_string.out)
query_str := query.out
query_str.replace_substring_all (" ", "+")
create cl.make
sess := cl.new_session ("https://ajax.googleapis.com/ajax/services/search")
sess.set_is_insecure (True)
if sess.is_available then
if attached {HTTP_CLIENT_RESPONSE} sess.get ("/news?v=1.0&q=" + query_str + "&rsz=" + page_size.out + "&start=" + (page_size * (page - 1)).out, Void) as l_response then
if not l_response.error_occurred then
l_json := l_response.body
end
end
end
if l_json /= Void and then not l_json.is_empty then
create json_parser.make_parser (l_json)
if attached {JSON_OBJECT} json_parser.parse_json as sp then
if attached {JSON_OBJECT} sp.item (create {JSON_STRING}.make_json ("responseData")) as responsedata and then attached {JSON_ARRAY} responsedata.item (create {JSON_STRING}.make_json ("results")) as results then
if attached {JSON_OBJECT} responsedata.item (create {JSON_STRING}.make_json ("cursor")) as cursor and then attached {JSON_STRING} cursor.item (create {JSON_STRING}.make_json ("estimatedResultCount")) as count then
@@ -94,14 +96,4 @@ feature
query: STRING
feature {NONE} -- Implementation
curl_easy: CURL_EASY_EXTERNALS
once
create Result
end
curl_handle: POINTER;
-- cURL handle
end