Skip to content

Commit

Permalink
Merge pull request #5 from nathanmac/master
Browse files Browse the repository at this point in the history
Add function: getRecentHistory()
  • Loading branch information
gorkalaucirica committed Oct 14, 2014
2 parents dc8f88b + c1968e1 commit e0e4dac
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
20 changes: 20 additions & 0 deletions API/RoomAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ public function getRoom($id)
return new Room($response);
}

/**
* Fetch latest chat history for this room.
* More info: https://www.hipchat.com/docs/apiv2/method/view_recent_room_history
*
* @param string $id The id or name of the room
* @param array $params Query string parameter(s), for example: array('max-results' => 30)
*
* @return array
*/
public function getRecentHistory($id, $params = array())
{
$response = $this->client->get(sprintf('/v2/room/%s/history/latest', $id), $params);

$messages = array();
foreach ($response['items'] as $response) {
$messages[] = new Message($response);
}
return $messages;
}

/**
* Creates a room
* More info: https://www.hipchat.com/docs/apiv2/method/create_room
Expand Down
35 changes: 30 additions & 5 deletions Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class Message
{

protected $id = null;

protected $color;

protected $message;
Expand All @@ -12,6 +15,11 @@ class Message

protected $messageFormat;

protected $date = null;

protected $from = null;


const COLOR_YELLOW = 'yellow';
const COLOR_GREEN = 'green';
const COLOR_RED = 'red';
Expand All @@ -25,15 +33,29 @@ class Message
/**
* Message constructor
*/
public function __construct()
public function __construct($json = null)
{
$this->color = self::COLOR_YELLOW;
$this->messageFormat = self::FORMAT_HTML;
$this->message = "";
$this->notify = false;
if ($json) {
$this->parseJson($json);
} else {
$this->color = self::COLOR_YELLOW;
$this->messageFormat = self::FORMAT_HTML;
$this->message = "";
$this->notify = false;
}
}

public function parseJson($json)
{
$this->id = $json['id'];
$this->from = is_array($json['from']) ? $json['from']['name'] : $json['from'];
$this->message = $json['message'];
$this->color = isset($json['color']) ? $json['color'] : null;
$this->messageFormat = isset($json['message_format']) ? $json['message_format'] : 'html';
$this->date = $json['date'];
}


/**
* Serializes Message object
*
Expand All @@ -42,10 +64,13 @@ public function __construct()
public function toJson()
{
$json = array();
$json['id'] = $this->id;
$json['from'] = $this->from;
$json['color'] = $this->color;
$json['message'] = $this->message;
$json['notify'] = $this->notify;
$json['message_format'] = $this->messageFormat;
$json['date'] = $this->date;

return $json;

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ The following list shows methods available and missing:
- [x] Remove member
- [ ] Delete webhook
- [ ] Get webhook
- [ ] View history
- [x] View history
- [x] Invite user

####Users
Expand Down
34 changes: 34 additions & 0 deletions spec/GorkaLaucirica/HipchatAPIv2Client/API/RoomAPISpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ function it_gets_room(Client $client)
$this->getRoom($id)->shouldReturnAnInstanceOf('GorkaLaucirica\HipchatAPIv2Client\Model\Room');
}

function it_gets_room_history(Client $client)
{
$id = 123456;
$response = $this->getMessageHistoryArrayResponse();
$client->get("/v2/room/$id/history/latest", array())->shouldBeCalled()->willReturn($response);

$this->getRecentHistory($id)->shouldHaveCount(2);
}

function it_creates_room(Client $client, Room $room)
{
$request = array(
Expand Down Expand Up @@ -139,4 +148,29 @@ protected function getArrayResponse()
);

}

protected function getMessageHistoryArrayResponse()
{
return array(
"items" => array(
array(
'id' => 1233,
'color' => 'yellow',
'from' => 'Tester',
'message' => 'test1',
'message_format' => 'html',
'date' => '2014-02-10 10:02:10'
),
array(
'id' => 1234,
'color' => 'red',
'from' => 'Tester',
'message' => 'test1',
'message_format' => 'html',
'date' => '2014-02-10 10:02:10'
)
)
);

}
}
15 changes: 14 additions & 1 deletion spec/GorkaLaucirica/HipchatAPIv2Client/Model/MessageSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ function it_is_initializable()
$this->shouldHaveType('GorkaLaucirica\HipchatAPIv2Client\Model\Message');
}

function it_parses_full_json()
{
$json = array(
'id' => '123556',
'from' => 'Tester',
'color' => 'yellow',
'message' => 'Hello World',
'message_format' => 'html',
'date' => '2014-02-10 10:02:10',
);
$this->parseJson($json);
}

function it_encodes_to_json()
{
$this->setColor(Message::COLOR_GRAY);
$this->setMessage('This is a test!!');
$this->toJson()->shouldReturn(
array("color" => "gray", "message" => "This is a test!!", 'notify' => false, 'message_format' => 'html')
array("id" => null, "from" => null, "color" => "gray", "message" => "This is a test!!", 'notify' => false, 'message_format' => 'html', 'date' => null)
);
}

Expand Down

0 comments on commit e0e4dac

Please sign in to comment.