Module: RestRedmine::API

Defined in:
lib/rest_redmine/api.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



7
8
9
# File 'lib/rest_redmine/api.rb', line 7

def config
  @config
end

Class Method Details

.get_path(action, id: nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rest_redmine/api.rb', line 10

def self.get_path(action, id: nil)
  url = "#{RestRedmine.configuration.server_url}/#{action}"
  url += "/#{id}" if id
  url += ".json"

  url
end

.request(action, data: {}, id: nil, method: :get) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rest_redmine/api.rb', line 18

def self.request(action, data: {}, id: nil, method: :get)
  retries ||= RestRedmine.configuration.retries
  url = get_path(action, id: id)

  if RestRedmine.configuration.api_key.nil?
    message = "
You must set configure first.

RestRedmine.configure do |config|
  config.api_key = '<api_key>'
  config.server_url = '<server_url>'
end
    "
    raise RestRedmine::Exception.new(message)
  end

  response = RestClient::Request.execute(
    :method => method, 
    :url => url, 
    :payload => data, 
    :headers => {
      :accept => :json,
      "X-Redmine-API-Key" => RestRedmine.configuration.api_key
    },
    :timeout => RestRedmine.configuration.timeout,
    :open_timeout => RestRedmine.configuration.timeout
  )
rescue RestRedmine::Exception => e
  RestRedmine.log << e
  RestRedmine.log << "REDMINE FAIL\nurl - #{url}\nparams - #{data}\nmethod - #{method}"
rescue => e
  RestRedmine.log << e
  RestRedmine.log << "REDMINE FAIL\nurl - #{url}\nparams - #{data}\nmethod - #{method}"
  retry unless (retries -= 1).zero?
else
  if response.length > 0
    JSON.parse(response)
  else
    true
  end
end