Class: Bizside::Redmine::Client

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

Constant Summary collapse

VERSION =
'0.3.0'
DEFAULT_CONFIG =
{
  host: 'localhost',
  prefix: '/',
  api_key: '',
  verify_ssl: false,
}.freeze
@@config =
DEFAULT_CONFIG.dup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overrides = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(overrides = {})
  overrides = overrides.symbolize_keys
  if logger = overrides.delete(:logger)
    @@logger = logger
  end

  @prefix = overrides[:prefix] || Bizside::Redmine::Client.config[:prefix]
  @connection = Bizside::Redmine::Connection.new(overrides)
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



19
20
21
# File 'lib/bizside/redmine/client.rb', line 19

def prefix
  @prefix
end

Class Method Details

.set_config(config) ⇒ Object



23
24
25
26
27
28
# File 'lib/bizside/redmine/client.rb', line 23

def self.set_config(config)
  @@config = DEFAULT_CONFIG.dup
  DEFAULT_CONFIG.keys.each do |key|
    @@config[key] = config[key] unless config[key].nil?
  end
end

Instance Method Details

#create_aggregated_wiki_page(params) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/bizside/redmine/client.rb', line 123

def create_aggregated_wiki_page(params)
  params.each do |project_identifier, hash|
    hash.each do |env, page_names|
      year_months = page_names.sort.reverse.join(" ").gsub(" ", "\n")
      content = "<wiki_page><text>#{year_months}</text></wiki_page>"
      connection.post_or_put("#{prefix}/projects/#{project_identifier}/wiki/#{env}-result-analyzer.xml", content)
    end
  end
end

#create_analyzed_wiki_pages(params) ⇒ Object



111
112
113
114
115
# File 'lib/bizside/redmine/client.rb', line 111

def create_analyzed_wiki_pages(params)
  content = "<wiki_page><text>#{params[:content]}</text></wiki_page>"
  response = connection.post_or_put("#{prefix}/projects/#{params[:project_identifier]}/wiki/#{params[:page_name]}.xml", content)
  Bizside::Redmine::ResultSet.new(:wiki_page, response.status)
end

#create_errors_wiki_pages(params) ⇒ Object



117
118
119
120
121
# File 'lib/bizside/redmine/client.rb', line 117

def create_errors_wiki_pages(params)
  content = "<wiki_page><text>h3. #{params[:page_name]} <notextile></notextile>&lt;pre&gt;#{params[:content]}&lt;/pre&gt;</text></wiki_page>"
  response = connection.post_or_put("#{prefix}/projects/#{params[:project_identifier]}/wiki/#{params[:page_name]}.xml", content)
  Bizside::Redmine::ResultSet.new(:wiki_page, response.status)
end

#create_issue(params) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bizside/redmine/client.rb', line 63

def create_issue(params)
  params = params.symbolize_keys
  issue_params = params[:issue].symbolize_keys

  api_params = {
    :issue => {
      :project_id => issue_params[:project_id],
      :subject => issue_params[:subject],
    }
  }
  api_params[:issue][:tracker_id] = issue_params[:tracker_id] if issue_params[:tracker_id]
  api_params[:issue][:status_id] = issue_params[:status_id] if issue_params[:status_id]
  api_params[:issue][:priority_id] = issue_params[:priority_id] if issue_params[:priority_id]
  api_params[:issue][:description] = issue_params[:description] if issue_params[:description]
  api_params[:issue][:category_id] = issue_params[:category_id] if issue_params[:category_id]
  api_params[:issue][:fixed_version_id] = issue_params[:fixed_version_id] if issue_params[:fixed_version_id]
  api_params[:issue][:assigned_to_id] = issue_params[:assigned_to_id] if issue_params[:assigned_to_id]
  api_params[:issue][:parent_issue_id] = issue_params[:parent_issue_id] if issue_params[:parent_issue_id]
  api_params[:issue][:custom_fields] = issue_params[:custom_fields] if issue_params[:custom_fields]
  api_params[:issue][:watcher_user_ids] = issue_params[:watcher_user_ids] if issue_params[:watcher_user_ids]
  api_params[:issue][:uploads] = issue_params[:uploads] if issue_params[:uploads]

  response = connection.post("#{prefix}/issues.json", api_params)
  decode(:issue, response)
end

#create_wiki_page(params) ⇒ Object



104
105
106
107
108
109
# File 'lib/bizside/redmine/client.rb', line 104

