Created
October 23, 2017 08:35
-
-
Save maeda-m/3aa86eeee09463bb8c1b5a23d0cee5c8 to your computer and use it in GitHub Desktop.
Redmine API Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Membership < RedmineBase | |
attr_accessor :id, :project_id, :user_id, :role_ids | |
def project=(attrs = {}) | |
self.project_id = attrs[:id] | |
end | |
def project | |
if self.project_id | |
return Project.all.find do |project| | |
self.project_id == project.id | |
end | |
else | |
return nil | |
end | |
end | |
def user=(attrs = {}) | |
self.user_id = attrs[:id] | |
end | |
def user | |
if self.user_id | |
return User.all.find do |user| | |
self.user_id == user.id | |
end | |
else | |
return nil | |
end | |
end | |
def roles=(records) | |
self.role_ids ||= [] | |
records.each do |record| | |
self.role_ids << record[:id] | |
end | |
end | |
def roles | |
self.role_ids ||= [] | |
return self.role_ids.map do |role_id| | |
Role.all.find { |role| role_id == role.id } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Project < RedmineBase | |
attr_accessor :id, :name, :identifier, :parent_id, :description, :homepage, :status, :is_public, :created_on, :updated_on, | |
:trackers, :issue_categories, :enabled_modules | |
def self.include_records | |
return [ 'trackers', 'issue_categories', 'enabled_modules' ] | |
end | |
def parent=(attrs = {}) | |
self.parent_id = attrs[:id] | |
end | |
def parent | |
if self.parent_id.empty? | |
return Project.all.find do |project| | |
self.parent_id == project.id | |
end | |
else | |
return nil | |
end | |
end | |
# 親子関係を判別し、配下のプロジェクトを全て取得するメソッド | |
def children | |
@children ||= [] | |
if @children.empty? | |
Project.all.each do |project| | |
parent = project.parent | |
@children << parent if parent && self.id == parent.id | |
end | |
end | |
return @children | |
end | |
# http://www.redmine.org/projects/redmine/wiki/Rest_WikiPages | |
def wikis | |
@wikis ||= [] | |
if @wikis.empty? && self.enabled_module_names.include?('wiki') | |
# Wikiページ一覧情報を取得 | |
wiki_pages = self.get(:wiki_pages, "/projects/#{self.identifier}/wiki/index") | |
wiki_pages.each do |attrs| | |
wiki_page = self.get(:wiki_page, "/projects/#{self.identifier}/wiki/#{attrs[:title]}") | |
@wikis << Wiki.new(wiki_page) | |
end | |
end | |
return @wikis | |
end | |
def enabled_module_names | |
self.enabled_modules.map { |enabled_module| enabled_module[:name] } | |
end | |
# http://www.redmine.org/projects/redmine/wiki/Rest_Memberships | |
def memberships | |
@memberships ||= [] | |
if @memberships.empty? | |
memberships = self.get(:memberships, "/projects/#{self.identifier}/memberships") | |
memberships.each do |attrs| | |
@memberships << Membership.new(attrs) | |
end | |
end | |
return @memberships | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'json' | |
require 'uri' | |
require 'active_support' | |
require 'active_support/all' | |
class RedmineBase | |
def self.url | |
return URI.parse('http://xxxxxxx') | |
end | |
def self.key | |
return 'xxxxxx' | |
end | |
def self.get(name, action, params = {}) | |
params.merge!({ key: self.key }) | |
params = params.map { |k, v| k.to_s + '=' + v.to_s }.join('&') | |
response = Net::HTTP.start(self.url.host, self.url.port) do |http| | |
http.get("#{action}.json?#{params}") | |
end | |
if response.body | |
return JSON.parse(response.body, symbolize_names: true)[name] | |
else | |
return {} | |
end | |
end | |
# 全レコード取得メソッド(レコード取得上限があるためメソッドを上書き) | |
def self.all | |
@records ||= [] | |
# レコード情報は一度に100件まで取得可能 | |
limit = 100 | |
# 10000レコードを上限に、情報を取得 | |
(0..99).to_a.each do |i| | |
# ページ割を算出し取得 | |
offset = i * limit | |
params = { limit: limit, offset: offset } | |
params.merge!(include: self.include_records.join(',')) if self.include_records.present? | |
projects = self.get(self.name.tableize.to_sym, '/' + self.name.tableize, params) | |
projects.each do |attrs| | |
@records << self.new(attrs) | |
end | |
end if @records.empty? | |
return @records | |
end | |
def self.include_records | |
return [] | |
end | |
def initialize(attrs) | |
attrs.each do |k, v| | |
self.public_send("#{k}=", v) | |
end | |
end | |
def get(name, action, params = {}) | |
self.class.get(name, action, params) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Role < RedmineBase | |
attr_accessor :id, :name | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < RedmineBase | |
attr_accessor :id, :login, :firstname, :lastname, :mail, :created_on, :last_login_on | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Wiki < RedmineBase | |
attr_accessor :title, :parent_title, :text, :version, :author_id, :comments, :created_on, :updated_on | |
def author=(attrs = {}) | |
self.author_id = attrs[:id] | |
end | |
def author | |
if self.author_id | |
return User.all.find do |user| | |
self.author_id == user.id | |
end | |
else | |
return nil | |
end | |
end | |
def parent=(attrs = {}) | |
self.parent_title = attrs[:title] | |
end | |
def parent | |
if self.parent_title | |
return Wiki.all.find do |wiki| | |
self.parent_title == wiki.title | |
end | |
else | |
return nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment