# Wiki-Interface for ATITD
# ========================
#
# Can read the Wiki of a telling and return pages.

package ATITD::Wiki;

use strict;
use warnings;

use constant {
	# String to send as User-Agent header when making requests.
	UA_STRING => 'Lib-ATITD-Wiki-Perl/0.3',
	
	# Proxy to use
	PROXY => undef,
};

use ATITD::Wiki::Page;
use LWP::UserAgent;
use URI;

# Creates a Wiki-Interface for a telling at a URL; optionally loads a page
sub new {
	my ($class, $telling, $url, $page_name) = @_;
	my $self = bless {
		telling => $telling,
		url => URI->new($url),
		ua => LWP::UserAgent->new(UA_STRING),
	};
	
	$self->ua->proxy(http => PROXY)
		if defined PROXY;
	
	return defined $page_name ? $self->page($page_name) : $self;
}

# Our telling.
sub telling { $_[0]->{telling} }

# Our User-Agent header.
sub ua { $_[0]->{ua} }

# Our base URL.
sub url { $_[0]->{url} }

# A page from the Wiki; sends a HTTP-Request.
sub page {
	my ($self, $page_name) = @_;
	
	return ATITD::Wiki::Page->new($self, $page_name)->fetch;
}

1;
