Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 11-20 of 22 total

Using a RewriteRule to simplify a ProjectX action

Following on from how to 'Simplify a ProjectX request using projectx-helper.cgi' [dzone.com] this RewriteRule reduces a projectx request to something which is clearly RESTful.

Rather than have the client script perform a request with the projectx-helper URL e.g.:
http://yourwebsite.com/p/projectx-helper.cgi?project=group_dynarex&method=create_file&path=feedex_rss&file=ruby_news

... it was easier to understand the request with a clean URL .

To make the clean URL the following line was added to the virtual hosts' include file:
   1  RewriteRule ^/feedex-rss/create-file/(\w+)$ /p/projectx-helper.cgi?project=group_dynarex&method=create_file&path=feedex_rss&file=$1 [p]


Here is the clean URL used by the client script:

http://yourwebsite.com/feedex-rss/create-file/ruby_news



Creating a clean url for a ProjectX method

Following on from the projectx-helper.cgi [dzone.com] post the following Apache rewrite instruction reduces the url to something more user friendly. In this example the user would enter the URL http://someurl.com/planner/create/date/190808 for a new planner record to be created for the 19th August.

   1  
   2     RewriteEngine on
   3     RewriteRule ^/planner/create/date/(\d+)$ /p/projectx-helper.cgi?project=mdynarex&method=create&file=planner&date=$1 [p]
   4  


Note the destination address is proxied to allow the execution of the script to be permitted.

Simplify a ProjectX request using projectx-helper.cgi

This Ruby CGI script is for use as an alternative to having the client prepare a ProjectX XML request against projectx.cgi.

   1  
   2  #!/usr/bin/ruby
   3  # projectx-helper.cgi
   4  
   5  require 'cgi'
   6  
   7  cgi = CGI.new
   8  
   9  h = cgi.params
  10  
  11  puts "Content-Type: text/xml"
  12  puts
  13  
  14  def pparam(h,var)
  15    val = ''
  16    if not h[var].empty? then
  17      val = h[var]
  18      h.delete(var)
  19    end
  20    val
  21  end
  22  
  23  def format_param(var, val)
  24      "#{' '*8}<param var='#{var}'>#{val}</param>\r"
  25  end
  26  
  27  def pparam_remainder(h)
  28    buffer = ''
  29    h.each do |param|
  30      buffer << format_param(param[0],param[1])
  31    end
  32    buffer
  33  end
  34  
  35  xml_project =<<XML_PROJECT
  36  <project name='#{pparam(h,'project')}'>
  37    <methods>
  38      <method name='#{pparam(h,'method')}'>
  39        <params>
  40  #{pparam_remainder(h)}
  41        </params>
  42      </method>
  43    </methods>
  44  </project>
  45  XML_PROJECT
  46  
  47  puts xml_project


example input url: http://yourwebsite.com/p/projectx-helper.cgi?project=abc&method=create&path=mdynarex&xyz=123
example output:
   1  
   2  <project name='abc'>
   3    <methods>
   4      <method name='create'>
   5        <params>
   6          <param var='xyz'>123</param>
   7          <param var='path'>mdynarex</param>
   8        </params>
   9      </method>
  10    </methods>
  11  </project>


CGI Reference: Programming Ruby: The Pragmatic Programmer's Guide [rubycentral.com]

A simple XHTML submit form for ProjectX

Preparing ProjectX API requests through the browser's address bar can get quite messy, however inputting the request through a simple form makes it much easier to read.

   1  
   2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   3    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   4  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   5    <head>
   6      <title>ProjectX API</title>
   7      <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
   8    </head>
   9    <body>
  10      <h1>ProjectX API form</h1>
  11      <p>Enter the Project API XML to send a request to the server.</p>
  12      <form action="http://rorbuilder.info/api/projectx.cgi" method="post" id="projectx_form">
  13      <fieldset><legend>xml_project</legend><textarea id="xml_project" name="xml_project" cols="104" rows="20"></textarea></fieldset>
  14      <div><button type="submit">Submit</button></div>
  15      </form>
  16    <p>
  17      <a href="http://validator.w3.org/check?uri=referer"><img
  18          src="http://www.w3.org/Icons/valid-xhtml10"
  19          alt="Valid XHTML 1.0 Strict" height="31" width="88" style="float:right;  border:0 "/></a>
  20    </p>
  21    <p style="clear:float">last updated: 13th April 2008</p>
  22    
  23    </body>
  24  </html>


