| 1 | package LinkEmbedder::Link::Travis; | ||
| 2 | use Mojo::Base 'LinkEmbedder::Link'; | ||
| 3 | |||
| 4 | has provider_name => 'Travis'; | ||
| 5 | has provider_url => sub { Mojo::URL->new('https://travis-ci.org') }; | ||
| 6 | has thumbnail_height => 501; | ||
| 7 | has thumbnail_url => $ENV{LINK_EMBEDDER_TRAVIS_THUMBNAIL_URL} | ||
| 8 | || 'https://cdn.travis-ci.org/images/logos/TravisCI-Mascot-1-20feeadb48fc2492ba741d89cb5a5c8a.png'; | ||
| 9 | has thumbnail_width => 497; | ||
| 10 | |||
| 11 | sub learn_p { | ||
| 12 | my $self = shift; | ||
| 13 | my $api_url = $self->url->clone->host('api.travis-ci.org'); | ||
| 14 | my $api_path = $api_url->path; | ||
| 15 | |||
| 16 | return $self->SUPER::learn_p unless $api_path =~ m!^/(.*/builds/\d+)$!; | ||
| 17 | |||
| 18 | $api_url->path->parse("/repositories/$1"); | ||
| 19 | return $self->_get_p($api_url)->then(sub { $self->_learn_from_json(shift) }); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub _learn_from_json { | ||
| 23 | my ($self, $tx) = @_; | ||
| 24 | my $json = $tx->res->json; | ||
| 25 | |||
| 26 | $self->type('rich'); | ||
| 27 | |||
| 28 | if (my $description = $json->{message}) { | ||
| 29 | $description = "$json->{author_name}: $description" if $json->{author_name}; | ||
| 30 | $self->description($description); | ||
| 31 | } | ||
| 32 | |||
| 33 | if ($json->{finished_at}) { | ||
| 34 | $self->title(sprintf 'Build %s at %s', $json->{status} ? 'failed' : 'succeeded', $json->{finished_at}); | ||
| 35 | } | ||
| 36 | elsif ($json->{started_at}) { | ||
| 37 | $self->title(sprintf 'Started building at %s.', $json->{started_at}); | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | $self->title('Build has not been started.'); | ||
| 41 | } | ||
| 42 | |||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | 1; |