Skip to content

Commit 42d8fc5

Browse files
feat(hackathon): Add Yahoo API Functionality
1 parent 85a2a3a commit 42d8fc5

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
use GuzzleHttp\Client;
8+
use App\Http\Requests;
9+
use App\Http\Controllers\Controller;
10+
11+
class YahooController extends Controller
12+
{
13+
/**
14+
* Instance of Guzzle Client
15+
* @var object
16+
*/
17+
protected $client;
18+
19+
/**
20+
* BaseUrl
21+
* @var string
22+
*/
23+
protected $baseUrl;
24+
25+
/**
26+
* Initialize the Controller with necessary arguments
27+
*/
28+
public function __construct()
29+
{
30+
$this->baseUrl = 'https://query.yahooapis.com/v1/public/yql';
31+
$this->client = new Client(['base_uri' => $this->baseUrl]);
32+
33+
$query = "SELECT * FROM weather.forecast WHERE (location = 10007)";
34+
35+
$relativeUrl = '?q=' . $query . '&format=json';
36+
$this->setGetResponse($relativeUrl);
37+
}
38+
39+
/**
40+
* Get the response from Yahoo API
41+
* @param string $relativeUrl
42+
*/
43+
private function setGetResponse($relativeUrl)
44+
{
45+
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
46+
}
47+
48+
/**
49+
* Get the whole response from a get operation
50+
* @return array
51+
*/
52+
private function getResponse()
53+
{
54+
return json_decode($this->response->getBody(), true);
55+
}
56+
57+
/**
58+
* Get the data response from a get operation
59+
* @return array
60+
*/
61+
private function getData()
62+
{
63+
return $this->getResponse()['query']['results']['channel'];
64+
}
65+
66+
67+
/**
68+
* Return all data to the Yahoo API dashboard
69+
* @return mixed
70+
*/
71+
public function getPage()
72+
{
73+
$data = $this->getData();
74+
75+
return view('api.yahoo')->withData($data);
76+
}
77+
78+
}

app/Http/routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
9292
'middleware' => ['auth']
9393
]);
9494

95+
Route::get('/api/yahoo', [
96+
'uses' => 'YahooController@getPage',
97+
'as' => 'api.yahoo',
98+
'middleware' => ['auth']
99+
]);
100+
95101
Route::post('/tweet/new', [
96102
'uses' => 'TwitterController@sendTweet',
97103
'as' => 'tweet.new',

resources/views/api/yahoo.blade.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@extends('layouts.master')
2+
3+
@section('content')
4+
<div class="main-container">
5+
@include('layouts.partials.alerts')
6+
7+
<div class="page-header">
8+
<h2><i style="color: #7b0099" class="fa fa-yahoo"></i>Yahoo API</h2>
9+
</div>
10+
11+
<div class="btn-group btn-group-justified">
12+
<a href="https://developer.yahoo.com/yql/" target="_blank" class="btn btn-primary">
13+
<i class="fa fa-check-square-o"></i> YQL Getting Started
14+
</a>
15+
<a href="https://developer.yahoo.com/everything.html" target="_blank" class="btn btn-primary">
16+
<i class="fa fa-code"></i> Yahoo APIs
17+
</a>
18+
</div>
19+
20+
<br>
21+
22+
<p class="lead">Weather for ZIP Code:<strong> 10007</strong></p>
23+
24+
<div class="alert alert-info">
25+
<p>It is currently<strong> {{ $data['item']['condition']['temp'] }}</strong> degrees in<strong> New York, NY</strong>.</p>
26+
</div>
27+
28+
<h3>YQL Query</h3>
29+
30+
<pre>SELECT * FROM weather.forecast WHERE (location = 10007)</pre>
31+
32+
</div>
33+
@stop

0 commit comments

Comments
 (0)