Skip to content

Commit

Permalink
Merge pull request #1 from jasonkellydk/create-keyword
Browse files Browse the repository at this point in the history
BC. Feat. Added support for creating keywords
  • Loading branch information
jasonkellydk authored May 8, 2017
2 parents d31f189 + b53a16f commit 6161a21
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 61 deletions.
2 changes: 2 additions & 0 deletions src/SpotOnLive/AccuRanker/Options/ApiOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ class ApiOptions extends Options implements OptionsInterface
/** @var array */
protected $defaults = [
'api_url' => 'https://app.accuranker.com/api/v3/',

'curl_timeout' => 60,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Application;
use SpotOnLive\AccuRanker\Services\AccuRankerService;
use SpotOnLive\AccuRanker\Services\CurlService;

class AccuRankerServiceProvider extends ServiceProvider
{
Expand All @@ -30,14 +31,14 @@ public function boot()
*/
public function register()
{
$this->app->bind('SpotOnLive\AccuRanker\Services\AccuRankerService', function (Application $app) {
if (!$config = config('accuranker')) {
$config = [];
}
$this->app->bind(AccuRankerService::class, function (Application $app) {
$curlService = $app->make(CurlService::class);

$curlService = $app->make('SpotOnLive\AccuRanker\Services\CurlService');
return new AccuRankerService($this->getConfig(), $curlService);
});

return new AccuRankerService($config, $curlService);
$this->app->bind(CurlService::class, function (Application $app) {
return new CurlService($this->getConfig());
});

$this->mergeConfig();
Expand All @@ -53,4 +54,16 @@ private function mergeConfig()
'accuranker'
);
}

/**
* @return array
*/
private function getConfig()
{
if (!$config = config('accuranker')) {
return [];
}

return $config;
}
}
141 changes: 92 additions & 49 deletions src/SpotOnLive/AccuRanker/Services/AccuRankerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,12 @@ public function __construct(array $config, CurlServiceInterface $curlService)
*/
public function listKeywordsForDomain($domainId)
{
$results = $this->api('domains/' . $domainId . '/keywords/');
$results = $this->get('domains/' . $domainId . '/keywords/');
// init keywords with empty array
$keywords = [];

foreach ($results as $result) {
// Keyword
$keyword = new Keyword();
$keyword->setId($result['id']);
$keyword->setDomain($result['domain']);
$keyword->setKeyword($result['keyword']);
$keyword->setLocation($result['location']);
$keyword->setSearchEngine($result['search_engine']);
$keyword->setIgnoreLocalResults($result['ignore_local_results']);
$keyword->setCreatedAt(\DateTime::createFromFormat('Y-m-d His', $result['created_at'] . ' 00000'));
$keyword->setSearchLocale($result['search_locale']);
$keyword->setStarred($result['starred']);
$keyword->setTags($result['tags']);
$keyword->setSearchVolume($result['search_volume']);

// Rank
$rank = new Rank();
$rank->setSearchDate(new \DateTime($result['rank']['search_date']));
$rank->setRank($result['rank']['rank']);
$rank->setUrl($result['rank']['url']);
$rank->setEstTraffic($result['rank']['est_traffic']);
$rank->setExtraRanks($result['rank']['extra_ranks']);

$keyword->setRank($rank);

$keywords[] = $keyword;
$keywords[] = $this->convertResponseToKeyword($result);
}

return $keywords;
Expand All @@ -78,54 +55,120 @@ public function listKeywordsForDomain($domainId)
*/
public function listKeywordHistory($keywordId)
{
$result = $this->api('keywords/' . $keywordId . '/');
$response = $this->get('keywords/' . $keywordId . '/');

return $this->convertResponseToKeyword($response);
}

/**
* Convert response to keyword model
*
* @param array $response
* @return Keyword
*/
private function convertResponseToKeyword(array $response)
{
$keyword = new Keyword();

$keyword->setId($result['id']);
$keyword->setDomain($result['domain']);
$keyword->setKeyword($result['keyword']);
$keyword->setLocation($result['location']);
$keyword->setSearchEngine($result['search_engine']);
$keyword->setIgnoreLocalResults($result['ignore_local_results']);
$keyword->setCreatedAt(\DateTime::createFromFormat('Y-m-d His', $result['created_at'] . ' 00000'));
$keyword->setSearchLocale($result['search_locale']);
$keyword->setStarred($result['starred']);
$keyword->setTags($result['tags']);
$keyword->setSearchVolume($result['search_volume']);

foreach ($result['history'] as $historyResult) {
$keyword->setId($response['id']);
$keyword->setDomain($response['domain']);
$keyword->setKeyword($response['keyword']);
$keyword->setLocation($response['location']);
$keyword->setSearchEngine($response['search_engine']);
$keyword->setIgnoreLocalResults($response['ignore_local_results']);
$keyword->setCreatedAt(DateTime::createFromFormat('Y-m-d His', $response['created_at'] . ' 00000'));
$keyword->setSearchLocale($response['search_locale']);
$keyword->setStarred($response['starred']);
$keyword->setTags($response['tags']);
$keyword->setSearchVolume($response['search_volume']);

if (isset($response['history'])) {
foreach ($response['history'] as $historyResult) {
$rank = new Rank();
$rank->setSearchDate(new DateTime($historyResult['search_date']));
$rank->setRank($historyResult['rank']);
$rank->setUrl($historyResult['url']);
$rank->setEstTraffic($historyResult['est_traffic']);
$rank->setExtraRanks($historyResult['extra_ranks']);

$keyword->addHistory($rank);
}
}

if (isset($response['rank'])) {
// Rank
$rank = new Rank();
$rank->setSearchDate(new \DateTime($historyResult['search_date']));
$rank->setRank($historyResult['rank']);
$rank->setUrl($historyResult['url']);
$rank->setEstTraffic($historyResult['est_traffic']);
$rank->setExtraRanks($historyResult['extra_ranks']);
$rank->setSearchDate(new DateTime($response['rank']['search_date']));
$rank->setRank($response['rank']['rank']);
$rank->setUrl($response['rank']['url']);
$rank->setEstTraffic($response['rank']['est_traffic']);
$rank->setExtraRanks($response['rank']['extra_ranks']);

$keyword->addHistory($rank);
$keyword->setRank($rank);
}

return $keyword;
}

/**
* Call API
* @param integer $domainId
* @param string $keyword
* @param string $searchType
* @param string $searchEngine
* @param array $optional
* @return Keyword
*/
public function createKeywordForDomain($domainId, $keyword, $searchType, $searchEngine, $optional = [])
{
$body = array_merge([
'keyword' => $keyword,
'search_type' => $searchType,
'search_engine' => $searchEngine,
], $optional);

$response = $this->post('domains/' . $domainId . '/keywords/', $body);

return $this->convertResponseToKeyword($response);
}

/**
* Call the CURL get service
*
* @param string $url
* @return array
* @throws InvalidAPICallException
* @throws InvalidCredentialsException
*/
public function api($url)
public function get($url)
{
$result = $this->curlService->curl(
$result = $this->curlService->get(
$this->getUrl() . $url,
$this->getToken()
);

return $this->parse($result);
}

/**
* Call the CURL post service
*
* @param string $url
* @param array $body
* @return array
* @throws InvalidAPICallException
* @throws InvalidCredentialsException
*/
public function post($url, $body)
{
$result = $this->curlService->post(
$this->getUrl() . $url,
$this->getToken(),
$body
);

return $this->parse($result);
}

/**
* Parse result
*
Expand Down
47 changes: 44 additions & 3 deletions src/SpotOnLive/AccuRanker/Services/CurlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@

namespace SpotOnLive\AccuRanker\Services;

use SpotOnLive\AccuRanker\Options\ApiOptions;


class CurlService implements CurlServiceInterface
{
/** @var ApiOptions */
protected $config;

/**
* @param array $config
*/
public function __construct(array $config)
{
$this->config = new ApiOptions($config);
}


/**
* Curl
*
* @param string $url
* @param string $token
* @param array $params
* @return string
*/
public function curl($url, $token, $params = [])
public function get($url, $token, $params = [])
{
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_TIMEOUT, $this->config->get('curl_timeout'));
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization:' . $token]);

$result = curl_exec($curl);
Expand All @@ -26,4 +40,31 @@ public function curl($url, $token, $params = [])

return $result;
}


/**
* @param string $url
* @param string $token
* @param array $body
* @return string
*/
public function post($url, $token, $body = [])
{
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$body);
curl_setopt($curl,CURLOPT_TIMEOUT, $this->config->get('curl_timeout'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization:' . $token]);

$result = curl_exec($curl);

curl_close($curl);

return $result;
}


}
12 changes: 9 additions & 3 deletions src/SpotOnLive/AccuRanker/Services/CurlServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
interface CurlServiceInterface
{
/**
* Curl
*
* @param string $url
* @param string $token
* @return string
*/
public function curl($url, $token);
public function get($url, $token);

/**
* @param $url
* @param $token
* @param $body
* @return string
*/
public function post($url, $token, $body);
}
3 changes: 3 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
return [
// API Url
// 'api_url' => 'https://app.accuranker.com/api/v3/',

// Curl timeout
'curl_timeout' => 60,
];

0 comments on commit 6161a21

Please sign in to comment.