Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Working handrolled Shurly web service module

  • Loading branch information...
commit a6f6d7b5a25169c526accef2c634c7f8e12b25ba 1 parent 978a476
Jeff Robbins jjeff authored
1  shurly.info
View
@@ -1,5 +1,6 @@
; $Id$
name = ShURLy
+package = ShURLy
description = A complete short URL service.
dependencies[] = views
core = 6.x
5 shurly.module
View
@@ -311,6 +311,7 @@ function shurly_dec2any($num, $base=62, $index=FALSE) {
function shurly_shorten($long_url, $custom = NULL, $account = NULL) {
$success = FALSE;
$account = ($account) ? $account : $GLOBALS['user'];
+ $error = '';
if (!shurly_validate_long($long_url)) {
$error = t('Invalid long URL.');
}
@@ -340,8 +341,8 @@ function shurly_shorten($long_url, $custom = NULL, $account = NULL) {
return array(
'success' => $success,
'error' => $error,
- 'long_url' => $long_url,
- 'short_url' => $short,
+ 'longUrl' => $long_url,
+ 'shortUrl' => $short,
);
}
187 shurly_service/shurly_service.inc
View
@@ -6,139 +6,86 @@
*/
/**
- * Return search results for nodes.
- *
- * @param $keys
- * The keywords to search for within the nodes.
- * @param $simple
- * When set to TRUE, only the fields indicated in $stdkeys will be returned.
- * This can be helpful to limit the size of the search results.
- * @param $fields
- * An array of the node properties that should be returned. When $simple
- * is not set, a full node object is returned with each result. You can
- * limit the properties of these objects to only the ones you need by
- * specifying them in this array. Again, this gives the opportunity to
- * limit your result set.
- *
- * @return
- * An array of search results. If $simple is TRUE, this array will contain
- * only results and no node objects. If $simple is FALSE the array will
- * contain both results and full node objects, possibly limited by the
- * properties indicated in $fields.
- *
- * @see hook_search()
- * @see node_search()
+ * Callback for shurly/api/shorten
*/
-function shurly_service_nodes($keys, $simple = FALSE, $fields = array()) {
- // define standard keys for simple set
- $stdkeys = array('node', 'link', 'type', 'title', 'user', 'date', 'snippet');
+function shurly_service_shorten() {
+ $defaults = array(
+ 'format' => 'json',
+ // 'domain' => NULL,
+ 'longUrl' => '',
+ // 'short' => '', // intentionally not implemented for now...
+ // 'login' => NULL,
+ // 'apiKey' => NULL,
+ 'func' => 'urlData', // function name for padded JSON
+ );
+ $input = $_REQUEST + $defaults;
+
+ $data = shurly_shorten($input['longUrl']);
+
+ shurly_service_output($data, $input);
+
+}
- // invoke the search hook to generate results
- $results = module_invoke('node', 'search', 'search', $keys);
- if ($results && is_array($results) && count($results)) {
- // if simple results requested, remove extra data
- if ($simple) {
- $num = count($results);
- for ($i = 0; $i<$num; $i++) {
- $keys = array_keys($results[$i]);
- foreach ($keys as $key) {
- if ($key == 'node') {
- $results[$i][$key] = $results[$i]['node']->nid;
- }
- if (!in_array($key, $stdkeys)) {
- unset($results[$i][$key]);
- }
- }
- }
- }
- else {
- foreach ($results as $key => $result) {
- $result['node'] = services_node_load($result['node'], $fields);
- $results[$key] = $result;
- }
- }
- return $results;
+/**
+ * Output data in a given format
+ */
+function shurly_service_output($data, $input) {
+ $format = $input['format'] ? $input['format'] : 'json';
+ if (function_exists('shurly_service_output_' . $format)) {
+ call_user_func('shurly_service_output_' . $format, $data, $input);
+ }
+ else {
+ print t('invalid format request');
}
- return services_error(t('Search returned no results.'), 404);
}
/**
- * Return site content specified by a system setting.
- *
- * This service returns search results across the site, as specified by the
- * user at admin/settings/shurly_service. This can combine results from any
- * module that implements hook_search().
- *
- * @param $keys
- * The keywords to search for within the content.
- * @param $simple
- * When set to TRUE, only the fields indicated in $stdkeys will be returned.
- * This can be helpful to limit the size of the search results.
- *
- * @return
- * An array of search results. If $simple is TRUE, this array will contain
- * only results and no content objects. If $simple is FALSE the array will
- * contain both results and full content objects.
- *
- * @see hook_search()
+ * Output JSON data
*/
-function shurly_service_content($keys, $simple = FALSE) {
- // define standard keys for simple set
- $stdkeys = array('link', 'type', 'title', 'user', 'date', 'snippet');
-
- // invoke the search hook to generate results
- $results = array();
- $shurly_hooks = variable_get('shurly_service_options', array('node' => 'node'));
-
- watchdog('shurly_service', t('search.content invoked for !keys using hooks !hooks', array('!keys' => $keys, '!hooks' => implode(', ', $shurly_hooks))));
+function shurly_service_output_json($data) {
+ drupal_json($data);
+}
- // run through only select hook_search() as defined in /admin/settings/shurly_service
- foreach ($shurly_hooks as $hook) {
- if (! empty($hook)) {
- $shurly_results = module_invoke($hook, 'search', 'search', $keys);
- if (! empty($shurly_results) ) {
- $results = array_merge($results, $shurly_results);
- }
- }
- }
+/**
+ * Output JSON data
+ */
+function shurly_service_output_jsonp($data, $input) {
+ $func = $input['func'] ? $input['func'] : 'urlData';
+ // send the header
+ drupal_json();
+ print $func . '(' . drupal_to_js($data) . ');';
+}
- watchdog('shurly_service', t('search.content returned !count results for !keys using hooks !hooks', array('!count' => count($results), '!keys' => $keys, '!hooks' => implode($shurly_hooks, ', '))));
+/**
+ * Output txt data
+ * Just outputs the shortUrl attribute... won't show errors
+ */
+function shurly_service_output_txt($data) {
+ print $data['shortUrl'];
+}
- if ($results and is_array($results) and count($results)) {
- // if simple results requested, remove extra data
- if ($simple) {
- $num = count($results);
- for ($i = 0; $i<$num; $i++) {
- $keys = array_keys($results[$i]);
- foreach ($keys as $key) {
- if (!in_array($key, $stdkeys)) {
- unset($results[$i][$key]);
- }
- }
- }
- }
- return $results;
- }
- return services_error(t('Search returned no results.'), 401);
+/**
+ * Output PHP serialized data
+ */
+function shurly_service_output_php($data) {
+ print serialize($data);
}
/**
- * Return search results for users.
- *
- * @param $keys
- * The keywords to search for within the users.
- *
- * @return
- * An array of search results.
- *
- * @see hook_search()
- * @see user_search()
+ * Output xml data
*/
-function shurly_service_users($keys) {
- // invoke the search hook to generate results
- $results = module_invoke('user', 'search', 'search', $keys);
- if ($results && is_array($results) && count($results)) {
- return $results;
+function shurly_service_output_xml($data) {
+ $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><data />");
+ if (is_array($data)) {
+ foreach($data as $key => $value) {
+ $value = htmlentities($value);
+ $xml->addChild($key, $value);
+ }
}
- return services_error(t('Search returned no results.'), 401);
+ $output = $xml->asXML();
+ header('Connection: close');
+ header('Content-Length: '. strlen($output));
+ header('Content-Type: text/xml');
+ header('Date: '. date('r'));
+ print $output;
}
3  shurly_service/shurly_service.info
View
@@ -1,8 +1,7 @@
; $Id$
name = ShURLy Service
description = Provides web services for the ShURLy module.
-package = Services - services
-dependencies[] = services
+package = ShURLy
dependencies[] = shurly
core = 6.x
5 shurly_service/shurly_service.module
View
@@ -18,16 +18,17 @@ function shurly_service_perm() {
* Implementation of hook_menu().
*/
-function search_service_menu() {
+function shurly_service_menu() {
$items = array();
$items['shurly/api/shorten'] = array(
'title' => t('Search Service'),
'description' => t('Configure search service.'),
'file' => 'shurly_service.inc',
- 'page callback' => 'drupal_get_form',
+ 'page callback' => 'shurly_service_shorten',
'access arguments' => array('Create short URLs'),
'type' => MENU_CALLBACK,
);
return $items;
}
+
Please sign in to comment.
Something went wrong with that request. Please try again.