def create_wiki_page(params)
  path = "#{prefix}/projects/#{params[:project_identifier]}/wiki/#{params[:page_name]}.xml"
  content = "<wiki_page><text>#{params[:content]}</text></wiki_page>"
  response = connection.post_or_put(path, content)
  Bizside::Redmine::ResultSet.new(:wiki_page, response.status)
end

#create_year_month_wiki_pages(params) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/bizside/redmine/client.rb', line 133

def create_year_month_wiki_pages(params)
  params.each do |project_identifier, hash|
    hash.each do |year_month, page_names|
      page_names = page_names.sort.reverse.join(" ").gsub(" ", "\n")
      content = "<wiki_page><text>#{page_names}</text></wiki_page>"
      connection.post_or_put("#{prefix}/projects/#{project_identifier}/wiki/#{year_month}.xml", content)
    end
  end
end

#dms_commit_file(params) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/bizside/redmine/client.rb', line 187

def dms_commit_file(params)
  params = params.symbolize_keys
  api_params = {
    attachments: {
      folder_id: params[:folder_id],
      uploaded_file: {
        name: params[:name],
        title: params[:title],
        description: params[:description].present? ? params[:description] : '-',
        comment: params[:comment].present? ? params[:comment] : '-',
        token: params[:token]
      }
    }
  }
  response = connection.post("#{prefix}/projects/#{params[:project_id]}/dmsf/commit.json", api_params)
  decode("", response)
end

#dms_create_folder(params) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/bizside/redmine/client.rb', line 162

def dms_create_folder(params)
  params = params.symbolize_keys

  api_params = {
    dmsf_folder: {
      title: params[:title],
      description: params[:description].present? ? params[:description] : '-'
    }
  }
  api_params[:dmsf_folder_id] = params[:parent_folder_id] if params[:parent_folder_id].present?

  response = connection.post("#{prefix}/projects/#{params[:project_id]}/dmsf/create.json", api_params)
  decode("", response)
end

#dms_folders(params) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/bizside/redmine/client.rb', line 152

def dms_folders(params)
  params = params.symbolize_keys

  api_params = {}
  api_params[:folder_id] = params[:folder_id] if params[:folder_id].present?

  response = connection.get("#{prefix}/projects/#{params[:project_id]}/dmsf.json", api_params)
  decode("", response)
end

#dms_upload_file(params) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/bizside/redmine/client.rb', line 177

def dms_upload_file(params)
  params = params.symbolize_keys
  api_params = {
    :file => params[:file]
  }
  filename = File.basename(params[:file])
  response = connection.post_with_multipart("#{prefix}/projects/#{params[:project_id]}/dmsf/upload.json?filename=#{filename}", api_params)
  decode('', response)
end

#projects(params = {}) ⇒ Object



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

def projects(params = {})
  params = params.symbolize_keys

  include_value = 'trackers,issue_categories'
  per = (params[:per] || 100).to_i
  page = (params[:page] || 1).to_i
  offset = (page - 1) * per
  api_params = { include: include_value, limit: per, offset: offset }

  response = connection.get("#{prefix}/projects.json", api_params)
  decode(:projects, response)
end

#trackersObject



53
54
55
56
# File 'lib/bizside/redmine/client.rb', line 53

def trackers
  response = connection.get("#{prefix}/trackers.json")
  decode(:trackers, response)
end

#update_issue(id, params) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bizside/redmine/client.rb', line 89

def update_issue(id, params)
  params = params.symbolize_keys
  issue_params = params[:issue].symbolize_keys

  api_params = {
    :issue => {
      :notes => issue_params[:notes],
    }
  }
  api_params[:issue][:uploads] = issue_params[:uploads] if issue_params[:uploads]

  response = connection.put("#{prefix}/issues/#{id}.json", api_params)
  decode(:issue, response)
end

#upload_file(params) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/bizside/redmine/client.rb', line 143

def upload_file(params)
  options = params.symbolize_keys
  api_params = {
    :file => options[:file]
  }
  response = connection.post_with_multipart("#{prefix}/uploads.json", api_params)
  decode(:upload, response)
end

#wiki_pages(project_identifier) ⇒ Object



58
59
60
61
# File 'lib/bizside/redmine/client.rb', line 58

def wiki_pages(project_identifier)
  response = connection.get("#{prefix}/projects/#{project_identifier}/wiki/index.json")
  decode(:wiki_pages, response)
end