From 2d90d5fddd64cb11e3df44c169092e5a4168202f Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Mon, 16 Jan 2017 12:57:07 -0800 Subject: [PATCH] move twitter logic to its own function --- controllers/Parse.php | 83 +++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/controllers/Parse.php b/controllers/Parse.php index 4ed5ab6..b7c2a60 100644 --- a/controllers/Parse.php +++ b/controllers/Parse.php @@ -92,45 +92,7 @@ class Parse { // Check if this is a Twitter URL and if they've provided API credentials, use the API if(preg_match('/https?:\/\/(?:mobile\.twitter\.com|twitter\.com|twtr\.io)\/(?:[a-z0-9_\/!#]+statuse?s?\/([0-9]+)|([a-zA-Z0-9_]+))/i', $url, $match)) { - $fields = ['twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret']; - $creds = []; - foreach($fields as $f) { - if($v=$request->get($f)) - $creds[$f] = $v; - } - $data = false; - if(count($creds) == 4) { - list($data, $parsed) = Formats\Twitter::parse($url, $match[1], $creds); - } elseif(count($creds) > 0) { - // If only some Twitter credentials were present, return an error - return $this->respond($response, 400, [ - 'error' => 'missing_parameters', - 'error_description' => 'All 4 Twitter credentials must be included in the request' - ]); - } else { - // Accept Tweet JSON and parse that if provided - $json = $request->get('json'); - if($json) { - list($data, $parsed) = Formats\Twitter::parse($url, $match[1], null, $json); - } - // Skip parsing from the Twitter API if they didn't include credentials - } - - if($data) { - if($request->get('include_original')) - $data['original'] = $parsed; - $data['url'] = $url; - $data['code'] = 200; - return $this->respond($response, 200, $data); - } else { - return $this->respond($response, 200, [ - 'data' => [ - 'type' => 'unknown' - ], - 'url' => $url, - 'code' => 0 - ]); - } + return $this->parseTwitterURL($request, $response, $url, $match); } // Now fetch the URL and check for any curl errors @@ -323,4 +285,47 @@ class Parse { return $element; } + private function parseTwitterURL(&$request, &$response, $url, $match) { + $fields = ['twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret']; + $creds = []; + foreach($fields as $f) { + if($v=$request->get($f)) + $creds[$f] = $v; + } + $data = false; + if(count($creds) == 4) { + list($data, $parsed) = Formats\Twitter::parse($url, $match[1], $creds); + } elseif(count($creds) > 0) { + // If only some Twitter credentials were present, return an error + return $this->respond($response, 400, [ + 'error' => 'missing_parameters', + 'error_description' => 'All 4 Twitter credentials must be included in the request' + ]); + } else { + // Accept Tweet JSON and parse that if provided + $json = $request->get('json'); + if($json) { + list($data, $parsed) = Formats\Twitter::parse($url, $match[1], null, $json); + } + // Skip parsing from the Twitter API if they didn't include credentials + } + + if($data) { + if($request->get('include_original')) + $data['original'] = $parsed; + $data['url'] = $url; + $data['code'] = 200; + return $this->respond($response, 200, $data); + } else { + return $this->respond($response, 200, [ + 'data' => [ + 'type' => 'unknown' + ], + 'url' => $url, + 'code' => 0 + ]); + } + + } + }