10 defined(
'JPATH_PLATFORM') or die;
32 public function create($files, $public =
false, $description = null)
40 'files' => $this->buildFileData((array) $files),
41 'public' => (
bool) $public,
42 'description' => $description
47 $response = $this->client->post($this->fetchUrl($path), $data);
50 if ($response->code != 201)
53 $error = json_decode($response->body);
54 throw new DomainException($error->message, $response->code);
57 return json_decode($response->body);
70 public function createComment($gistId, $body)
73 $path =
'/gists/' . (int) $gistId .
'/comments';
83 $response = $this->client->post($this->fetchUrl($path), $data);
86 if ($response->code != 201)
89 $error = json_decode($response->body);
90 throw new DomainException($error->message, $response->code);
93 return json_decode($response->body);
105 public function delete($gistId)
108 $path =
'/gists/' . (int) $gistId;
111 $response = $this->client->delete($this->fetchUrl($path));
114 if ($response->code != 204)
117 $error = json_decode($response->body);
118 throw new DomainException($error->message, $response->code);
131 public function deleteComment($commentId)
134 $path =
'/gists/comments/' . (int) $commentId;
137 $response = $this->client->delete($this->fetchUrl($path));
140 if ($response->code != 204)
143 $error = json_decode($response->body);
144 throw new DomainException($error->message, $response->code);
160 public function edit($gistId, $files = null, $public = null, $description = null)
163 $path =
'/gists/' . (int) $gistId;
166 $data =
new stdClass;
169 if (isset($description))
171 $data->description = $description;
177 $data->public = $public;
183 $data->files = $this->buildFileData((array) $files);
187 $data = json_encode($data);
190 $response = $this->client->patch($this->fetchUrl($path), $data);
193 if ($response->code != 200)
196 $error = json_decode($response->body);
197 throw new DomainException($error->message, $response->code);
200 return json_decode($response->body);
213 public function editComment($commentId, $body)
216 $path =
'/gists/comments/' . (int) $commentId;
226 $response = $this->client->patch($this->fetchUrl($path), $data);
229 if ($response->code != 200)
232 $error = json_decode($response->body);
233 throw new DomainException($error->message, $response->code);
236 return json_decode($response->body);
248 public function fork($gistId)
251 $path =
'/gists/' . (int) $gistId .
'/fork';
255 $response = $this->client->post($this->fetchUrl($path),
'');
258 if ($response->code != 201)
261 $error = json_decode($response->body);
262 throw new DomainException($error->message, $response->code);
265 return json_decode($response->body);
277 public function get($gistId)
280 $path =
'/gists/' . (int) $gistId;
283 $response = $this->client->get($this->fetchUrl($path));
286 if ($response->code != 200)
289 $error = json_decode($response->body);
290 throw new DomainException($error->message, $response->code);
293 return json_decode($response->body);
305 public function getComment($commentId)
308 $path =
'/gists/comments/' . (int) $commentId;
311 $response = $this->client->get($this->fetchUrl($path));
314 if ($response->code != 200)
317 $error = json_decode($response->body);
318 throw new DomainException($error->message, $response->code);
321 return json_decode($response->body);
335 public function getComments($gistId, $page = 0, $limit = 0)
338 $path =
'/gists/' . (int) $gistId .
'/comments';
341 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
344 if ($response->code != 200)
347 $error = json_decode($response->body);
348 throw new DomainException($error->message, $response->code);
351 return json_decode($response->body);
365 public function getList($page = 0, $limit = 0)
371 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
374 if ($response->code != 200)
377 $error = json_decode($response->body);
378 throw new DomainException($error->message, $response->code);
381 return json_decode($response->body);
395 public function getListByUser($user, $page = 0, $limit = 0)
398 $path =
'/users/' . $user .
'/gists';
401 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
404 if ($response->code != 200)
407 $error = json_decode($response->body);
408 throw new DomainException($error->message, $response->code);
411 return json_decode($response->body);
424 public function getListPublic($page = 0, $limit = 0)
427 $path =
'/gists/public';
430 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
433 if ($response->code != 200)
436 $error = json_decode($response->body);
437 throw new DomainException($error->message, $response->code);
440 return json_decode($response->body);
453 public function getListStarred($page = 0, $limit = 0)
456 $path =
'/gists/starred';
459 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
462 if ($response->code != 200)
465 $error = json_decode($response->body);
466 throw new DomainException($error->message, $response->code);
469 return json_decode($response->body);
481 public function isStarred($gistId)
484 $path =
'/gists/' . (int) $gistId .
'/star';
487 $response = $this->client->get($this->fetchUrl($path));
490 if ($response->code == 204)
494 elseif ($response->code == 404)
501 $error = json_decode($response->body);
502 throw new DomainException($error->message, $response->code);
515 public function star($gistId)
518 $path =
'/gists/' . (int) $gistId .
'/star';
521 $response = $this->client->put($this->fetchUrl($path),
'');
524 if ($response->code != 204)
527 $error = json_decode($response->body);
528 throw new DomainException($error->message, $response->code);
541 public function unstar($gistId)
544 $path =
'/gists/' . (int) $gistId .
'/star';
547 $response = $this->client->delete($this->fetchUrl($path));
550 if ($response->code != 204)
553 $error = json_decode($response->body);
554 throw new DomainException($error->message, $response->code);
568 protected function buildFileData(array $files)
572 foreach ($files as $key => $file)
575 if (!is_numeric($key))
577 $data[$key] = array(
'content' => $file);
581 elseif (!file_exists($file))
583 throw new InvalidArgumentException(
'The file ' . $file .
' does not exist.');
587 $data[basename($file)] = array(
'content' => file_get_contents($file));