You are here

public function DisqusResource::__call in Drupal Most Popular 7

File

modules/mostpopular_disqus/disqusapi/disqusapi.php, line 73

Class

DisqusResource

Code

public function __call($name, $args) {
  $resource = $this->interface->{$name};
  if (!$resource) {
    throw new DisqusInterfaceNotDefined();
  }
  $kwargs = (array) $args[0];
  foreach ((array) $resource->required as $k) {
    if (empty($kwargs[$k])) {
      throw new Exception('Missing required argument: ' . $k);
    }
  }
  $api = $this->api;
  if (empty($kwargs['api_secret'])) {
    $kwargs['api_secret'] = $api->key;
  }

  // emulate a named pop
  $version = !empty($kwargs['version']) ? $kwargs['version'] : $api->version;
  $format = !empty($kwargs['format']) ? $kwargs['format'] : $api->format;
  unset($kwargs['version'], $kwargs['format']);
  $url = 'https://' . DISQUS_API_HOST;
  $path = '/api/' . $version . '/' . implode('/', $this->tree) . '/' . $name . '.' . $format;
  if (!empty($kwargs)) {
    if ($resource->method == 'POST') {
      $post_data = $kwargs;
    }
    else {
      $post_data = false;
      $path .= '?' . dsq_get_query_string($kwargs);
    }
  }
  $response = dsq_urlopen($url . $path, $post_data);
  $data = call_user_func($api->formats[$format], $response['data']);
  if ($response['code'] != 200) {
    throw new DisqusAPIError($data->code, $data->response);
  }
  return $data->response;
}