Project

General

Profile

Rest api with ruby » History » Version 11

Toshi MARUYAMA, 2016-04-25 15:31
pass "Issue.find(:all)"

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 10 Toshi MARUYAMA
On Redmine 3.x (Rails 4.2), you need to add 'activeresource' gem.
8
For example, at Gemfile.local:
9
<pre><code class="ruby">
10
gem 'activeresource'
11
</code></pre>
12
13 1 Jean-Philippe Lang
Here is a simple ruby script that demonstrates how to use the Redmine REST API:
14
15
<pre>
16
<code class="ruby">
17
require 'rubygems'
18
require 'active_resource'
19
20
# Issue model on the client side
21
class Issue < ActiveResource::Base
22
  self.site = 'http://redmine.server/'
23
  self.user = 'foo'
24
  self.password = 'bar'
25
end
26
27 11 Toshi MARUYAMA
if false
28
  # Retrieving issues
29
  issues = Issue.find(:all)
30
  puts issues.first.subject
31
end
32 1 Jean-Philippe Lang
33
# Retrieving an issue
34
issue = Issue.find(1)
35
puts issue.description
36
puts issue.author.name
37
38
# Creating an issue
39 2 Jean-Philippe Lang
issue = Issue.new(
40
  :subject => 'REST API',
41
  :assigned_to_id => 1,
42 7 Denis Savitskiy
  :project_id => 1
43 9 Marcin Garski
# custom field with id=2 exist in database
44
  :custom_fields => [{id: 2, value: "IT"}]
45 7 Denis Savitskiy
)
46 1 Jean-Philippe Lang
if issue.save
47
  puts issue.id
48
else
49
  puts issue.errors.full_messages
50
end
51 9 Marcin Garski
52 1 Jean-Philippe Lang
53
# Updating an issue
54
issue = Issue.find(1)
55
issue.subject = 'REST API'
56
issue.save
57
58
# Deleting an issue
59
issue = Issue.find(1)
60 8 Toshi MARUYAMA
#issue.destroy
61 1 Jean-Philippe Lang
</code>
62
</pre>
63 6 Geoffroy Planquart
64
_You may need to set @include_root_in_json = true@ in your ActiveResource class_