31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'ext/quacker/quacker.c', line 31
VALUE method_quack_response(VALUE self) {
CURL *curl_handle;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://api.openweathermap.org/data/2.5/weather?id=1835848&APPID=ddaa30ea4f531944617c179cc7c1b270");
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_perform(curl_handle);
// the json segment indicates rainy weather
// `strstr()` returns index if substring is present
return rb_sprintf(strstr(chunk.memory, "\"weather\":[{\"id\":5") == NULL ? "..." : "quack");
}
|