Project

General

Profile

Rest api with ruby » History » Version 9

Marcin Garski, 2016-04-25 14:37
#22628

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 9 Marcin Garski
# custom field with id=2 exist in database
36
  :custom_fields => [{id: 2, value: "IT"}]
37 7 Denis Savitskiy
)
38 1 Jean-Philippe Lang
if issue.save
39
  puts issue.id
40
else
41
  puts issue.errors.full_messages
42
end
43 9 Marcin Garski
44 1 Jean-Philippe Lang
45
# Updating an issue
46
issue = Issue.find(1)
47
issue.subject = 'REST API'
48
issue.save
49
50
# Deleting an issue
51
issue = Issue.find(1)
52 8 Toshi MARUYAMA
#issue.destroy
53 1 Jean-Philippe Lang
</code>
54
</pre>
55 6 Geoffroy Planquart
56
_You may need to set @include_root_in_json = true@ in your ActiveResource class_