Skip to content

Instantly share code, notes, and snippets.

@Cside
Created March 29, 2012 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cside/2233692 to your computer and use it in GitHub Desktop.
Save Cside/2233692 to your computer and use it in GitHub Desktop.
create mock methods of titanium
#!/usr/bin/env perl
use common::sense;
use Data::Dumper;
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Terse = 1;
sub p ($) { print Dumper shift }
use Coro;
use Coro::LWP;
use Web::Query;
use LWP::UserAgent::WithCache;
use List::MoreUtils qw(mesh);
$Web::Query::UserAgent = LWP::UserAgent::WithCache->new(+{
'namespace' => 'lwp-cache',
'cache_root' => '/Users/Cside/.cache',
'default_expires_in' => 60 * 100,
});
sub get_urls {
@{
wq('http://developer.appcelerator.com/apidoc/mobile/latest/')
->find('#sidenav li a')
->map(sub { $_->attr('href') })
};
};
sub get_api_maps {
my ($url, @maps) = @_;
my $wq = wq($url);
my %map;
my $flg = 0;
my $namespace = $wq->find('h1.namespace')->text;
my @objects;
$wq->find('.apidoc_container > *')->each(sub {
my $elm = $_;
my $type = $elm->attr('id');
if ($type ~~ [qw(objects methods properties)]) {
$flg = $type;
return;
}
if ($flg) {
my @names = @{$elm->find('td.name')->map(sub { $_->text })};
if ($flg eq 'objects') {
push @objects, @names;
} elsif ($flg eq 'methods') {
my @summaries = @{
$elm->find('td.summary')->map(sub {
$_->html =~ m|Creates and returns an instance of .+?<code>(.+?)</code>| ? $1 : '';
})
};
$map{$flg} = +{mesh @names, @summaries} if @names;
} elsif ($flg eq 'properties') {
$map{$flg} = \@names if @names;
}
$flg = 0;
}
});
my @maps = ({$namespace => \%map});
if (@objects) {
my @coros;
for my $object_name (@objects) {
push @coros, async {
my $url = "http://developer.appcelerator.com/apidoc/mobile/latest/${object_name}-object.html";
push @maps, get_api_maps($url, @maps);
};
}
$_->join for @coros;
}
@maps;
}
my ($obj_str, $method_str) = ("", "");
my @queue = do {
my @data;
my @coros;
for my $url (get_urls()) {
push @coros, async {
push @data, get_api_maps($url);
};
}
$_->join for @coros;
@data;
};
for my $one (sort { (keys %$a)[0] cmp (keys %$b)[0] } @queue) {
my ($ns, $data) = %$one;
my $fix = sub { $_[0] =~ s/\.([^.]+)/['$1']/gr };
$obj_str .= $fix->($ns)
. " = function () {};\n";
if (my $methods = $data->{methods}) {
for my $method (keys %$methods) {
my $_ns = $fix->("$ns.$method");
$method_str .= $methods->{$method}
? $_ns . " = function () { return " . $fix->($methods->{$method}) . " };\n"
: $_ns . " = function () {};\n";
}
} elsif (my $properties = $data->{properties}) {
}
}
say <<"RESULT";
$obj_str
$method_str
var Ti = Titanium;
RESULT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment