summary refs log tree commit diff
path: root/lib/githubexplorer/Gexf.pm
blob: 652800e67617ef7943692cfa2533fa235d399f49 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package githubexplorer::Gexf;

use Moose;
use XML::Simple;

has schema => (is => 'ro', isa => 'Object', required => 1);

has graph => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub {
        my $graph = {
            gexf => {
                version => "1.0",
                meta    => { creator => ['rtgi'] },
                graph   => {
                    type       => 'static',
                    attributes => {
                        class     => 'node',
                        type      => 'static',
                        attribute => [
                            {
                                id    => 0,
                                type  => 'string',
                                title => 'name'
                            },
                            {
                                id    => 1,
                                type  => 'string',
                                title => 'followers_count'
                            },
                            {
                                id    => 2,
                                type  => 'string',
                                title => 'following_count'
                            },
                        ]
                    }
                }
            }
        };
    }
);

sub profiles {
    my $self     = shift;
    my $profiles = $self->schema->resultset('Profiles')->search();

    while ( my $profile = $profiles->next ) {
        my $node = {
            id              => $profile->id,
            label           => $profile->login,
            name            => $profile->name,
            followers_count => $profile->followers_count,
            following_count => $profile->following_count,
        };
        push @{ $self->graph->{gexf}->{graph}->{nodes}->{node} }, $node;
    }

    my $edges = $self->schema->resultset('Follow')->search();
    my $id    = 0;
    while ( my $edge = $edges->next ) {
        my $e = {
            cardinal => 1,
            source   => $edge->origin,
            target   => $edge->source,
            type     => 'dir',
            id       => $id++,
        };
        push @{ $self->graph->{gexf}->{graph}->{eges}->{edge} }, $e;
    }

    my $xml_out = XMLout( $self->graph, AttrIndent => 1, keepRoot => 1 );
    return $xml_out;
}

1;