Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
CSVファイルを読み込んでRedmineにREST API経由でユーザーを登録する
#!/usr/bin/env ruby
# encoding: utf-8
#
# CSVファイルを読み込んで、REST API経由でRedmineのユーザーを作成する
#
# CSVファイルの形式:
# ログインID,パスワード,名,姓,メールアドレス
require 'rest-client' # gem install rest-client
require 'open-uri'
require 'json'
require 'csv'
require 'kconv'
REDMINE_USER = 'admin' # システム管理者でなければならない
REDMINE_PASSWORD = 'admin'
REDMINE_URL = 'http://redmine-trunk.dev/'
REDMINE_QUERY_SIZE = 100
csvdata = Kconv::kconv(ARGF.read, Kconv::UTF8)
line_no = 0
CSV.parse(csvdata) do |row|
line_no += 1
userdata = {
:login => row[0],
:password => row[1],
:firstname => row[2],
:lastname => row[3],
:mail => row[4],
}
begin
RestClient::Request.execute(
:method => :post,
:url => "#{REDMINE_URL}/users.json",
:user => REDMINE_USER,
:password => REDMINE_PASSWORD,
:payload => {:user => userdata}.to_json,
:headers => {
:content_type => 'application/json'
}
)
STDERR.puts "#{ARGF.filename}:#{line_no}: success"
rescue => e
STDERR.puts "#{ARGF.filename}:#{line_no}: error: #{e.message}: #{userdata.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment