Permalink
Browse files

Merge pull request #8 from EvanK/implement-optional-url-params

Implemented optional url params
  • Loading branch information...
thousandsofthem committed Oct 18, 2012
2 parents 1192219 + ff44a1b commit 34449182f2f5d59ac51a6e5d2da7f0170a0e6acd
Showing with 27 additions and 8 deletions.
  1. +27 −8 IronMQ.class.php
View
@@ -131,6 +131,9 @@ class IronMQ extends IronCore{
'api_version' => '1',
);
+ const LIST_QUEUES_PER_PAGE = 30;
+ const GET_MESSAGE_TIMEOUT = 60;
+
/**
* @param string|array $config_file_or_options
* Array of options or name of config file.
@@ -165,11 +168,22 @@ public function setProjectId($project_id) {
}
}
- public function getQueues($page = 0){
+ /**
+ * Get list of message queues
+ *
+ * @param int $page
+ * Zero-indexed page to view
+ * @param int $per_page
+ * Number of queues per page
+ */
+ public function getQueues($page = 0, $per_page = self::LIST_QUEUES_PER_PAGE) {
$url = "projects/{$this->project_id}/queues";
$params = array();
- if($page > 0) {
- $params['page'] = $page;
+ if($page !== 0) {
+ $params['page'] = (int) $page;
+ }
+ if($per_page !== self::LIST_QUEUES_PER_PAGE) {
+ $params['per_page'] = (int) $per_page;
}
$this->setJsonHeaders();
return self::json_decode($this->apiCall(self::GET, $url, $params));
@@ -261,14 +275,18 @@ public function postMessages($queue_name, $messages) {
*
* @param string $queue_name Queue name
* @param int $count
+ * @param int $timeout
* @return array|null array of messages or null
*/
- public function getMessages($queue_name, $count=1) {
+ public function getMessages($queue_name, $count = 1, $timeout = self::GET_MESSAGE_TIMEOUT) {
$queue = rawurlencode($queue_name);
$url = "projects/{$this->project_id}/queues/$queue/messages";
$params = array();
- if($count > 1) {
- $params['n'] = $count;
+ if($count !== 1) {
+ $params['n'] = (int) $count;
+ }
+ if($timeout !== self::GET_MESSAGE_TIMEOUT) {
+ $params['timeout'] = (int) $timeout;
}
$this->setJsonHeaders();
$response = $this->apiCall(self::GET, $url, $params);
@@ -284,10 +302,11 @@ public function getMessages($queue_name, $count=1) {
* Get single message from queue
*
* @param string $queue_name Queue name
+ * @param int $timeout
* @return mixed|null single message or null
*/
- public function getMessage($queue_name) {
- $messages = $this->getMessages($queue_name, 1);
+ public function getMessage($queue_name, $timeout = self::GET_MESSAGE_TIMEOUT) {
+ $messages = $this->getMessages($queue_name, 1, $timeout);
if ($messages){
return $messages[0];
}else{

0 comments on commit 3444918

Please sign in to comment.