-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-pjw-groupme-rest-api.php
49 lines (43 loc) · 1.24 KB
/
class-pjw-groupme-rest-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* Simple GroupMe REST API class
*
*/
class pjw_groupme_rest_api {
private $access_token;
public function __construct( $access_token ) {
$this->access_token = $access_token;
}
protected function make_get_request( $path, $args = array() ) {
$args['token'] = $this->access_token;
$url = add_query_arg( $args, 'https://api.groupme.com/v3/' . $path );
$response = wp_remote_get( $url );
if ( 200 == wp_remote_retrieve_response_code( $response ) ) {
return json_decode( wp_remote_retrieve_body( $response ) )->response;
} else {
return array();
}
}
protected function messages_sorter( $a, $b ) {
if ( $a->created_at == $b->created_at ) {
return 0;
} elseif ( $a->created_at < $b->created_at ) {
return -1;
} else {
return 1;
}
}
/** Public API **/
public function groups() {
return $this->make_get_request( 'groups' );
}
public function group( $group_id ) {
return $this->make_get_request( 'groups/' . $group_id );
}
public function messages( $group_id, $args = array() ) {
$messages = $this->make_get_request( 'groups/' . $group_id . '/messages', $args );
// Ensure we are always chronologically sorted
usort( $messages->messages, array( $this, 'messages_sorter' ) );
return $messages;
}
}