Last active
February 17, 2017 01:54
-
-
Save raulmangolin/08ebddfe257e21ad9fca4ce99330a437 to your computer and use it in GitHub Desktop.
Nation Builder Developer Exercises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class NationBuilder | |
{ | |
public $siteSlug = 'sandbox825'; | |
public $baseApiUrl = ''; | |
public $token = ''; | |
public function __construct() | |
{ | |
$this->baseApiUrl = 'https://' . $this->siteSlug . '.nationbuilder.com'; | |
} | |
public function addPeople($data) | |
{ | |
$params = array('person' => $data); | |
$endpoint = '/api/v1/people?access_token=' . $this->token; | |
$result = $this->doCurl($endpoint, json_encode($params), 'POST', 201); | |
if ($result) { | |
return json_decode($result); | |
} else { | |
return false; | |
} | |
} | |
public function doCurl($endpoint, $params, $method, $status) | |
{ | |
$ch = curl_init($this->baseApiUrl . $endpoint); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); | |
curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Accept: application/json', | |
'Content-Type: application/json' | |
)); | |
$response = trim(curl_exec($ch)); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
if ($httpCode == $status) { | |
return $response; | |
} else { | |
return false; | |
} | |
} | |
public function deletePeople($id) | |
{ | |
$params = array(); | |
$endpoint = '/api/v1/people/' . $id . '?access_token=' . $this->token; | |
return $this->doCurl($endpoint, json_encode($params), 'DELETE', 204); | |
} | |
public function updatePeople($id, $data) | |
{ | |
$params = array('person' => $data); | |
$endpoint = '/api/v1/people/' . $id . '?access_token=' . $this->token; | |
return $this->doCurl($endpoint, json_encode($params), 'PUT', 201); | |
} | |
public function createSurvey($data) | |
{ | |
$params = array('survey' => $data); | |
$endpoint = '/api/v1/sites/' . $this->siteSlug . '/pages/surveys?access_token=' . $this->token; | |
$result = $this->doCurl($endpoint, json_encode($params), 'POST', 200, false); | |
if ($result) { | |
return json_decode($result); | |
} else { | |
return false; | |
} | |
} | |
public function answerSurvey($data) | |
{ | |
$params = array('survey_response' => $data); | |
$endpoint = '/api/v1/survey_responses?access_token=' . $this->token; | |
$result = $this->doCurl($endpoint, json_encode($params), 'POST', 200); | |
if ($result) { | |
return json_decode($result); | |
} else { | |
return false; | |
} | |
} | |
public function logContact($id, $data) | |
{ | |
$params = array('contact' => $data); | |
$endpoint = '/api/v1/people/' . $id . '/contacts?access_token=' . $this->token; | |
$result = $this->doCurl($endpoint, json_encode($params), 'POST', 200); | |
if ($result) { | |
return json_decode($result); | |
} else { | |
return false; | |
} | |
} | |
} | |
$randomData = md5(date("Y-m-d H:i:s")); | |
/* | |
* Nation Builde Class Init | |
*/ | |
$nb = new NationBuilder(); | |
/* | |
* Add People | |
*/ | |
$person = $nb->addPeople(array('email' => $randomData . '[email protected]', 'first_name' => 'Pedro', 'last_name' => 'Mangolin')); | |
/* | |
* Create Survey | |
*/ | |
$survey = $nb->createSurvey(array( | |
'slug' => $randomData . '_survey', | |
'name' => 'Nation Builder Developer', | |
'tags' => array( | |
'funny' | |
), | |
'status' => 'published', | |
'questions' => array( | |
array( | |
'prompt' => 'What issue is more important to you?', | |
'external_id' => null, | |
'slug' => $randomData . '_question', | |
'type' => 'multiple', | |
'status' => 'published', | |
'choices' => array( | |
array( | |
'name' => 'Question 1', | |
'feedback' => null | |
), | |
array( | |
'name' => 'Question 2', | |
"feedback" => null | |
), | |
array( | |
'name' => 'Question 3', | |
'feedback' => null | |
), | |
array( | |
'name' => 'Question 4', | |
"feedback" => null | |
) | |
) | |
) | |
) | |
)); | |
/* | |
* Answer Survey | |
*/ | |
if ($survey) { | |
$data = array( | |
'survey_id' => $survey->survey->id, | |
'person_id' => $person->person->id, | |
'question_responses' => array( | |
array( | |
'question_id' => $survey->survey->questions[0]->id, | |
'response' => $survey->survey->questions[0]->choices[0]->id, | |
) | |
) | |
); | |
$answer = $nb->answerSurvey($data); | |
/* | |
* Log contact | |
*/ | |
if ($answer) { | |
$log = $nb->logContact( | |
$person->person->id, | |
array( | |
'author_id' => 1, | |
'sender_id' => 1, | |
"type_id" => 5, | |
'status' => 'answered', | |
"note" => "He answered :)", | |
"method" => "tweet", | |
) | |
); | |
} | |
} | |
/* | |
* Update People | |
*/ | |
if ($person) { | |
$nb->updatePeople($person->person->id, array('email' => $randomData . '[email protected]')); | |
$nb->deletePeople($person->person->id); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment