Module: RedmineAPIHelper::APIHelper

Included in:
Helpers
Defined in:
lib/redmine_api_helper/api_helper.rb

Defined Under Namespace

Classes: TooManyRedirects

Constant Summary collapse

LIMIT_REDIRECTS =
10
USER_AGENT =
["Redmine API Helper", RedmineAPIHelper::VERSION].join(" ")

Instance Method Summary collapse

Instance Method Details

#create_object(object, params = {}) ⇒ Object

creates a new object with params, corresponds to controller#create



84
85
86
87
88
# File 'lib/redmine_api_helper/api_helper.rb', line 84

def create_object(object, params={})
  jpost( {object => params}, :url => send("#{object.to_s.pluralize}_url") ).send(object)
rescue Exception => err
  error(err)
end

#create_project_object(project_id, object, params = {}) ⇒ Object

def



90
91
92
93
94
# File 'lib/redmine_api_helper/api_helper.rb', line 90

def create_project_object(project_id, object, params={})
  jpost( {object => params}, :url => send("project_#{object.to_s.pluralize}_url", project_id) ).send(object)
rescue Exception => err
  error(err)
end

#destroy_object(object, id, params = {}) ⇒ Object

deletes an existing object with params, corresponds to controller#destroy



114
115
116
117
118
# File 'lib/redmine_api_helper/api_helper.rb', line 114

def destroy_object(object, id, params={})
  jdel(:url => send("#{object}_url", id), :params => params )
rescue Exception => err
  error(err)
end

#destroy_project_object(project_id, object, id, params = {}) ⇒ Object

def



120
121
122
123
124
# File 'lib/redmine_api_helper/api_helper.rb', line 120

def destroy_project_object(project_id, object, id, params={})
  jdel(:url => send("project_#{object}_url", project_id, id), :params => params )
rescue Exception => err
  error(err)
end

#error(err) ⇒ Object

returns error



36
37
38
# File 'lib/redmine_api_helper/api_helper.rb', line 36

def error(err)
  OpenStruct.new(:error => err.message, :backtrace => err.backtrace)
end

#fetch(options = {}) ⇒ Object

fetch(options), get request



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/redmine_api_helper/api_helper.rb', line 129

def fetch(options={})
  
  # create query parameters
  params                   = options[:params].to_h.to_query
  url                      = options[:url]
  
  # create GET request
  uri                      = URI.parse([url, params.presence].compact.join("?"))
  req                      = Net::HTTP::Get.new(uri.request_uri)
  
  # create HTTP handler
  http                     = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl             = uri.scheme.downcase == "https"
  
  # get request
  @http_response           = http.request(req)
  
  case @http_response
  
  when Net::HTTPSuccess 
    @http_response.body.present? ? @http_response.body : serialize(:response => "OK")
    
  when Net::HTTPRedirection 
    options[:redirects] = options[:redirects].to_i + 1
    raise TooManyRedirects if options[:redirects] > LIMIT_REDIRECTS
    fetch( options.merge(:url => response['location']) )
    
  else
    serialize(:response => @http_response.code)
    
  end
  
end

#jdel(options = {}) ⇒ Object

jdel(options), delete request



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/redmine_api_helper/api_helper.rb', line 274

def jdel(options={})
  
  index                    = options[:index].to_i
  json                     = options[:json].nil? || !!options[:json]
  params                   = json ? options[:params].to_h.merge(:format => "json").to_query : options[:params].to_h.to_query
  content_type             = json ? "application/json" : options[:content_type]
  api                      = options[:api_key].nil? ? true : !!options[:api_key]
  url                      = options[:url].presence || args.objects[index].object_url
  
  # create DELETE request
  uri                      = URI.parse( [url, params.presence].compact.join("?"))
  req                      = Net::HTTP::Delete.new(uri.request_uri)
  req["Content-Type"]      = content_type
  req['X-Redmine-API-Key'] = args.api_key if api
  req["Referer"]           = args.deep_try(:eparams, :url) || args.deep_try(:urls, :back)
  req["User-Agent"]        = USER_AGENT
  req["Accept"]            = "*/*"
  req.basic_auth(args.site_user, args.site_password) if args.site_user.present? || args.site_password.present?
  
  # create HTTP handler
  http                     = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl             = uri.scheme.downcase == "https"
  
  # get request
  @http_response           = http.request(req)
  
  # follow redirection or get result code
  handle_response(options)
  
end

#jget(options = {}) ⇒ Object

jget(options), get request



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/redmine_api_helper/api_helper.rb', line 166

def jget(options={})
  
  index                    = options[:index].to_i
  json                     = options[:json].nil? || !!options[:json]
  params                   = json ? options[:params].to_h.merge(:format => "json").to_query : options[:params].to_h.to_query
  content_type             = json ? "application/json" : options[:content_type]
  api                      = options[:api_key].nil? ? true : !!options[:api_key]
  url                      = options[:url].presence || args.objects[index].object_url
  
  # create GET request
  uri                      = URI.parse( [url, params.presence].compact.join("?"))
  req                      = Net::HTTP::Get.new(uri.request_uri)
  req["Content-Type"]      = content_type
  req['X-Redmine-API-Key'] = args.api_key if api
  req["Referer"]           = args.deep_try(:eparams, :url) || args.deep_try(:urls, :back)
  req["User-Agent"]        = USER_AGENT
  req["Accept"]            = "*/*"
  req.basic_auth(args.site_user, args.site_password) if args.site_user.present? || args.site_password.present?
  
  # create HTTP handler
  http                     = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl             = uri.scheme.downcase == "https"
  
  # get request
  @http_response           = http.request(req)
  
  # follow redirection or get result code
  handle_response(options)
  
