Rest api with ruby » History » Version 1
Jean-Philippe Lang, 2010-01-17 20:47
| 1 | 1 | Jean-Philippe Lang | h1. Using the REST API with Ruby |
|---|---|---|---|
| 2 | |||
| 3 | Redmine REST API follows the Rails's RESTful conventions, so using it with "ActiveResource":http://api.rubyonrails.org/classes/ActiveResource/Base.html is pretty straightforward. |
||
| 4 | |||
| 5 | Here is a simple ruby script that demonstrates how to use the Redmine REST API: |
||
| 6 | |||
| 7 | <pre> |
||
| 8 | <code class="ruby"> |
||
| 9 | require 'rubygems' |
||
| 10 | require 'active_resource' |
||
| 11 | |||
| 12 | # Issue model on the client side |
||
| 13 | class Issue < ActiveResource::Base |
||
| 14 | self.site = 'http://redmine.server/' |
||
| 15 | self.user = 'foo' |
||
| 16 | self.password = 'bar' |
||
| 17 | end |
||
| 18 | |||
| 19 | # Retrieving issues |
||
| 20 | issues = Issue.find(:all) |
||
| 21 | puts issues.first.subject |
||
| 22 | |||
| 23 | # Retrieving an issue |
||
| 24 | issue = Issue.find(1) |
||
| 25 | puts issue.description |
||
| 26 | puts issue.author.name |
||
| 27 | |||
| 28 | # Creating an issue |
||
| 29 | issue = Issue.new(:subject => 'REST API', :assigned_to_id => 1, :project_id => 1) |
||
| 30 | if issue.save |
||
| 31 | puts issue.id |
||
| 32 | else |
||
| 33 | puts issue.errors.full_messages |
||
| 34 | end |
||
| 35 | |||
| 36 | # Updating an issue |
||
| 37 | issue = Issue.find(1) |
||
| 38 | issue.subject = 'REST API' |
||
| 39 | issue.save |
||
| 40 | |||
| 41 | # Deleting an issue |
||
| 42 | issue = Issue.find(1) |
||
| 43 | issue.destroy |
||
| 44 | </code> |
||
| 45 | </pre> |