public function testDecode()
 {
     //Valid JSON
     $result = JSONDecoder::decode('{"Organization": "ThinkUp Documentation Team"}');
     $this->assertNotNull($result);
     $this->assertEqual($result->Organization, "ThinkUp Documentation Team");
     //Valid JSON returned as associative array
     $result = JSONDecoder::decode('{"Organization": "ThinkUp Documentation Team"}', $assoc = true);
     $this->assertNotNull($result);
     $this->assertEqual($result["Organization"], "ThinkUp Documentation Team");
     //Invalid JSON
     $this->expectException("JSONDecoderException");
     $result = JSONDecoder::decode("{'Organization': 'ThinkUp Documentation Team'}");
     $this->assertNull($result);
 }
 /**
  * Set per-endpoint rate limits and next reset time.
  * @return void
  */
 public function initializeEndpointRateLimits()
 {
     $endpoint = $this->endpoints['rate_limits'];
     $args = array();
     $args["resources"] = 'account,statuses,users,followers,lists,friends,favorites,friendships,application';
     list($http_status, $payload) = $this->apiRequest($endpoint, $args);
     $rate_limit_data_array = JSONDecoder::decode($payload, true);
     $rate_limit_data_array = $rate_limit_data_array["resources"];
     $limits = array();
     foreach ($rate_limit_data_array as $resource) {
         foreach ($resource as $key => $values) {
             $limits[$key] = $values;
         }
     }
     foreach ($this->endpoints as $endpoint) {
         $endpoint->setRemaining($limits[$endpoint->getShortPath()]['remaining']);
         $endpoint->setLimit($limits[$endpoint->getShortPath()]['limit']);
         $endpoint->setReset($limits[$endpoint->getShortPath()]['reset']);
     }
 }
    /**
     * Get the subscription status for a ThinkUp.com member via an API call.
     * @param  str $email
     * @return Object
     */
    public function getSubscriptionStatus($email)
    {
        if ($email == '*****@*****.**') {
            $resp = <<<EOD
{
    "email":"*****@*****.**",
    "subscription_status":"Payment failed"
}
EOD;
            return JSONDecoder::decode($resp);
        } elseif ($email == '*****@*****.**') {
            $resp = <<<EOD
{
    "email":"*****@*****.**",
    "subscription_status":"Payment due"
}
EOD;
            return JSONDecoder::decode($resp);
        } elseif ($email == '*****@*****.**') {
            $resp = <<<EOD
{
    "email":"*****@*****.**",
    "subscription_status":"Paid"
}
EOD;
            return JSONDecoder::decode($resp);
        } elseif ($email == '*****@*****.**') {
            $resp = <<<EOD
{
    "email":"*****@*****.**",
    "subscription_status":"Free trial"
}
EOD;
            return JSONDecoder::decode($resp);
        } else {
            return null;
        }
    }
 /**
  * Check to see if two avatars cached on ThinkUp.com are different.
  * Sometimes, Twitter stores the same avatar at different URLs, and the files have different checksums, but
  * the images look very similar. This is an extra check for image differences for hosted ThinkUp.com users.
  * @param  str $image1
  * @param  str $image2
  * @return bool
  */
 public function didAvatarsChange($image1, $image2)
 {
     $config = Config::getInstance();
     if ($config->getValue('image_proxy_enabled') == true) {
         $image_proxy_sig = $config->getValue('image_proxy_sig');
         $api_url = "https://images.thinkup.com/";
         $params = array('image1' => $image1, 'image2' => $image2, 's' => $image_proxy_sig);
         $query = http_build_query($params);
         $api_call = $api_url . '?' . $query;
         //echo $api_call;
         $result = self::getURLContents($api_call);
         //print_r($result);
         try {
             $result_decoded = JSONDecoder::decode($result);
         } catch (JSONDecoderException $e) {
             //Punt - something went very wrong. Err on the side of nothing changed.
             return false;
         }
         //print_r($result_decoded);
         return $result_decoded->show_diff;
     }
     return true;
 }
 /**
  * Convert JSON errors to array.
  * @param str $error_data
  * @return array
  */
 public function parseJSONError($error_data)
 {
     $json = JSONDecoder::decode($error_data);
     $parsed_payload = array();
     if (isset($json->errors)) {
         $parsed_payload['error'] = $json->errors[0]->message;
     }
     return $parsed_payload;
 }
    private static function decodeFileContents($file_path, $decode_json = true)
    {
        $debug = getenv('TEST_DEBUG') !== false ? true : false;
        if ($debug) {
            echo "READING LOCAL TEST DATA FILE: " . $file_path . '

';
        }
        if (file_exists($file_path)) {
            $contents = file_get_contents($file_path);
            if ($decode_json) {
                try {
                    return JSONDecoder::decode($contents);
                } catch (JSONDecoderException $e) {
                    return $contents;
                }
            } else {
                return $contents;
            }
        } else {
            if ($debug) {
                echo $file_path . " does not exist.\n\n";
            }
            return '';
        }
    }
 /**
  * Make a Graph API request with the absolute URL. This URL needs to include the https://graph.facebook.com/ at
  * the start and all the query string parameters EXCEPT the acces token.
  *
  * This is for use in paging, when the API payload specifies the full URL for the next page.
  *
  * @param str $url
  * @param str $access_token
  * @return array Decoded JSON response
  */
 public static function apiRequestFullURL($url, $access_token = null)
 {
     $params = array();
     if (isset($access_token)) {
         //Add access_token
         $params['access_token'] = $access_token;
         $access_token_str = http_build_query($params);
         if (strpos($url, '?') === false) {
             $url = $url . '?' . $access_token_str;
         } else {
             $url = $url . '&' . $access_token_str;
         }
     }
     //DEBUG
     // if (php_sapi_name() == "cli") {//Crawler being run at the command line
     //     Logger::getInstance()->logInfo("Graph API call: ".$url, __METHOD__.','.__LINE__);
     // }
     $result = Utils::getURLContents($url);
     try {
         // if (php_sapi_name() == "cli") {//Crawler being run at the command line
         //     Logger::getInstance()->logInfo("Graph API call payload: ".$result, __METHOD__.','.__LINE__);
         // }
         return JSONDecoder::decode($result);
     } catch (JSONDecoderException $e) {
         return $result;
     }
 }
 public function testAPIDisabled()
 {
     $_GET['un'] = '*****@*****.**';
     $_GET['as'] = 'c9089f3c9adaf0186f6ffb1ee8d6501c';
     // test default option
     $controller = new InsightAPIController(true);
     $output = $controller->go();
     $this->debug($output);
     $output = json_decode($output);
     $this->assertFalse(isset($output->error));
     // test option true
     $option_dao = DAOFactory::getDAO('OptionDAO');
     $option_dao->insertOption(OptionDAO::APP_OPTIONS, 'is_api_disabled', 'true');
     $controller = new InsightAPIController(true);
     $output = $controller->go();
     $this->debug($output);
     $output = json_decode($output);
     $this->assertEqual($output->error->type, 'APIDisabledException');
     // test option false
     $option_dao->updateOptionByName(OptionDAO::APP_OPTIONS, 'is_api_disabled', 'false');
     $controller = new InsightAPIController(true);
     $output = $controller->go();
     $this->debug($output);
     $output = JSONDecoder::decode($output);
     $this->assertFalse(isset($output->error));
     $this->assertEqual(count($output[0]->related_data->posts), 3);
 }