Project

General

Profile

Rest api with ruby » History » Revision 2

Revision 1 (Jean-Philippe Lang, 2010-01-17 20:47) → Revision 2/15 (Jean-Philippe Lang, 2010-03-12 20:04)

h1. Using the REST API with Ruby 

 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. 

 Here is a simple ruby script that demonstrates how to use the Redmine REST API: 

 <pre> 
 <code class="ruby"> 
 require 'rubygems' 
 require 'active_resource' 

 # Issue model on the client side 
 class Issue < ActiveResource::Base 
   self.site = 'http://redmine.server/' 
   self.user = 'foo' 
   self.password = 'bar' 
 end 

 # Retrieving issues 
 issues = Issue.find(:all) 
 puts issues.first.subject 

 # Retrieving an issue 
 issue = Issue.find(1) 
 puts issue.description 
 puts issue.author.name 

 # Creating an issue 
 issue = Issue.new( 
   :subject Issue.new(:subject => 'REST API', 
   :assigned_to_id => 1, 
   :project_id => 1, 
   :custom_field_values => {2 => 'Fixed'} 
 ) 1) 
 if issue.save 
   puts issue.id 
 else 
   puts issue.errors.full_messages 
 end 

 # Updating an issue 
 issue = Issue.find(1) 
 issue.subject = 'REST API' 
 issue.save 

 # Deleting an issue 
 issue = Issue.find(1) 
 issue.destroy 
 </code> 
 </pre>