-
Notifications
You must be signed in to change notification settings - Fork 34
/
account_sample.pl
executable file
·120 lines (79 loc) · 2.82 KB
/
account_sample.pl
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/perl
=pod
=head1 NAME
account_sample.pl - Simple example of using the Ohloh API from Perl
=head1 SYNOPSIS
account_sample.pl api_key email
=head1 DESCRIPTION
This is a small example of how to use the Ohloh API from Perl,
mimicking the Ruby example - account_sample.rb
Detailed information about the Ohloh API can be found at
https://github.com/blackducksw/ohloh_api
The script takes an API key and the email address of a developer
registered on Ohloh as parameters, and prints out all the
information of that account.
=head1 EXAMPLE
./account_sample.pl YOUR_API_KEY_HERE [email protected]
=head1 DEPENDENCIES
This script uses
L<LWP::Simple> (to query the Ohloh website),
L<Digest::MD5> (to MD5s the email address as requested by the API)
and
L<XML::Simple> (to parse the server's answer).
=head1 VERSION
This documentation refers to account_sample version 0.1
=head1 AUTHOR
Yanick Champoux ([email protected])
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2007 Yanick Champoux ([email protected]). All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See perldoc perlartistic.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
use strict;
use warnings;
use LWP::Simple;
use Digest::MD5 qw/ md5_hex /;
use XML::Simple;
my $VERSION = '0.01';
my $API_VERSION = 1;
die_usage() unless @ARGV == 2;
my ( $api_key, $email ) = @ARGV;
my $email_md5 = md5_hex($email);
### let's build the request url
# first the base url
my $url = 'https://www.openhub.net/';
# then the email "page" we want
$url .= "accounts/$email_md5.xml";
# and finally the API version and the api key
$url .= '?' . join '&' => "v=$API_VERSION", "api_key=$api_key";
# request the url from the server
my $response = get $url or die "Ohloh server didn't return anything\n";
# parse the XML response
my $xml = eval { XMLin($response) } or die "Server didn't return valid XML\n";
# was the request a success?
die "request didn't succeed: $xml->{error}\n"
unless $xml->{status} eq 'success';
# make an alias to the info we want
my %account = %{ $xml->{result}{account} };
# now we print the info for the account
for my $attr (
qw/ id name created_at updated_at homepage_url
avatar_url posts_count location country_code
latitude longitude /
) {
# only print the attribute if it exist
printf "%-20s: %s\n", $attr, $account{$attr}
if defined $account{$attr} and not ref $account{$attr};
}
# print the kudo information
printf "%-20s: %s\n", 'kudo rank', $account{kudo_score}{kudo_rank}
if $account{kudo_score};
exit;
### utility functions ######################################
sub die_usage {
die "usage: $0 api_key email\n";
}
__END__