Project

General

Profile

Rest api with ruby » History » Version 12

David Lukas Müller, 2017-06-13 16:44
added example for pure Ruby (without Rails)

1 1 Jean-Philippe Lang
h1. Using the REST API with Ruby
2
3 12 David Lukas Müller
{{>toc}}
4
5 1 Jean-Philippe Lang
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.
6
7 12 David Lukas Müller
h2. ActiveResource (Rails)
8 4 Eric Davis
9 10 Toshi MARUYAMA
On Redmine 3.x (Rails 4.2), you need to add 'activeresource' gem.
10
For example, at Gemfile.local:
11
<pre><code class="ruby">
12
gem 'activeresource'
13
</code></pre>
14
15 1 Jean-Philippe Lang
Here is a simple ruby script that demonstrates how to use the Redmine REST API:
16
17
<pre>
18
<code class="ruby">
19
require 'rubygems'
20
require 'active_resource'
21
22
# Issue model on the client side
23
class Issue < ActiveResource::Base
24
  self.site = 'http://redmine.server/'
25
  self.user = 'foo'
26
  self.password = 'bar'
27
end
28
29 11 Toshi MARUYAMA
if false
30
  # Retrieving issues
31
  issues = Issue.find(:all)
32
  puts issues.first.subject
33
end
34 1 Jean-Philippe Lang
35
# Retrieving an issue
36
issue = Issue.find(1)
37
puts issue.description
38
puts issue.author.name
39
40
# Creating an issue
41 2 Jean-Philippe Lang
issue = Issue.new(
42
  :subject => 'REST API',
43
  :assigned_to_id => 1,
44 7 Denis Savitskiy
  :project_id => 1
45 9 Marcin Garski
# custom field with id=2 exist in database
46
  :custom_fields => [{id: 2, value: "IT"}]
47 7 Denis Savitskiy
)
48 1 Jean-Philippe Lang
if issue.save
49
  puts issue.id
50
else
51
  puts issue.errors.full_messages
52
end
53 9 Marcin Garski
54 1 Jean-Philippe Lang
55
# Updating an issue
56
issue = Issue.find(1)
57
issue.subject = 'REST API'
58
issue.save
59
60
# Deleting an issue
61
issue = Issue.find(1)
62 8 Toshi MARUYAMA
#issue.destroy
63 1 Jean-Philippe Lang
</code>
64
</pre>
65 6 Geoffroy Planquart
66 1 Jean-Philippe Lang
_You may need to set @include_root_in_json = true@ in your ActiveResource class_
67 12 David Lukas Müller
68
69
70
h2. Pure Ruby (without Rails)
71
72
Here is an example to set the status of Issue #9599 to internal status_id 1 with a @net/http@ PUT-request:
73
74
<pre><code class="ruby">
75
require 'net/https'
76
require 'uri'
77
require 'json'
78
79
def update_issue_status issue_id, new_status_id, change_note
80
  base_url = "https://your.redmine.example.com"
81
  api_token = "xxxxxxxxxxxxxxx"
82
  
83
  payload = {
84
    issue: {
85
      notes: change_note,
86
      status_id: new_status_id
87
    }
88
  }
89
90
  url = "#{base_url}/issues/#{issue_id}.json"
91
  uri = URI.parse(url)
92
  req = Net::HTTP::Put.new(uri.request_uri)
93
  
94
  req["Content-Type"] = "application/json"
95
  req['X-Redmine-API-Key'] = api_token
96
  req.body = payload.to_json
97
98
  http = Net::HTTP.new(uri.host, uri.port)
99
  http.use_ssl = true
100
  response = http.request(req)
101
  return response
102
end
103
104
# Set Status of issue #9599 to internal status_id = 1
105
response = update_issue_status 9599, 1, "Changed Issue Status via REST-API"
106
</code></pre>