The web page can be seen at http://rorbuilder.info/r/projectx-api/index.html
The following XML request value when submitted should return an XML result containing the results and the method executed.
   1  
   2  <project name='whiteboardqueue'>
   3    <methods>
   4      <method name='get_user_id'>
   5        <params/>
   6      </method>
   7    </methods>
   8  </project>

eg.
   1  
   2  <result method="rtn_get_user_id">
   3    <get_user_id>36539</get_user_id>
   4  </result>

Undo the last shape stored in the whiteboard message buffer

This ECMAScript implements an undo feature for the SVG whiteboard. When the user presses CTRL+Z the last shape in the message_buffer variable is removed.

   1  
   2    function undoLast() {
   3      ipos = getLastMethodPos(message_buffer,-1)
   4      if (ipos > 0)
   5        message_buffer = message_buffer.substring(0, ipos)
   6      else
   7        message_buffer = ''    
   8    }
   9    
  10    function getLastMethodPos(message, pos) {
  11      i = message.indexOf('<method', pos+1)
  12      if (i >= 0) 
  13        result = getLastMethodPos(message, i)
  14      else
  15        result = pos 
  16      return result;
  17    }


Here's an example of the messages (containing shapes) stored in the message_buffer
   1  
   2  "<method name='create'><params><param var='type'>shape</param><param var='body'>
   3  polyline%20x%3D%27524%27%20y%3D%27198%27%20fill%3D%27none%27%20stroke%3D%27red%27%20id%3D
   4  %27348553%27%20stroke-width%3D%272%27%20points%3D%27524%2C198%20523%2C198%20522%2C198%20521%2C198%20
   5  513%2C198%20511%2C198%20503%2C198%20495%2C200%20493%2C200%20485%2C204%20483%2C205%20475%2C207%20474%
   6  2C208%20470%2C212%20466%2C216%20465%2C217%20464%2C219%20463%2C220%20463%2C222%20463%2C223%20463%2C22
   7  5%20463%2C226%20463%2C228%20464%2C230%20465%2C231%20466%2C233%20467%2C234%20471%2C238%20472%2C238%20
   8  478%2C242%20484%2C246%20485%2C247%20488%2C247%20489%2C248%20491%2C248%20493%2C249%20496%2C249%20497%
   9  2C249%20505%2C249%20515%2C249%20521%2C247%20531%2C245%20533%2C245%20539%2C243%20547%2C243%20548%2C24
  10  3%20550%2C242%20555%2C237%20556%2C237%20557%2C235%20558%2C234%20558%2C233%20559%2C231%20559%2C230%20
  11  560%2C228%20560%2C226%20560%2C222%20559%2C221%20559%2C220%20559%2C219%20559%2C217%20559%2C216%20558%
  12  2C215%20558%2C214%20556%2C208%20555%2C206%20555%2C205%20553%2C199%20549%2C191%20547%2C185%20546%2C18
  13  4%20544%2C176%20540%2C170%20539%2C168%20535%2C162%20534%2C161%20528%2C155%20528%2C154%20524%2C148%20
  14  518%2C144%20517%2C143%20511%2C139%20509%2C138%20501%2C134%20499%2C133%20491%2C129%20489%2C129%20479%
  15  2C129%20469%2C129%20467%2C129%20459%2C129%20457%2C129%20454%2C129%20452%2C129%20446%2C131%20438%2C13
  16  1%20437%2C132%20435%2C132%20435%2C133%20434%2C133%20434%2C134%20433%2C134%20433%2C135%27%23%3A
  17  </param><param var='sender'>34855</param></params></method><method name='create'><params><param 
  18  var='type'>shape</param><param var='body'>polyline%20x%3D%27457%27%20y%3D%27175%27%20fill%3D%27none
  19  %27%20stroke%3D%27red%27%20id%3D
  20  %27348554%27%20stroke-width%3D%272%27%20points%3D%27457%2C175%20456%2C175%20455%2C176%20454%2C176%20
  21  453%2C177%20452%2C177%20451%2C177%20449%2C178%20448%2C179%20442%2C181%20440%2C183%20438%2C184%20438%
  22  2C185%20437%2C186%20436%2C187%20435%2C189%20435%2C191%20434%2C193%20434%2C194%20433%2C195%20433%2C19
  23  6%20431%2C200%20431%2C201%20431%2C203%20431%2C205%20431%2C206%20431%2C207%20431%2C208%20431%2C209%20
  24  431%2C210%20431%2C211%20431%2C212%20431%2C213%20431%2C214%20431%2C215%20431%2C216%20431%2C217%20432%
  25  2C223%20432%2C224%20432%2C225%20432%2C226%20433%2C226%20433%2C227%20433%2C228%20433%2C229%20434%2C22
  26  9%20434%2C230%20435%2C231%20435%2C233%20435%2C234%20436%2C235%20437%2C236%20437%2C237%20438%2C237%20
  27  438%2C238%20438%2C239%20438%2C240%20439%2C240%20439%2C241%20443%2C245%20444%2C246%20444%2C247%20445%
  28  2C247%20446%2C248%20447%2C248%20448%2C248%20448%2C249%20449%2C249%20450%2C250%20451%2C251%20452%2C25
  29  2%20454%2C253%20455%2C253%20461%2C255%20462%2C255%20464%2C256%20466%2C256%20467%2C257%20469%2C257%20
  30  469%2C258%20471%2C259%20472%2C259%20474%2C260%20475%2C260%20477%2C261%20478%2C261%20480%2C262%20481%
  31  2C262%20481%2C263%20482%2C263%20483%2C263%20484%2C263%20485%2C263%20486%2C263%20489%2C263%20491%2C26
  32  3%20492%2C263%20495%2C263%20496%2C263%20502%2C261%20504%2C261%20507%2C261%20509%2C261%20510%2C261%20
  33  511%2C261%20512%2C261%20513%2C261%20515%2C261%20517%2C261%20518%2C261%20520%2C260%20523%2C260%20524%
  34  2C260%20526%2C259%20528%2C259%20529%2C259%20534%2C258%20535%2C258%20536%2C257%20537%2C257%20538%2C25
  35  7%20540%2C257%27%23%3A</param><param var='sender'>34855</param></params></method>"


