{"id":148,"date":"2010-01-25T20:57:46","date_gmt":"2010-01-26T01:57:46","guid":{"rendered":"http:\/\/somethingdoug.com\/thoughts\/?p=148"},"modified":"2010-01-25T20:57:46","modified_gmt":"2010-01-26T01:57:46","slug":"www-usf-directory-published","status":"publish","type":"post","link":"https:\/\/somethingdoug.com\/thoughts\/2010\/01\/25\/www-usf-directory-published\/","title":{"rendered":"WWW::USF::Directory Published"},"content":{"rendered":"<p>I have published my interface to the <a href=\"http:\/\/directory.acomp.usf.edu\/\">University of South Florida online directory<\/a> to the CPAN as <a href=\"http:\/\/search.cpan.org\/perldoc?WWW::USF::Directory\">WWW::USF::Directory<\/a>. This is a start to my USF-oriented modules to go up on the CPAN for public use. This module allows you to interact with the USF online directory. It can be used to make a simple directory repeater like so:<\/p>\n<pre lang=\"perl\">#!\/usr\/bin\/perl\r\n\r\npackage Directory;\r\n\r\nuse 5.008;\r\nuse strict;\r\nuse warnings 'all';\r\n\r\nuse base 'CGI::Application';\r\n\r\nuse JSON 2.00; # The API was changed\r\nuse Try::Tiny;\r\nuse WWW::USF::Directory;\r\n\r\nsub setup {\r\n\tmy ($self) = @_;\r\n\r\n\t# Create a directory object for use later\r\n\t$self->{directory} = WWW::USF::Directory->new(\r\n\t\tinclude_faculty  => 1,\r\n\t\tinclude_staff    => 1,\r\n\t\tinclude_students => 0,\r\n\t);\r\n\r\n\t# Start in search mode, which is the only mode\r\n\t$self->start_mode('search');\r\n\t$self->run_modes(search => 'search');\r\n}\r\n\r\nsub search {\r\n\tmy ($self) = @_;\r\n\r\n\t# Hold the results and response\r\n\tmy (@results, $response);\r\n\r\n\t# Set the header to specity JSON\r\n\t$self->header_add(-type => 'application\/json');\r\n\r\n\ttry {\r\n\t\t# Search the directory\r\n\t\t@results = $self->{directory}->search(\r\n\t\t\tname => scalar $self->query->param('name'),\r\n\t\t);\r\n\r\n\t\tforeach my $result (@results) {\r\n\t\t\t# Change the ::Directory::Entry object into a hash of its attributes\r\n\t\t\t$result = _moose_object_as_hash($result);\r\n\r\n\t\t\tif (exists $result->{affiliations}) {\r\n\t\t\t\t$result->{affiliations} = [map {\r\n\t\t\t\t\t_moose_object_as_hash($_);\r\n\t\t\t\t} @{$result->{affiliations}}];\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t# Return the JSON-encoded results to print\r\n\t\t$response = JSON->new->encode({\r\n\t\t\tresults => \\@results,\r\n\t\t});\r\n\t}\r\n\tcatch {\r\n\t\t# Get the error\r\n\t\tmy $error = $_;\r\n\r\n\t\t# Return a JSON with the error\r\n\t\t$response = JSON->new->encode({\r\n\t\t\terror   => \"$error\",\r\n\t\t\tresults => [],\r\n\t\t});\r\n\t};\r\n\r\n\t# Return the response\r\n\treturn $response;\r\n}\r\n\r\nsub _moose_object_as_hash {\r\n\tmy ($object) = @_;\r\n\r\n\t# Convert a Moose object to a HASH with the attribute_name => attribute_value\r\n\tmy $hash = { map {\r\n\t\t($_->name, $_->get_value($object))\r\n\t} $object->meta->get_all_attributes };\r\n\r\n\treturn $hash;\r\n}\r\n\r\n1;\r\n\r\npackage main;\r\n\r\nuse 5.008;\r\nuse strict;\r\nuse warnings 'all';\r\n\r\n# Start and tun the application\r\nDirectory->new->run();<\/pre>\n<p>This will actually be added as an example in the next release. You can run this as a CGI and use the <code>name<\/code> param to pass what to search for, or run it from the command line with <code>name=\"Name\"<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have published my interface to the University of South Florida online directory to the CPAN as WWW::USF::Directory. This is a start to my USF-oriented modules to go up on the CPAN for public use. This module allows you to interact with the USF online directory. It can be used to make a simple directory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9],"tags":[27,19,22],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-howto","category-perl","tag-perl","tag-perl5","tag-usf"],"_links":{"self":[{"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":4,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/posts\/148\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}