Class: Redmine::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/redmine/client.rb

Overview

The client is a Redmine-aware REST API client that maps remote resources into local methods and data. It uses the RestClient under the hood and outputs our own domain objects.

Instance Method Summary collapse

Constructor Details

#initialize(rest_client:) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/redmine/client.rb', line 9

def initialize(rest_client:)
  @rest_client = rest_client
end

Instance Method Details

#issue(issue_id) ⇒ Object



13
14
15
16
# File 'lib/redmine/client.rb', line 13

def issue(issue_id)
  data = @rest_client.get("/issues/#{issue_id}.json?include=journals").first
  Issue.new(data.fetch('issue'))
end

#issue_statusesObject



32
33
34
35
36
37
# File 'lib/redmine/client.rb', line 32

def issue_statuses
  @rest_client
    .get('/issue_statuses.json')
    .first
    .fetch('issue_statuses')
end

#issues(options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redmine/client.rb', line 39

def issues(options = {}) # rubocop:disable Metrics/AbcSize
  options = { limit: 10, offset: 0, sort: :asc }.merge(options.to_h)
  Enumerator.new do |yielder|
    loop do
      result, _response = @rest_client.get(
        '/issues.json?' + URI.encode_www_form(options)
      )
      result.fetch('issues').each { |issue| yielder << Issue.new(issue) }
      position = result.fetch('limit') + result.fetch('offset')
      raise StopIteration unless result.fetch('total_count').to_i > position
      options.merge!(offset: options.fetch(:limit) + options.fetch(:offset))
    end
  end
end

#project(id) ⇒ Object



26
27
28
29
30
# File 'lib/redmine/client.rb', line 26

def project(id)
  @rest_client
    .get("/projects/#{id}.json?include=trackers")
    .fetch('project')
end

#projectsObject



18
19
20
21
22
23
24
# File 'lib/redmine/client.rb', line 18

def projects
  @rest_client
    .get('/projects.json')
    .first
    .fetch('projects')
    .map { |p| Project.new(p) }
end