end

#jpost(body, options = {}) ⇒ Object

jpost(body, options), post request



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/redmine_api_helper/api_helper.rb', line 237

def jpost(body, options={})
  
  index                    = options[:index].to_i
  json                     = options[:json].nil? || !!options[:json]
  params                   = json ? options[:params].to_h.merge(:format => "json").to_query : options[:params].to_h.to_query
  content_type             = json ? "application/json" : options[:content_type]
  api                      = options[:api_key].nil? ? true : !!options[:api_key]
  url                      = options[:url].presence || args.objects[index].object_url
  
  # create POST request
  uri                      = URI.parse( [url, params.presence].compact.join("?"))
  req                      = Net::HTTP::Post.new(uri.request_uri)
  req["Content-Type"]      = content_type 
  req['X-Redmine-API-Key'] = args.api_key if api
  req["Referer"]           = args.deep_try(:eparams, :url) || args.deep_try(:urls, :back)
  req["User-Agent"]        = USER_AGENT
  req["Accept"]            = "*/*"
  req.basic_auth(args.site_user, args.site_password) if args.site_user.present? || args.site_password.present?
  
  # create body
  req.body                 = deserialize(body).to_json
  
  # create HTTP handler
  http                     = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl             = uri.scheme.downcase == "https"
  
  # get request
  @http_response           = http.request(req)
  
  # follow redirection or get result code
  handle_response(options)
  
end

#jput(body, options = {}) ⇒ Object

jput(body, options), put request



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/redmine_api_helper/api_helper.rb', line 200

def jput(body, options={})
  
  index                    = options[:index].to_i
  json                     = options[:json].nil? || !!options[:json]
  params                   = json ? options[:params].to_h.merge(:format => "json").to_query : options[:params].to_h.to_query
  content_type             = json ? "application/json" : options[:content_type]
  api                      = options[:api_key].nil? ? true : !!options[:api_key]
  url                      = options[:url].presence || args.objects[index].object_url
  
  # create PUT request
  uri                      = URI.parse( [url, params.presence].compact.join("?"))
  req                      = Net::HTTP::Put.new(uri.request_uri)
  req["Content-Type"]      = content_type 
  req['X-Redmine-API-Key'] = args.api_key if api
  req["Referer"]           = args.deep_try(:eparams, :url) || args.deep_try(:urls, :back)
  req["User-Agent"]        = USER_AGENT
  req["Accept"]            = "*/*"
  req.basic_auth(args.site_user, args.site_password) if args.site_user.present? || args.site_password.present?
  
  # create body
  req.body                 = deserialize(body).to_json
  
  # create HTTP handler
  http                     = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl             = uri.scheme.downcase == "https"
  
  # get request
  @http_response           = http.request(req)
  
  # follow redirection or get result code
  handle_response(options)
  
end

#list_objects(objects, params = {}) ⇒ Object

lists objects, corresponds to controller#index



54
55
56
57
58
# File 'lib/redmine_api_helper/api_helper.rb', line 54

def list_objects(objects, params={})
  jget(:url => send("#{objects}_url"), :params => params ).send(objects) 
rescue Exception => err
  error(err)
end

#list_project_objects(project_id, objects, params = {}) ⇒ Object

def



60
61
62
63
64
# File 'lib/redmine_api_helper/api_helper.rb', line 60

def list_project_objects(project_id, objects, params={})
  jget(:url => send("project_#{objects}_url", project_id), :params => params ).send(objects)
rescue Exception => err
  error(err)
end

#read_object(object, id, params = {}) ⇒ Object

reads object having id, corresponds to controller#show



69
70
71
72
73
# File 'lib/redmine_api_helper/api_helper.rb', line 69

def read_object(object, id, params={})
  jget(:url => send("#{object}_url", id), :params => params ).send(object)
rescue Exception => err
  error(err)
end

#read_project_object(project_id, object, id, params = {}) ⇒ Object

def



75
76
77
78
79
# File 'lib/redmine_api_helper/api_helper.rb', line 75

def read_project_object(project_id, object, id, params={})
  jget(:url => send("project_#{object}_url", project_id, id), :params => params ).send(object)
rescue Exception => err
  error(err)
end

#update_object(object, id, params = {}) ⇒ Object

updates an existing object with params, corresponds to controller#update



99
100
101
102
103
# File 'lib/redmine_api_helper/api_helper.rb', line 99

def update_object(object, id, params={})
  jput( {object  => params}, :url => send("#{object}_url", id) )
rescue Exception => err
  error(err)
end

#update_project_object(project_id, object, id, params = {}) ⇒ Object

def



105
106
107
108
109
# File 'lib/redmine_api_helper/api_helper.rb', line 105

def update_project_object(project_id, object, id, params={})
  jput( {object  => params}, :url => send("project_#{object}_url", project_id, id) )
rescue Exception => err
  error(err)
end

#url_path(*fragments, **options) ⇒ Object

assembles url from fragments



43
44
45
46
47
48
49
# File 'lib/redmine_api_helper/api_helper.rb', line 43

def url_path(*fragments, **options)
  [fragments.map do |fragment|
     fragment.to_s.gsub(/\/\z/,"")
   end.join("/"),
   options.to_query.presence
  ].compact.join("?")
end