Project

General

Profile

Rest api with ruby » History » Version 8

Toshi MARUYAMA, 2016-04-25 14:23
commnet out "issue.destroy" because this sample id is hard-coded "1"

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 4 Eric Davis
h2. ActiveResource
6
7 1 Jean-Philippe Lang
Here is a simple ruby script that demonstrates how to use the Redmine REST API:
8
9
<pre>
10
<code class="ruby">
11
require 'rubygems'
12
require 'active_resource'
13
14
# Issue model on the client side
15
class Issue < ActiveResource::Base
16
  self.site = 'http://redmine.server/'
17
  self.user = 'foo'
18
  self.password = 'bar'
19
end
20
21
# Retrieving issues
22
issues = Issue.find(:all)
23
puts issues.first.subject
24
25
# Retrieving an issue
26
issue = Issue.find(1)
27
puts issue.description
28
puts issue.author.name
29
30
# Creating an issue
31 2 Jean-Philippe Lang
issue = Issue.new(
32
  :subject => 'REST API',
33
  :assigned_to_id => 1,
34 7 Denis Savitskiy
  :project_id => 1
35 3 J Doe
)
36 7 Denis Savitskiy
issue.custom_field_values = {'2' => 'Fixed'}
37 1 Jean-Philippe Lang
if issue.save
38
  puts issue.id
39
else
40
  puts issue.errors.full_messages
41
end
42
43
# Updating an issue
44
issue = Issue.find(1)
45
issue.subject = 'REST API'
46
issue.save
47
48
# Deleting an issue
49
issue = Issue.find(1)
50 8 Toshi MARUYAMA
#issue.destroy
51 1 Jean-Philippe Lang
</code>
52
</pre>
53 6 Geoffroy Planquart
54
_You may need to set @include_root_in_json = true@ in your ActiveResource class_