Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed Apr 10, 2016
1 parent 81a557d commit 61c78c9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
2 changes: 1 addition & 1 deletion META6.json
Expand Up @@ -2,7 +2,7 @@
"perl" : "6.c",
"name" : "GlotIO",
"version" : "1.001001",
"description" : "use glot.io API: pastebin allowing execution of code",
"description" : "Use glot.io API: pastebin allowing execution of code",
"depends" : [
"HTTP::Tinyish",
"JSON::Fast",
Expand Down
38 changes: 38 additions & 0 deletions README.md
Expand Up @@ -107,6 +107,44 @@ A shortcut for calling `.run` (takes same arguments) and returning
just the `stderr` key. Will `fail` with the entire `Hash` returned
from `.run` if the program errors out.

## `.list`

```perl6
say $glot.list[0..3];

say $glot.list(:3page, :50per-page, :mine)[0];
```

Fetches a list of metadata for snippets. Takes optional
named arguments:

* `page` positive integer starting at and defaulting to 1. Specifies the page to display
* `per-page` positive integer stating the number of items to return perl
page. Defaults to `100`. Maximum value is `100`.
* `mine` boolean specifying whether public or your own snippets should be
listed. Defaults to `False`. Requires `key` argument to `.new` to be provided
if set to `True.

Returns a list of `Hash`s as the one shown here:

```perl6
[
{
created => "2016-04-09T17:52:19Z",
files_hash => "2afa1f37cc0bc7d033e4b3a049659792f5caac6d",
id => "edltstt3n0",
language => "cpp",
modified => "2016-04-09T17:52:19Z",
owner => "anonymous",
public => Bool::True,
title => "Untitled",
url => "https://snippets.glot.io/snippets/edltstt3n0",
},
...
]
```


----

# REPOSITORY
Expand Down
10 changes: 2 additions & 8 deletions examples/glot.p6
Expand Up @@ -3,11 +3,5 @@ use lib 'lib';
use GlotIO;
my GlotIO $glot .= new: key => 't/key'.IO.lines[0];

say $glot.stderr: 'perl6', 'note "Hello, World!"';

=finish

say $glot.run: 'perl6', [
'main.p6' => 'use lib "."; use Foo; doit;',
'Foo.pm6' => 'unit module Foo; sub doit is export { say "42" }',
];
use Data::Dump;
say Dump $glot.list(:4page, :!mine);
47 changes: 40 additions & 7 deletions lib/GlotIO.pm6
Expand Up @@ -5,15 +5,19 @@ use JSON::Fast;
use URI::Escape;

has Str $.key;
has $!api-url = 'https://run.glot.io';
has $!run-api-url = 'https://run.glot.io';
has $!snip-api-url = 'https://snippets.glot.io';
has $!ua = HTTP::Tinyish.new(agent => "Perl 6 NASA.pm6");

method !request ($method, $url, $content?, *%params) {
method !request ($method, $url, $content?, *%params, Bool :$add-token) {
#%params = %params.kv.map: { uri-escape $_ };

my %res;
if ( $method eq 'GET' ) {
%res = $!ua.get: $url;
%res = $!ua.get: $url,
headers => %(
('Authorization' => "Token $!key" if $add-token)
);
}
elsif ( $method eq 'POST' ) {
%res = $!ua.post: $url,
Expand All @@ -28,15 +32,28 @@ method !request ($method, $url, $content?, *%params) {
}

%res<success> or fail "ERROR %res<status>: %res<reason>";
return from-json %res<content>;

return from-json %res<content>
unless %res<headers><link>;

say %res<headers><link>;
say '---';
my %links;
for %res<headers><link> ~~ m:g/ '<'(.+?)'>;'\s+'rel="'(.+?)\" / -> $m {
%links{ ~$m[1] } = ~$m[0];
};
return %(
|%links,
content => from-json %res<content>
);
}

method languages {
self!request('GET', $!api-url ~ '/languages').map: *<name>;
self!request('GET', $!run-api-url ~ '/languages').map: *<name>;
}

method versions (Str $lang) {
my $uri = $!api-url ~ '/languages/' ~ uri-escape($lang);
my $uri = $!run-api-url ~ '/languages/' ~ uri-escape($lang);
self!request('GET', $uri).map: *<version>;
}

Expand All @@ -45,7 +62,7 @@ multi method run (Str $lang, @files, :$ver = 'latest') {
%content<files> = @files.map: {
%(name => .key, content => .value )
};
my $uri = $!api-url ~ '/languages/' ~ uri-escape($lang)
my $uri = $!run-api-url ~ '/languages/' ~ uri-escape($lang)
~ '/' ~ uri-escape($ver);
self!request: 'POST', $uri, to-json %content;
}
Expand All @@ -59,8 +76,24 @@ method stdout (|c) {
fail "Error: $res" if $res<error>;
$res<stdout>;
}

method stderr (|c) {
my $res = self.run(|c);
fail "Error: $res" if $res<error>;
$res<stderr>;
}

method list (
Int $page = 1,
Int :$per-page = 100,
Str :$owner,
Str :$language,
Bool :$mine = False,
) {
self!request: 'GET', $!snip-api-url ~ '/snippets'
~ "?page=$page&per_page=$per-page"
~ ( $owner.defined ?? "&owner=" ~ uri-escape($owner) !! '' )
~ ( $language.defined ?? "&language=" ~ uri-escape($language) !! '' ),
add-token => $mine;
}

0 comments on commit 61c78c9

Please sign in to comment.