In the above example there are 2 methods calls ready to be sent to the server, the 2nd method would be removed after the undoLast() function had executed.

The methods are ready to be passed to the function getLatestMessages which passes the request through the ProjectX API, which then returns the results.

This code only removes the shape before it's sent to the server, I still need to write the code to delete a shape which is in the server whiteboardqueue, and remove the shape element from the web browser display (SVG DOM).

Deleting a redundant shape message from the whiteboard queue

This code is used to remove a message from the whiteboard queue containing a shape which has recently moved it's position (x and y coordinates) on the board. Code extract from whiteboardqueue.rb

   1  
   2    def call_delete_shape(params)
   3      #get the shape's message id.
   4      doc = Document.new(params)
   5      shape_id = doc.root.elements["param[@var='id']"].text.to_s
   6      initialize_doc
   7  
   8      doc_xml = Document.new(decode2(@doc_file.root.to_s))
   9      id = doc_xml.root.elements["records/message/body/*[@id='#{shape_id}']"].parent.parent.attributes.get_attribute('id')
  10      delete_record(id)
  11      save_file
  12    end


*update 10:04pm*
Added the link to the project code http://github.com/jrobertson/whiteboardqueue/tree

Instruct a shared whiteboard to save and refresh

The following code used with the ProjectX API informs the client web browser that the whiteboard will be refreshed in 5 seconds. It then archives the current whiteboard information, formats it, and sends a message to each web browser to refresh their view.

   1  
   2  <project name="whiteboardqueue">
   3    <methods>
   4      <method name="create">
   5        <params>
   6          <param var="type">ecmascript</param>
   7          <param var="body">startRefresh(5)</param>
   8          <param var="sender">system</param>
   9        </params>
  10      </method>
  11      <method name="timer">
  12        <params>
  13          <param var="timer">5</param>
  14        </params>
  15      </method>
  16      <method name="archive_and_format">
  17        <params/>
  18      </method>
  19      <method name="create">
  20        <params>
  21          <param var="type">ecmascript</param>
  22          <param var="body">reloadDocument()</param>
  23          <param var="sender">system</param>
  24        </params>
  25      </method>
  26    </methods>
  27  </project>


*update 1:14am*
The whiteboard demo [rorbuilder.info] allows the user to draw using the mouse within the web browser which renders SVG. Tested on Flock and Firefox.

*update 4:42pm 28 Mar 08*
You can also view the whiteboard message queue [rorbuilder.info].

