class Ruboty::Redmine::Client

Public Class Methods

new(url, api_key, options) click to toggle source
# File lib/ruboty/redmine/client.rb, line 8
def initialize(url, api_key, options)
  @url = url
  @api_key = api_key
  @options = options
end

Public Instance Methods

create_issue(opts) click to toggle source
# File lib/ruboty/redmine/client.rb, line 59
def create_issue(opts)
  req = {
    issue: {
      subject: opts[:subject],
      project_id: opts[:project].id,
    },
  }
  req[:issue][:tracker_id] = opts[:tracker].id if opts[:tracker]

  OpenStruct.new(
    JSON.parse(post('/issues.json', req).body)['issue']
  )
  # {
  #   "issue": {
  #     "id": 1,
  #     "project": {
  #       "id": 1,
  #       "name": "..."
  #     },
  #     "tracker": {
  #       "id": 1,
  #       "name": "..."
  #     },
  #     "status": {
  #       "id": 1,
  #       "name": "new"
  #     },
  #     "priority": {
  #       "id": 4,
  #       "name": "通常"
  #     },
  #     "author": {
  #       "id": 1,
  #       "name": "Arai Ryota"
  #     },
  #     "subject": "This is test",
  #     "start_date": "2017-02-01",
  #     "done_ratio": 0,
  #     "created_on": "2017-02-01T11:10:35Z",
  #     "updated_on": "2017-02-01T11:10:35Z"
  #   }
  # }
end
find_project(query) click to toggle source
# File lib/ruboty/redmine/client.rb, line 28
def find_project(query)
  projects.find do |project|
    [
      project.id.to_s,
      project.name.downcase,
      project.identifier.downcase,
    ].include?(query.downcase)
  end
end
find_tracker(query) click to toggle source
# File lib/ruboty/redmine/client.rb, line 38
def find_tracker(query)
  trackers.find do |tracker|
    [
      tracker.id.to_s,
      tracker.name.downcase,
    ].include?(query.downcase)
  end
end
issues(opts) click to toggle source
# File lib/ruboty/redmine/client.rb, line 47
def issues(opts)
  params = {}
  params[:project_id] = opts[:project].id if opts[:project]
  params[:tracker_id] = opts[:tracker].id if opts[:tracker]
  params[:sort] = opts[:sort] if opts[:sort]

  res = JSON.parse(get('/issues.json', params).body)
  res['issues'].map do |tracker|
    OpenStruct.new(tracker)
  end
end
projects() click to toggle source
# File lib/ruboty/redmine/client.rb, line 14
def projects
  res = JSON.parse(get('/projects.json').body)
  res['projects'].map do |project|
    OpenStruct.new(project)
  end
end
trackers() click to toggle source
# File lib/ruboty/redmine/client.rb, line 21
def trackers
  res = JSON.parse(get('/trackers.json').body)
  res['trackers'].map do |tracker|
    OpenStruct.new(tracker)
  end
end
update_issue(issue, opts) click to toggle source
# File lib/ruboty/redmine/client.rb, line 103
def update_issue(issue, opts)
  req = {issue: opts}

  res = put("/issues/#{issue.id}.json", req)
  unless res.status == 200
    raise "Updating issue failed. (#{res.body})"
  end
end
url_for_issue(issue) click to toggle source
# File lib/ruboty/redmine/client.rb, line 112
def url_for_issue(issue)
  URI.join(@url, "/issues/#{issue.id}")
end

Private Instance Methods

conn() click to toggle source
# File lib/ruboty/redmine/client.rb, line 118
def conn
  new_options = {url: @url}
  new_options[:proxy] = @options[:http_proxy] if @options[:http_proxy]

  conn = Faraday.new(new_options) do |faraday|
    faraday.request  :url_encoded
    faraday.response :logger if ENV['DEBUG']
    faraday.adapter  Faraday.default_adapter
  end

  basic_auth_user = @options[:basic_auth_user]
  basic_auth_password = @options[:basic_auth_password]

  if basic_auth_user && basic_auth_password
    conn.basic_auth(basic_auth_user, basic_auth_password)
  end

  conn
end
get(path, params = {}) click to toggle source
# File lib/ruboty/redmine/client.rb, line 138
def get(path, params = {})
  conn.get do |req|
    req.url path
    req.params = params
    req.headers['X-Redmine-API-Key'] = @api_key
  end
end
post(path, params = {}) click to toggle source
# File lib/ruboty/redmine/client.rb, line 146
def post(path, params = {})
  conn.post do |req|
    req.url path
    req.body = params.to_json
    req.headers['X-Redmine-API-Key'] = @api_key
    req.headers['Content-Type'] = 'application/json'
  end
end
put(path, params = {}) click to toggle source
# File lib/ruboty/redmine/client.rb, line 155
def put(path, params = {})
  conn.put do |req|
    req.url path
    req.body = params.to_json
    req.headers['X-Redmine-API-Key'] = @api_key
    req.headers['Content-Type'] = 'application/json'
  end
end