Skip to content

Commit

Permalink
Merge pull request #10 from eleclerc/webhook
Browse files Browse the repository at this point in the history
add support for Webhook maintenance
  • Loading branch information
gorkalaucirica committed Oct 30, 2014
2 parents d6be3c2 + bce5aa2 commit 2df3743
Show file tree
Hide file tree
Showing 5 changed files with 389 additions and 3 deletions.
52 changes: 52 additions & 0 deletions API/RoomAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use GorkaLaucirica\HipchatAPIv2Client\Client;
use GorkaLaucirica\HipchatAPIv2Client\Model\Message;
use GorkaLaucirica\HipchatAPIv2Client\Model\Room;
use GorkaLaucirica\HipchatAPIv2Client\Model\Webhook;


class RoomAPI
{
Expand Down Expand Up @@ -186,4 +188,54 @@ public function setTopic($roomId, $topic)
{
$this->client->put(sprintf('/v2/room/%s/topic', $roomId), array('topic' => $topic));
}

/**
* Creates a new webhook
* More info: https://www.hipchat.com/docs/apiv2/method/create_webhook
*
* @param string $roomId The id or name of the room
* @param Webhook $webhook The webhook to create
*
* @return int Just created webhook id
*/
public function createWebhook($roomId, Webhook $webhook)
{
$response = $this->client->post(sprintf('/v2/room/%s/webhook', $roomId), $webhook->toJson());

return $response['id'];
}

/**
* Deletes a new webhook
* More info: https://www.hipchat.com/docs/apiv2/method/delete_webhook
*
* @param string $roomId The id or name of the room
* @param string $webhookId The id of the webhook to delete
*
* @return void
*/
public function deleteWebhook($roomId, $webhookId)
{
$this->client->delete(sprintf('/v2/room/%s/webhook/%s', $roomId, $webhookId));
}

/**
* Gets all webhooks for this room
* More info: https://www.hipchat.com/docs/apiv2/method/get_all_webhooks
*
* @param string $roomId The id or name of the room
*
* @TODO should return a Collection
* @return array Array of Webhook
*/
public function getAllWebhooks($roomId)
{
$webhooks = array();
$response = $this->client->get(sprintf('/v2/room/%s/webhook', $roomId));
foreach ($response['items'] as $item) {
$webhooks[] = new Webhook($item);
}

return $webhooks;
}
}
212 changes: 212 additions & 0 deletions Model/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php

namespace GorkaLaucirica\HipchatAPIv2Client\Model;

class Webhook
{

/**
* The unique identifier for the created entity
* @var string|null
*/
protected $id = null;

/**
* The URL to send the webhook POST to
* @var string
*/
protected $url;

/**
* The regular expression pattern to match against messages.
* Only applicable for message events.
* @var string
*/
protected $pattern;

/**
* The event to listen for
* Valid values:
* room_message, room_notification, room_exit, room_enter, room_topic_change.
* @var string
*/
protected $event;

/**
* The label for this webhook
* @var string
*/
protected $name;

/**
* URLs to retrieve webhook information
* @var array
*/
protected $links;

/**
* Webhook constructor
*/
public function __construct($json = null)
{
if ($json) {
$this->parseJson($json);
} else {
$this->url = '';
$this->pattern = '';
$this->event = 'room_message';
$this->name = '';
$this->links = '';
}
}

public function parseJson($json)
{
$this->id = isset($json['id']) ? $json['id'] : null;
$this->url = $json['url'];
$this->pattern = $json['pattern'];
$this->event = $json['event'];
$this->name = $json['name'];
$this->links = (isset($json['links']) && is_array($json['links'])) ? $json['links'] : array();
}


/**
* Serializes Webhook object
*
* @return array
*/
public function toJson()
{
$json = array();
$json['id'] = $this->id;
$json['url'] = $this->url;
$json['pattern'] = $this->pattern;
$json['event'] = $this->event;
$json['name'] = $this->name;
$json['links'] = $this->links;

return $json;
}

/**
* Gets the unique identifier for the created entity
* @return null|string
*/
public function getId()
{
return $this->id;
}

/**
* Sets the unique identifier for the created entity
* @param null|string $id
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}

/**
* Gets the event to listen for
*
* @return string
*/
public function getEvent() {
return $this->event;
}

/**
* Sets the event to listen for
*
* @param string $event
* @return self
*/
public function setEvent($event) {
$this->event = $event;
return $this;
}

/**
* Gets the URLs to retrieve webhook information
* @return array
*/
public function getLinks()
{
return $this->links;
}

/**
* Sets the URLs to retrieve webhook information
* @param array $links
* @return self
*/
public function setLinks($links)
{
$this->links = $links;
return $this;
}

/**
* Gets the label for this webhook
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Sets the label for this webhook
* @param string $name
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}

/**
* Gets the regular expression pattern to match against messages.
* @return string
*/
public function getPattern()
{
return $this->pattern;
}

/**
* Sets the regular expression pattern to match against messages.
* @param string $pattern
* @return self
*/
public function setPattern($pattern)
{
$this->pattern = $pattern;
return $this;
}

/**
* Gets the URL to send the webhook POST to
* @return string
*/
public function getUrl()
{
return $this->url;
}

/**
* Sets the URL to send the webhook POST to
* @param string $url
* @return self
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}

}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ The following list shows methods available and missing:
- [x] Update room
- [x] Get room
- [x] Delete room
- [ ] Create webhook
- [ ] Get all webhooks
- [x] Create webhook
- [x] Get all webhooks
- [ ] Get room statistics
- [ ] Get all members
- [x] Set topic
- [x] Add member
- [x] Remove member
- [ ] Delete webhook
- [x] Delete webhook
- [ ] Get webhook
- [x] View history
- [x] Invite user
Expand Down
45 changes: 45 additions & 0 deletions spec/GorkaLaucirica/HipchatAPIv2Client/API/RoomAPISpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GorkaLaucirica\HipchatAPIv2Client\Client;
use GorkaLaucirica\HipchatAPIv2Client\Model\Message;
use GorkaLaucirica\HipchatAPIv2Client\Model\Room;
use GorkaLaucirica\HipchatAPIv2Client\Model\Webhook;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand Down Expand Up @@ -109,6 +110,29 @@ function it_sets_topic(Client $client) {
$this->setTopic(665432, 'New topic');
}

function it_creates_webhook(Client $client, Webhook $webhook) {
$request = array(
'url' => 'http://example.com/webhook',
'pattern' => '/phpspec/',
'event' => 'room_message',
'name' => '112233',
);
$webhook->toJson()->shouldBeCalled()->willReturn($request);
$client->post('/v2/room/123456/webhook', $request)->shouldBeCalled();
$this->createWebhook('123456', $webhook);
}

function it_deletes_webhook(Client $client) {
$client->delete('/v2/room/123456/webhook/112233')->shouldBeCalled();
$this->deleteWebhook('123456', '112233');
}

function it_gets_webhooks(Client $client, Webhook $webhook) {
$response = $this->getWebhookArrayResponse();
$client->get('/v2/room/234567/webhook')->shouldBeCalled()->willReturn($response);
$this->getAllWebhooks('234567')->shouldHaveCount(2);
}

protected function getResourceResponse()
{
return array(
Expand Down Expand Up @@ -173,4 +197,25 @@ protected function getMessageHistoryArrayResponse()
);

}

protected function getWebhookArrayResponse()
{
return array(
'items' => array(
array(
'url' => 'http://example.com/dummywebhook',
'pattern' => '/phpspec_delete_webhook/',
'event' => 'room_message',
'name' => '332123',
),
array(
'url' => 'http://example.com/dummywebhook2',
'pattern' => '/phpspec_delete_webhook2/',
'event' => 'room_message',
'name' => '432123',
)
)
);

}
}
Loading

0 comments on commit 2df3743

Please sign in to comment.