Rest api with ruby » History » Version 3
J Doe, 2010-08-21 02:02
added single quotes o the custom field ID 2 -> '2'
| 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 | 2 | Jean-Philippe Lang | issue = Issue.new( |
| 30 | :subject => 'REST API', |
||
| 31 | :assigned_to_id => 1, |
||
| 32 | :project_id => 1, |
||
| 33 | 3 | J Doe | :custom_field_values => {'2' => 'Fixed'} |
| 34 | 2 | Jean-Philippe Lang | ) |
| 35 | 1 | Jean-Philippe Lang | if issue.save |
| 36 | puts issue.id |
||
| 37 | else |
||
| 38 | puts issue.errors.full_messages |
||
| 39 | end |
||
| 40 | |||
| 41 | # Updating an issue |
||
| 42 | issue = Issue.find(1) |
||
| 43 | issue.subject = 'REST API' |
||
| 44 | issue.save |
||
| 45 | |||
| 46 | # Deleting an issue |
||
| 47 | issue = Issue.find(1) |
||
| 48 | issue.destroy |
||
| 49 | </code> |
||
| 50 | </pre> |