173 | | |
| 173 | |
| 174 | === Using from Ruby === |
| 175 | You can either use the XMLRPC functionality included in the [http://ruby-doc.org/stdlib/libdoc/xmlrpc/rdoc/index.html Ruby Standard Library] or [/attachment/wiki/XmlRpcPlugin/trac4r.tar.gz download the trac4r library] which does all the trivial stuff for you. |
| 176 | |
| 177 | This example uses trac4r: |
| 178 | {{{ |
| 179 | #!ruby |
| 180 | require 'trac4r/trac' |
| 181 | # initialize the connection (username and password can be ommitted if not needed, but most of the time you will need them if anonymous doesn't have XMLRPC permissions) |
| 182 | trac = Trac.new "https://dev.example.com/trac/my_awesome_project", "myusername", "mypassword" |
| 183 | # get a list of all tickets (as an array of numbers) |
| 184 | trac.tickets.list :include_closed => true # this is the default anyway |
| 185 | # get all the tickets |
| 186 | # NOTE: the results here are cached, so you can call it as often as you want without producing traffic more than once. |
| 187 | # use ":cached_results => false" to get the latest version |
| 188 | trac.tickets.get_all :include_closed => true |
| 189 | # get a single ticket |
| 190 | ticket = trac.tickets.get 5 |
| 191 | # print the data |
| 192 | puts "Title: #{ticket.summary}" |
| 193 | puts "Description: #{ticket.description}" |
| 194 | puts "Milestone: #{ticket.milestone}" |
| 195 | puts "Status: #{ticket.status}" |
| 196 | puts "Type: #{ticket.type}" |
| 197 | puts "Priority: #{ticket.priority}" |
| 198 | # get a list of all wiki pages |
| 199 | trac.wiki.list |
| 200 | # download one page |
| 201 | trac.wiki.get_html "SomeRandomPageName" # HTML version |
| 202 | trac.wiki.get_raw "AnotherRandomPageName" # trac syntax version (e.g. for editing) |
| 203 | # for previews use |
| 204 | trac.wiki.raw_to_html "content of a page in [wiki:WikiFormatting Trac syntax] as a ''String''" |
| 205 | # to post a page use |
| 206 | trac.wiki.put "NameOfThePage", "content in Trac syntax" |
| 207 | # list the attachments of a wiki page |
| 208 | trac.wiki.attachments "NameOfThePage" |
| 209 | # save an attachment |
| 210 | File.new("my_cool_document","w") do |f| |
| 211 | f.write trac.wiki.get_attachment "NameOfThePage", "my_cool_document.pdf" |
| 212 | end |
| 213 | }}} |
| 214 | |
| 215 | Also see the included Documentation in trac4r/doc |
| 216 | |
| 217 | If you need to do a custom query do |
| 218 | {{{ |
| 219 | #!ruby |
| 220 | trac.query("system.getAPIVersion") |
| 221 | }}} |
| 222 | The first argument is the method name, all other arguments are directly passed to XMLRPC. |
| 223 | For this example of cause you could do `trac.api_version` instead ;) |
| 224 | |
| 225 | If you have any problems with trac4r you can email me: niklas.cathor (eat) gmail dot com |
| 226 | |