NAME
ACME::QuoteDB - API implements CRUD for a Collection of Quotes (adages/proverbs/sayings/epigrams, etc)
VERSION
Version 0.1.2
SYNOPSIS
Easy access to a collection of quotes (the 'Read' part)
As quick one liner:
# randomly display one quote from all available. (like motd, 'fortune')
perl -MACME::QuoteDB -le 'print quote()'
# Say you have populated your quotes database with some quotes from
# 'The Simpsons'
# randomly display one quote from all available for person 'Ralph'
perl -MACME::QuoteDB -le 'print quote({AttrName => "ralph"})'
# example of output
Prinskipper Skippel... Primdable Skimpsker... I found something!
-- Ralph Wiggum
# get 1 quote, only using these categories (you have defined)
perl -MACME::QuoteDB -le 'print quote({Category => [qw(Humor Cartoon ROTFLMAO)]})'
In a script/module, OO usage:
use ACME::QuoteDB;
my $sq = ACME::QuoteDB->new;
# get random quote from any attribution
print $sq->get_quote;
# get random quote from specified attribution
print $sq->get_quote({AttrName => 'chief wiggum'});
# example of output
I hope this has taught you kids a lesson: kids never learn.
-- Chief Wiggum
# get all quotes from one source
print @{$sq->get_quotes({Source => 'THE SimPSoNs'})}; # is case insensitive
# get 2 quotes, with a low rating that contain a specific string
print @{$sq->get_quotes_contain({
Contain => 'til the cow',
Rating => '1-5',
Limit => 2
})};
# get 5 quotes from given source
print @{$sq->get_quotes({Source => 'The Simpsons',
Limit => 5
})};
# list all sources
print $sq->list_attr_sources;
# list all categories
print $sq->list_categories;
DESCRIPTION
This module provides an easy to use programmitic interface to a database (sqlite3 or mysql) of 'quotes'. (any content really, that can fit into our "defined format")
For simplicty you can think of it as a modern fancy perl version of fortune (with a management interface, remote database connection support, plus additional features and some not (yet) supported)
Originally, this module was designed for a collection of quotes from a well known TV show, once I became aware that distributing it as such would be copyright infringement, I generalized the module, so it can be loaded with 'any' content. (in the quote-ish format)
Supported actions include: (CRUD)
- 1 Create
-
* Adding quote(s) * 'Batch' Loading quotes from a file (stream, other database, etc)
- 1 Read
-
* Displaying a single quote, random or based on some criteria * Displaying multiple quotes, based on some criteria * Displaying a specific number of quotes, based on some search criteria
- 1 Update
-
* Update an existing quote
- 1 Delete
-
* Remove an existing quote
Examples of Read
my $sq = ACME::QuoteDB->new;
# on Oct 31st, one could get an appropriate (humorous) quote:
# (providing, of course that you have defined/populated these categories)
print $sq->get_quote({Category => [qw(Haloween Humor)]});
# get everthing from certain attributor:
print @{$sq->get_quotes({AttrName => 'comic book guy'})};
# get all quotes with a certain rating
$sq->get_quotes({Rating => '7.0'});
# get all quotes containing some specific text:
$sq->get_quotes_contain({Contain => 'til the cow'});
Examples of Create
(See ACME::QuoteDB::LoadDB for batch loading)
# add a quote to the database
my $id_of_added = $sq->add_quote({
Quote => 'Hi, I'm Peter,...",
AttrName => 'Peter Griffin',
Source => 'Family American Dad Guy',
Rating => '1.6',
Category => 'TV Humor',
});
Example of Update
# update a quote in the database
my $quote_id = $sq->get_quote_id({Quote => 'Hi, I'm Peter,..."});
$sq->update_quote({
QuoteId => $quote_id,
Quote => 'Hi, I'm Peter, and your not!',
AttrName => 'Peter Griffin',
Source => 'Family Guy',
Rating => '5.7',
Category => [qw(TV Humor Crude Adolescent)]
});
# category/quote is a many to many relationship:
# 1 quote can be in many categories. (and of course 1 category can have many quotes)
Example of Delete
# delete a quote from the database
$sq->delete_quote({QuoteId => $quote_id});
- record format
-
One full quote database record currently consits of 5 fields:
Quote, AttrName, Source, Rating, Category
Quote => 'the quote desired' # mandatory AttrName => 'who said it' # mandatory Source => 'where was it said' Rating => 'how you rate the quote/if at all', Category => 'what category is the quote in',
For example:
Quote => 'Hi, I'm Peter,...", AttrName => 'Peter Griffin', Source => 'Family Guy', Rating => '8.6', Category => 'TV Humor',
- * NOTE: In order for this module to be useful one has to load some quotes to the database. Hey, just once though :) (see below - Loading Quotes)
OVERVIEW
Easy, quick auto-CRUD access to a collection of quotes. (which you provide)
Some ideal uses for this module could be:
Quotes Website (quotes/movie/lyrics/limerick/proverbs/jokes/etc)
perl replacement for 'fortune'
Dynamic signature generation
international languages (has utf8 support)
convenient storing/sharing collections of quotes
for me to finally have a place to store (and manage) quotes (that can be easily backed up or even to a remote db if desired)
anywhere perl is supported and 'quotes' are desired.
others? (let me know what you do, if you want, if you do)
See "DESCRIPTION" above
Also see ACME::QuoteDB::LoadDB
USAGE
use ACME::QuoteDB;
my $sq = ACME::QuoteDB->new;
print $sq->get_quote;
# examples are based on quotes data in the test database.
# (see tests t/data/)
# get specific quote based on basic text search.
# search all 'ralph' quotes for string 'wookie'
print $sq->get_quotes_contain({
Contain => 'wookie',
AttrName => 'ralph',
Limit => 1 # only return 1 quote (if any)
});
# output:
I bent my wookie.
-- Ralph Wiggums
# returns all quotes attributed to 'ralph', with a rating between
# (and including) 7 to 9
print join "\n", @{$sq->get_quotes({
AttrName => 'ralph',
Rating => '7-9'
})
};
# same thing but limit to 2 results returned
# (and including) 7 to 9
print join "\n", @{$sq->get_quotes({
AttrName => 'ralph',
Rating => '7-9',
Limit => 2
})
};
# get 6 random quotes (any attribution)
foreach my $q ( @{$sq->get_quotes({Limit => 6})} ) {
print "$q\n";
}
# get list of available attributions (that have quotes provided by this module)
print $sq->list_attr_names;
# any unique part of name will work
# i.e these will all return the same results (because of our limited
# quotes db data set)
print $sq->get_quotes({AttrName => 'comic book guy'});
print $sq->get_quotes({AttrName => 'comic book'});
print $sq->get_quotes({AttrName => 'comic'});
print $sq->get_quotes({AttrName => 'book'});
print $sq->get_quotes({AttrName => 'book guy'});
print $sq->get_quotes({AttrName => 'guy'});
# get all quotes, only using these categories (you have defined)
print @{$sq->get_quotes({ Category => [qw(Humor ROTFLMAO)] })};
# get all quotes from Futurama
print @{$sq->get_quotes({Source => Futurama})};
Also see t/02* included with this distribution. (available from the CPAN if not included on your system)
SUBROUTINES/METHODS
For the most part this is an OO module. There is one function (quote) provided for command line 'one liner' convenience.
quote
returns one quote. (is exported).
this takes identical arguments to 'get_quote'. (see below)
example:
perl -MACME::QuoteDB -le 'print quote()'
new
instantiate a ACME::QuoteDB object.
takes no arguments
# example
my $sq = ACME::QuoteDB->new;
get_quote
returns one quote
# get random quote from any attribution
print $sq->get_quote;
# get random quote from specified attribution
print $sq->get_quote({AttrName => 'chief wiggum'});
Optional arguments, a hash ref.
available keys: AttrName, Rating
my $args_ref = {
AttrName => 'chief wiggum'
Rating => 7,
};
print $sq->get_quote($args_ref);
Note: The 'Rating' option is very subjective.
It's a 0-10 scale of 'quality' (or whatever you decide it is)
To get a list of the available AttrNames use the list_attr_names method
listed below.
Any unique part of name will work
Example, for attribution 'comic book guy'
# these will all return the same results
print $sq->get_quotes({AttrName => 'comic book guy'});
print $sq->get_quotes({AttrName => 'comic book'});
print $sq->get_quotes({AttrName => 'comic'});
print $sq->get_quotes({AttrName => 'book'});
print $sq->get_quotes({AttrName => 'book guy'});
print $sq->get_quotes({AttrName => 'guy'});
# However, keep in mind the less specific the request is the more results
# are returned, for example the last one would match, 'Comic Book Guy',
# 'Buddy Guy' and 'Guy Smiley',...
add_quote
Adds the supplied record to the database
possible Key arguments consist of:
Quote, AttrName, Source, Rating, Category
with only Quote and AttrName being mandatory (all are useful though):
For Example:
my $q = 'Lois: Peter, what did you promise me?' .
"\nPeter: That I wouldn't drink at the stag party." .
"\nLois: And what did you do?" .
"\nPeter: Drank at the stag pa-- ... Whoa. I almost walked into that one.";
$sq->add_quote({
Quote => $q,
AttrName => 'Peter Griffin',
Source => 'Family Guy',
Rating => '8.6',
Category => 'TV Humor',
});
get_quote_id (very beta)
given a (verbatim) quote, will retrieve that quotes id
(only useful for then doing an L</update> or L</delete>
possible Key arguments consist of: Quote
my $q = 'Lois: Peter, what did you promise me?' .
"\nPeter: That I wouldn't drink at the stag party." .
"\nLois: And what did you do?" .
"\nPeter: Drank at the stag pa-- ... Whoa. I almost walked into that one.";
my $qid = $sq->get_quote_id({Quote => $q});
print $qid; # 30
delete_quote (very beta)
deletes an existing quote in the database
takes an valid quote id (see L</get_quote_id>)
possible Key arguments consist of: QuoteId
$sq->delete_quote({QuoteId => $qid});
update_quote (very beta)
updates an existing quote in the database
possible Key arguments consist of: QuoteId, Quote
my $q = 'Lois: Peter, what did you promise me?' .
"\nPeter: That I wouldn't drink at the stag party." .
"\nLois: And what did you do?" .
"\nPeter: Drank at the stag pa-- ... Whoa. I almost walked into that one.";
$q =~ s/Lois/Marge/xmsg;
$q =~ s/Peter/Homer/xmsg;
$sq->update_quote({
QuoteId => $qid, # as returned from L</get_quote_id>
Quote => $q,
AttrName => 'Lois Simpson',
Source => 'The Simpsons Guys',
Rating => '9.6',
Category => 'Sometimes Offensive Humor',
});
get_quotes
returns zero or more quote(s)
Optional arguments, a hash ref.
available keys: AttrName, Rating, Limit
# returns 2 ralph wiggum quotes with a rating between
# (and including) 7 to 9
print join "\n", @{$sq->get_quotes({
AttrName => 'ralph',
Rating => '7-9',
Limit => 2
})
};
AttrName and Rating work exactely the same as for get_quote (docs above)
Limit specifies the amout of results you would like returned. (just like
with SQL)
get_quotes_contain
returns zero or more quote(s), based on a basic text search.
# get specific quote based on basic text search.
# search all ralph wiggum quotes for string 'wookie'
print $sq->get_quotes_contain({
Contain => 'wookie',
AttrName => 'ralph',
Limit => 1 # only return 1 quote (if any)
})->[0]; # q{Ralph: I bent my wookie.};
Optional arguments, a hash ref.
available keys: AttrName, Contain, Limit
AttrName and Limit work exactly the same as for get_quotes (docs above)
Contain specifies a text string to search quotes for. If a AttrName
option is included, search is limited to that attribution.
Contain is a simple text string only. Regex not supported
Contain literally becomes: AND quote LIKE '%$contain%'
list_attr_names
returns a list of attributions (name) for which we have quotes.
# get list of available attributions (that have quotes provided by this module)
print $sq->list_attr_names;
list_categories
returns a list of categories defined in the database
# get list of available categories (that have quotes provided by this module)
print $sq->list_categories;
list_attr_sources
returns a list of attribution sources defined in the database
# get list of attribution sources (that have quotes provided by this module)
print $sq->list_attr_sources;
LOADING QUOTES
In order to actually use this module, one has to load quotes content, hopefully this is relativly easy,... (see t/01-load_quotes.t in tests)
- 1 add_quote, one record at a time, probably within an iteration loop
-
see "add_quote"
- 1 (Batch Load) load quotes from a csv file. (tested with comma and tab delimiters)
-
format of file must be as follows: (headers) "Quote", "Attribution Name", "Attribution Source", "Category", "Rating" for example: "Quote", "Attribution Name", "Attribution Source", "Category", "Rating" "I hope this has taught you kids a lesson: kids never learn.","Chief Wiggum","The Simpsons","Humor",9 "Sideshow Bob has no decency. He called me Chief Piggum. (laughs) Oh wait, I get it, he's all right.","Chief Wiggum","The Simpsons","Humor",8
- 1 if these dont suit your needs, ACME::QuoteDB::LoadDB is sub-classable,
-
so one can extract data anyway they like and populate the db themselves. (there is a test that illustrates overriding the stub method, 'dbload') you need to populate a record data structure: $self->set_record(quote => q{}); # mandatory $self->set_record(name => q{}); # mandatory $self->set_record(source => q{}); # optional but useful $self->set_record(catg => q{}); # optional but useful $self->set_record(rating => q{}); # optional but useful # then to write the record you call $self->write_record; NOTE: this is a record-by-record operation, so one would perform this within a loop. there is no bulk (memory dump) write operation currently.
For more see ACME::QuoteDB::LoadDB
DIAGNOSTICS
An error such as:
DBD::SQLite::db prepare_cached failed: no such table: ,...
probably means that you do not have a database created in the correct format.
basically, you need to create the database, usually, on a first run
you need to add the flag (to the loader):
create_db => 1, # first run, create the db
appending to an existing database is the default behaviour
see "create_db_tables" in ACME::QuoteDB::LoadDB
CONFIGURATION AND ENVIRONMENT
if you are running perl > 5.8.5 and have access to install cpan modules, you should have no problem installing this module (utf-8 support in DBD::SQLite not avaible until 5.8 - we don't support 'non utf-8 mode)
By default, the quotes database used by this module installs in the system path, 'lib', (See "INSTALL PATHS" in Module::Build) as world writable - i.e. 0666 (and probably owned by root) If you don't like this, you can modify Build.PL to not chmod the file and it will install as 444/readonly, you can also set a chown in there for whoever you want to have RW access to the quotes db.
Alternativly, one can specify a location to a quotes database (file) to use. (Since the local mode is sqlite3, the file doesn't even need to exist, just needs read/write access to the path on the filesystem)
Set the environmental variable:
$ENV{ACME_QUOTEDB_PATH} (untested on windows)
(this has to be set before trying a database load and also (everytime before using this module, obviouly)
Something such as:
BEGIN { # give alternate path to the DB # doesn't need to exist, will create $ENV{ACME_QUOTEDB_PATH} = '/home/me/my_stuff/my_quote_db' }
* (NOTE: be sure this (BEGIN) exists *before* the 'use ACME::QuoteDB' lines)
The default is to use sqlite3.
In order to connect to a mysql database, several environmental variables are required.
BEGIN { # have to set this to use remote database $ENV{ACME_QUOTEDB_REMOTE} = 'mysql'; $ENV{ACME_QUOTEDB_DB} = 'acme_quotedb'; $ENV{ACME_QUOTEDB_HOST} = 'localhost'; $ENV{ACME_QUOTEDB_USER} = 'acme_user'; $ENV{ACME_QUOTEDB_PASS} = 'acme'; }
Set the above in a begin block.
The database connection is transparent.
Module usage wise, all operations are the same but now you will be writing to the remote mysql database specified.
(The user will need read/write permissions to the db/tables) (mysql admin duties are beyond the scope of this module)
The only supported databases at this time are sqlite and mysql.
It is trivial to add support for others
DEPENDENCIES
criticism (pragma - enforce Perl::Critic if installed)
version(pragma - version numbers)
INCOMPATIBILITIES
none known of
SEE ALSO
man fortune (unix/linux)
AUTHOR
David Wright, <david_v_wright at yahoo.com>
TODO
- 1 if the database cannot be found, no error is printed!!!
-
or if you have no write access to it! "you'll just get 'no attribute can be found,,...", which is cryptic to say the least!
- 1 add a dump backup to csv
-
a backup mechanism for your db to a regular text csv file.
- 1 clean up tests 'skip if module X' not installed
-
(one of sqlite3 or mysql is required). currently dies if DBD::SQLite not installed
- 1 support multiple categories from LoadDB
-
how to load multipul categories from a csv file? (try to avoid somthing ugly in our csv file format). or maybe don't support this.
- 1 (possibly) support long/short quotes output (see 'man fortune')
BUGS AND LIMITATIONS
The CRUD stuff is weak for sure. (i.e. add_quote, update_quote, delete_quote, get_quote_id)
For example, currently you can only get the quote id from the exact quote
In the future, I may just expose the DBI::Class object directly to those that need/want it.
currently, I am not encapsulating the record data structure used by LoadDB->write. (i.e. it's a typical perl5 ojbect, the blessed hash)
I will for sure be encapsulating all data in a future version. (so, don't have code that does $self->{record}->{name} = 'value', or you won't be happy down the road). Instead use $self->get_record('name') (getter) or $self->set_record(name => 'my attrib') (setter)
When we are using a SQLite database backend ('regular' local usage), we should probably be using, ORLite instead of Class::DBI (although we have not seen any issues yet).
Please report any bugs or feature requests to bug-acme-quotedb at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ACME-QuoteDB. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc ACME::QuoteDB
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
The construction of this module was guided by:
Perl Best Practices - Conway
Test Driven Development
Object Oriented Programming
Gnu
vim
Debian Linux
Mac OSX
The collective wisdom and code of The CPAN
this module was created with module-starter
module-starter --module=ACME::QuoteDB \ --author="David Wright" --mb --email=david_v_wright@yahoo.com
ERRATA
Q: Why did you put it in the ACME namespace?
A: Seemed appropriate. I emailed modules@cpan.org and didn't get a
different reaction.
Q: Why did you write this?
A: At a past company, a team I worked on a project with had a test suite,
in which at the completion of successful tests (100%), a 'wisenheimer'
success message would be printed. (Like a quote or joke or the like)
(Interestingly, it added a 'fun' factor to testing, not that one is needed
of course ;). It was hard to justify spending company time to find and
add decent content to the hand rolled process, this would have helped.
Q: Don't you have anything better to do, like some non-trivial work?
A: Yup
Q: Hey Dood! why are u uzing Class::DBI as your ORM!? Haven't your heard
of L<DBIx::Class>?
A: Yup, and I'm aware of 'the new hotness' L<Rose::DB>. If you use this
module and are unhappy with the ORM, feel free to change it.
So far L<Class::DBI> is working for my needs.
FOOTNOTES
- fortune
-
unix application in 'games' (FreeBSD) type 'man fortune' from the command line
- copyright infringement
-
http://www.avvo.com/legal-answers/is-it-copyright-trademark-infringement-to-operate--72508.html
- wikiquote
-
interesting reading, wikiquote fair use doc: http://en.wikiquote.org/wiki/Wikiquote:Copyrights
LICENSE AND COPYRIGHT
Copyright 2009 David Wright, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 740:
Expected text after =item, not a bullet