| 1 | package Net::Flowdock; | ||
| 2 | { | ||
| 3 | $Net::Flowdock::VERSION = '0.03'; | ||
| 4 | } | ||
| 5 | use Moose; | ||
| 6 | |||
| 7 | # ABSTRACT: Flowdock API | ||
| 8 | |||
| 9 | use Net::HTTP::Spore; | ||
| 10 | |||
| 11 | |||
| 12 | has '_client' => ( | ||
| 13 | is => 'rw', | ||
| 14 | lazy => 1, | ||
| 15 | default => sub { | ||
| 16 | my $self = shift; | ||
| 17 | |||
| 18 | my $client = Net::HTTP::Spore->new_from_string( | ||
| 19 | '{ | ||
| 20 | "name": "Flowdock", | ||
| 21 | "authority": "GITHUB:gphat", | ||
| 22 | "version": "1.0", | ||
| 23 | "methods": { | ||
| 24 | "get_flows": { | ||
| 25 | "path": "/v2/flows", | ||
| 26 | "method": "GET", | ||
| 27 | "authentication": true | ||
| 28 | }, | ||
| 29 | "get_flow": { | ||
| 30 | "path": "/v2/flows/:organization/:flow", | ||
| 31 | "required_params": [ | ||
| 32 | "organization", "flow" | ||
| 33 | ], | ||
| 34 | "method": "GET", | ||
| 35 | "authentication": true | ||
| 36 | }, | ||
| 37 | "push_team_inbox": { | ||
| 38 | "path": "/v2/messages/team_inbox/:key", | ||
| 39 | "required_params": [ | ||
| 40 | "source", "from_address", "subject", "content" | ||
| 41 | ], | ||
| 42 | "optional_params": [ | ||
| 43 | "from_name", "project", "format", "tags", "link" | ||
| 44 | ], | ||
| 45 | "method": "POST", | ||
| 46 | "authentication": false | ||
| 47 | }, | ||
| 48 | "push_chat": { | ||
| 49 | "path": "/v2/messages/chat/:key", | ||
| 50 | "required_params": [ | ||
| 51 | "content", "external_user_name" | ||
| 52 | ], | ||
| 53 | "optional_params": [ | ||
| 54 | "tags" | ||
| 55 | ], | ||
| 56 | "method": "POST", | ||
| 57 | "authentication": false | ||
| 58 | }, | ||
| 59 | "send_message": { | ||
| 60 | "path": "/v2/flows/:organization/:flow/messages", | ||
| 61 | "required_params": [ | ||
| 62 | "event", "content" | ||
| 63 | ], | ||
| 64 | "optional_params": [ | ||
| 65 | "tags" | ||
| 66 | ], | ||
| 67 | "method": "POST", | ||
| 68 | "authentication": true | ||
| 69 | } | ||
| 70 | } | ||
| 71 | }', | ||
| 72 | base_url => $self->url, | ||
| 73 | trace => $self->debug | ||
| 74 | ); | ||
| 75 | $client->enable('Format::JSON'); | ||
| 76 | $client->enable('Auth::Basic', username => $self->username, password => $self->password); | ||
| 77 | return $client | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | |||
| 81 | |||
| 82 | has 'debug' => ( | ||
| 83 | is => 'rw', | ||
| 84 | isa => 'Bool', | ||
| 85 | default => 0, | ||
| 86 | ); | ||
| 87 | |||
| 88 | |||
| 89 | has 'key' => ( | ||
| 90 | is => 'rw', | ||
| 91 | isa => 'Str', | ||
| 92 | lazy => 1, | ||
| 93 | default => sub { die "This call requires a key to be set" }, | ||
| 94 | ); | ||
| 95 | |||
| 96 | |||
| 97 | has 'password' => ( | ||
| 98 | is => 'rw', | ||
| 99 | isa => 'Str' | ||
| 100 | ); | ||
| 101 | |||
| 102 | |||
| 103 | has 'url' => ( | ||
| 104 | is => 'rw', | ||
| 105 | isa => 'Str', | ||
| 106 | default => 'https://api.flowdock.com' | ||
| 107 | ); | ||
| 108 | |||
| 109 | |||
| 110 | has 'username' => ( | ||
| 111 | is => 'rw', | ||
| 112 | isa => 'Str' | ||
| 113 | ); | ||
| 114 | |||
| 115 | around BUILDARGS => sub { | ||
| 116 | my $orig = shift; | ||
| 117 | my $self = shift; | ||
| 118 | |||
| 119 | my $params = $self->$orig(@_); | ||
| 120 | my $token = delete $params->{token}; | ||
| 121 | if ($token) { | ||
| 122 | $params->{username} = $token; | ||
| 123 | $params->{password} = ''; | ||
| 124 | } | ||
| 125 | |||
| 126 | return $params; | ||
| 127 | }; | ||
| 128 | |||
| 129 | |||
| 130 | sub get_flow { | ||
| 131 | my $self = shift; | ||
| 132 | my $args = shift; | ||
| 133 | |||
| 134 | return $self->_client->get_flow( | ||
| 135 | organization => $args->{organization}, | ||
| 136 | flow => $args->{flow} | ||
| 137 | ); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | sub get_flows { | ||
| 142 | my $self = shift; | ||
| 143 | my $args = shift; | ||
| 144 | |||
| 145 | return $self->_client->get_flows; | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | sub send_message { | ||
| 150 | my $self = shift; | ||
| 151 | my $args = shift; | ||
| 152 | |||
| 153 | return $self->_client->send_message( | ||
| 154 | organization => $args->{organization}, | ||
| 155 | flow => $args->{flow}, | ||
| 156 | event => $args->{event}, | ||
| 157 | content => $args->{content}, | ||
| 158 | tags => $args->{tags} | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | sub push_team_inbox { | ||
| 164 | my $self = shift; | ||
| 165 | my $args = shift; | ||
| 166 | |||
| 167 | return $self->_client->push_team_inbox( | ||
| 168 | key => $self->key, | ||
| 169 | source => $args->{source}, | ||
| 170 | from_address => $args->{from_address}, | ||
| 171 | from_name => $args->{from_name}, | ||
| 172 | subject => $args->{subject}, | ||
| 173 | content => $args->{content}, | ||
| 174 | project => $args->{project}, | ||
| 175 | format => $args->{format}, | ||
| 176 | tags => $args->{tags}, | ||
| 177 | link => $args->{link} | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub push_chat { | ||
| 183 | my $self = shift; | ||
| 184 | my $args = shift; | ||
| 185 | |||
| 186 | return $self->_client->push_chat( | ||
| 187 | key => $self->key, | ||
| 188 | content => $args->{content}, | ||
| 189 | external_user_name => $args->{external_user_name}, | ||
| 190 | tags => $args->{tags} | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | |||
| 194 | 1; | ||
| 195 | |||
| 196 | __END__ | ||
| 197 | =pod | ||
| 198 | |||
| 199 | =head1 NAME | ||
| 200 | |||
| 201 | Net::Flowdock - Flowdock API | ||
| 202 | |||
| 203 | =head1 VERSION | ||
| 204 | |||
| 205 | version 0.03 | ||
| 206 | |||
| 207 | =head1 SYNOPSIS | ||
| 208 | |||
| 209 | use Net::Flowdock; | ||
| 210 | |||
| 211 | my $client = Net::Flowdock->new(key => 'find-your-own'); | ||
| 212 | |||
| 213 | # Or, if you need to use authenticated methods | ||
| 214 | |||
| 215 | my $client = Net::Flowdock->new(key => 'find-your-own', username => 'foo', password => 'bar'); | ||
| 216 | |||
| 217 | $client->push_team_inbox({ | ||
| 218 | source => 'CPAN', | ||
| 219 | from_address => 'gphat@cpan.org', | ||
| 220 | from_name => 'Cory Watson', | ||
| 221 | subject => 'Uploaded Net::Flowdock', | ||
| 222 | content => "Sho' nuff", | ||
| 223 | project => 'Open Source', | ||
| 224 | tags => 'wow,yeah,poop', | ||
| 225 | link => 'http://search.cpan.org' | ||
| 226 | }); | ||
| 227 | |||
| 228 | =head1 DESCRIPTION | ||
| 229 | |||
| 230 | Net::Flowdock is a simple client for using the L<Flowdock API|https://www.flowdock.com/api>. | ||
| 231 | It specifically speaks to the L<REST|https://www.flowdock.com/api/rest> and | ||
| 232 | L<Push|https://www.flowdock.com/api/push> APIs. | ||
| 233 | |||
| 234 | =head1 ATTRIBUTES | ||
| 235 | |||
| 236 | =head2 debug | ||
| 237 | |||
| 238 | Set/Get the debug flag. | ||
| 239 | |||
| 240 | =head2 key | ||
| 241 | |||
| 242 | Set/Get the key to use when connecting to Flowdock. | ||
| 243 | |||
| 244 | To obtain the API Token go to Settings -> Team Inbox inside a flow. | ||
| 245 | |||
| 246 | =head2 password | ||
| 247 | |||
| 248 | Set/Get the password for authenticated request. | ||
| 249 | |||
| 250 | =head2 url | ||
| 251 | |||
| 252 | Set/Get the URL for Flowdock. Defaults to https://api.flowdock.com. | ||
| 253 | |||
| 254 | =head2 username | ||
| 255 | |||
| 256 | Set/Get the username for authenticated request. | ||
| 257 | |||
| 258 | =head1 METHODS | ||
| 259 | |||
| 260 | =head2 get_flow (organization => $org, flow => $flow) | ||
| 261 | |||
| 262 | Get a single flow. Single flow information always includes user list of flow. Otherwise the data format is identical to the list. | ||
| 263 | |||
| 264 | =head2 get_flows | ||
| 265 | |||
| 266 | Lists the flows that the authenticated user has access to. | ||
| 267 | |||
| 268 | =head2 send_message (organization => $org, flow => $flow, event => $event, content => $content, tags => $tags) | ||
| 269 | |||
| 270 | Send a messge to a flow. | ||
| 271 | |||
| 272 | =over 4 | ||
| 273 | |||
| 274 | =item event | ||
| 275 | |||
| 276 | One of the valid Flowdock message events. Determines the type of message being sent to Flowdock. See Message Types section below. Required. | ||
| 277 | |||
| 278 | =item content | ||
| 279 | |||
| 280 | The actual message. The format of content depends on the event. Required. Types are message (normal chat), status, (status update), mail (team inbox). | ||
| 281 | |||
| 282 | item tags | ||
| 283 | |||
| 284 | List of tags to be added to the message. Can be either an array (JSON only) or a string with tags delimited with commas. User tags should start with '@'. Hashtags can optionally be prefixed with "#". Tags are case insensitive. | ||
| 285 | |||
| 286 | =back | ||
| 287 | |||
| 288 | Some examples: | ||
| 289 | |||
| 290 | A status update: | ||
| 291 | |||
| 292 | $client->send_message({ | ||
| 293 | organization => 'iinteractive', | ||
| 294 | flow => 'testing', | ||
| 295 | event => 'status', | ||
| 296 | content => 'Away for a bit', | ||
| 297 | }); | ||
| 298 | |||
| 299 | A message in chat: | ||
| 300 | |||
| 301 | $client->send_message({ | ||
| 302 | organization => 'iinteractive', | ||
| 303 | flow => 'testing', | ||
| 304 | event => 'message', | ||
| 305 | content => 'I am a robot', | ||
| 306 | tags => 'foo, bar' | ||
| 307 | }); | ||
| 308 | |||
| 309 | XXX Todo: mail | ||
| 310 | |||
| 311 | =head2 push_team_inbox ({ source => $source, from_address => $email }) | ||
| 312 | |||
| 313 | Required fields: | ||
| 314 | |||
| 315 | =over 4 | ||
| 316 | |||
| 317 | =item source | ||
| 318 | |||
| 319 | Human readable identifier of the application that uses the Flowdock API. Only alphanumeric characters, underscores and whitespace can be used. This identifier will be used as the primary method of categorization for the messages. | ||
| 320 | |||
| 321 | Example value: Awesome Issue Management App | ||
| 322 | |||
| 323 | =item from_address | ||
| 324 | |||
| 325 | Email address of the message sender. The email address is used to show a avatar of the sender. You can customize the avatar by registering the address in | ||
| 326 | |||
| 327 | Example value: john.doe@yourdomain.com | ||
| 328 | |||
| 329 | =item subject | ||
| 330 | |||
| 331 | Subject line of the message, will be displayed as the title of Team Inbox message. | ||
| 332 | |||
| 333 | =item content | ||
| 334 | |||
| 335 | Content of the message, will be displayed as the body of Team Inbox message. | ||
| 336 | |||
| 337 | Following HTML tags can be used: a abbr acronym address article aside b big blockquote br caption cite code dd del details dfn div dl dt em figcaption figure footer h1 h2 h3 h4 h5 h6 header hgroup hr i img ins kbd li nav ol p pre samp section small span strong sub summary sup table tbody td tfoot th thead tr tt ul var wb | ||
| 338 | |||
| 339 | =back | ||
| 340 | |||
| 341 | Optional fields: | ||
| 342 | |||
| 343 | =over 4 | ||
| 344 | |||
| 345 | =item from_name | ||
| 346 | |||
| 347 | Name of the message sender. | ||
| 348 | |||
| 349 | Example value: John Doe | ||
| 350 | |||
| 351 | =item project | ||
| 352 | |||
| 353 | Human readable identifier for more detailed message categorization. Only alphanumeric characters, underscores and whitespace can be used. This identifier will be used as the secondary method of categorization for the messages. | ||
| 354 | |||
| 355 | Example value: My Project | ||
| 356 | |||
| 357 | =item format | ||
| 358 | |||
| 359 | Format of the message content, default value is "html". Only HTML is currently supported. | ||
| 360 | |||
| 361 | Example value: html | ||
| 362 | |||
| 363 | =item tags | ||
| 364 | |||
| 365 | Tags of the message, separated by commas. | ||
| 366 | |||
| 367 | Example value: cool,stuff | ||
| 368 | |||
| 369 | =item link | ||
| 370 | |||
| 371 | Link associated with the message. This will be used to link the message subject in Team Inbox. | ||
| 372 | |||
| 373 | Example value: http://www.flowdock.com/ | ||
| 374 | |||
| 375 | =back | ||
| 376 | |||
| 377 | =head2 push_chat ({ content => $content, external_user_name => $username }) | ||
| 378 | |||
| 379 | =over 4 | ||
| 380 | |||
| 381 | =item content | ||
| 382 | |||
| 383 | Content of the message. Tags will be automatically parsed from the message content. Required. | ||
| 384 | |||
| 385 | =item external_user_name | ||
| 386 | |||
| 387 | Name of the "user" sending the message. Required. | ||
| 388 | |||
| 389 | =item tags | ||
| 390 | |||
| 391 | Tags of the message, separated by commas. Optional. | ||
| 392 | |||
| 393 | Example value: cool,stuff | ||
| 394 | |||
| 395 | =back | ||
| 396 | |||
| 397 | =head1 AUTHENTICATED | ||
| 398 | |||
| 399 | =head1 ANONYMOUS | ||
| 400 | |||
| 401 | =head1 AUTHOR | ||
| 402 | |||
| 403 | Cory G Watson <gphat@cpan.org> | ||
| 404 | |||
| 405 | =head1 COPYRIGHT AND LICENSE | ||
| 406 | |||
| 407 | This software is copyright (c) 2012 by Infinity Interactive, Inc. | ||
| 408 | |||
| 409 | This is free software; you can redistribute it and/or modify it under | ||
| 410 | the same terms as the Perl 5 programming language system itself. | ||
| 411 | |||
| 412 | =cut | ||
| 413 |