*update 6:29pm Mar 08*
I've created a short url (http://rubyurl.com/vxHD) (to demonstrate the cleaning of the whiteboard) which redirects to this http://rorbuilder.info/api/projectx.cgi?xml_project=<project name="whiteboardqueue"><methods><method name="create"><params><param var="type">ecmascript</param><param var="body">startRefresh(5)</param><param var="sender">system</param></params></method><method name="timer"><params><param var="timer">5</param></params></method><method name="archive_and_format"><params/></method><method name="create"><params><param var="type">ecmascript</param><param var="body">reloadDocument()</param><param var="sender">system</param></params></method></methods></project>

I've

Adding new files to your github repository

Here is a set of instructions to apply new files to my github repository which is called projectx.

Before you start make sure you don't already have the repository name listed as a directory in the local current directory.

1) copy the remote repository to your local machine
syntax: git clone [uri] # eg.
   1  git clone git@github.com:jrobertson/projectx.git

1.5) cd into the newly created local repository eg.
   1  cd projectx

2) copy the local file to the local repository directory
eg.
   1  cp ../projectx2/feed.rb .

3) add the local files to the local repository
syntax: git add [file] # eg.
   1  git add feed.rb

4) Inform the git system that you have completed the required changes for this session.
   1  git commit -a # add a message associated with this file revision

5) copy the new local repository files back to the remote repository.
   1  git push # updates the changes back to the server

Note: The text with the square-brackets should be replaced with your own values.

Reference: A tour of git: the basics [cworth.org]

Twitter and Jaiku from the command line

The following instructions make it easy to post to Twitter and Jaiku from the command line. The instructions were copied from the article "Ubuntu Unleashed: Howto Twitter From the Command Line in Ubuntu!" [ubuntu-unleashed.com] and modified to post via Rorbuilder's ProjectX API.

   1  sudo apt-get install curl

   1  sudo gedit /usr/bin/jaitwit

Now Paste this in gEdit and simply replace "YourUsername" with your username and "YourPassword" with your twitter passwd, then replace the Jaiku variables (YourUsername, YourPassword, YourCity, YourAccessKey) and ctrl-s to save, then alt-F4 to exit!
   1  
   2  curl http://rorbuilder.info/api/projectx.cgi?xml_project=%3Cproject%20name=%22micro_blog%22%3E%3Cmethods%3E%3Cmethod%20name=%22post2jaiku%22%3E%3Cparams%3E%3Cparam%20var=%22user%22%20val=%22YourUsername%22/%3E%3Cparam%20var=%22msg%22%20val=%22`echo $@|tr ' ' '+'`%22/%3E%3Cparam%20var=%22location%22%20val=%22YourCity%22/%3E%3Cparam%20var=%22apikey%22%20val=%22YourAccessKey%22/%3E%3C/params%3E%3C/method%3E%3Cmethod%20name=%22post2twitter%22%3E%3Cparams%3E%3Cparam%20var=%22user%22%20val=%22YourUsername%22/%3E%3Cparam%20var=%22msg%22%20val=%22`echo $@|tr ' ' '+'`%22/%3E%3Cparam%20var=%22password%22%20val=%22YourPassword%22/%3E%3C/params%3E%3C/method%3E%3C/methods%3E%3C/project%3E -o /dev/null
   3  echo Message Sent!

Then chmod for exec privileges:
   1  chmod +x /usr/bin/jaitwit

Then from the CLI type jaitwit followed by your message.
   1  jaitwit "message here without the quotes"

Post to Jaiku using PHP

This example uses the ProjectX API to post to Jaiku.com
   1  
   2  <?php
   3    $msg = 'this is just a test message using the ProjectX API for posting to Jaiku';
   4  
   5    $xml_result =  simplexml_load_file('http://rorbuilder.info/api/projectx.cgi?xml_project=<project name="jaiku"><methods><method name="post"><params><param var="user" val="jrobertson"/><param var="msg" val="' . $msg . '"/><param var="location" val="London"/><param var="apikey" val="9ee6ffd165r364492"/></params></method></methods></project>');
   6    $method_result = $xml_result->post2jaiku;
   7    echo 'result' . $method_result;
   8  ?>


Reference: SimpleXML processing with PHP [ibm.com]
« Newer Snippets
Older Snippets »
Showing 11-20 of 22 total