Project

General

Profile

p2r_lib.rb

should be in the same directory when running p2r.rb - Alexei Siventsev, 2019-05-23 12:42

 
1
# Copyright (c) 2019 NPO Karat
2
# Author: Aleksei Siventsev
3
#=====================================================================
4
# Console Script for MS Project to Redmine synchronization - utils
5
#=====================================================================
6

    
7
#-----------------------------------------------------------------------------------------------------
8
# console help
9
#-----------------------------------------------------------------------------------------------------
10
# include help text to code for future compilation
11
HELP=
12
    %{
13
Command line options
14
--------------------
15
  --help or -h      this text
16
  --execute or -e   actual execution (dry run is default)
17

    
18
}
19

    
20
#-----------------------------------------------------------------------------------------------------
21
# exit on condition
22
#-----------------------------------------------------------------------------------------------------
23
def chk cond,msg;if cond;puts msg+"\n\n";exit 0;end;end
24

    
25
#-----------------------------------------------------------------------------------------------------
26
# REST to Redmine API
27
#-----------------------------------------------------------------------------------------------------
28
def rm_request path, data = nil,method = nil, verbose = false
29
  reply = nil
30
  hdrs = {'Content-Type'=>'application/json', 'X-Redmine-API-Key' => $settings['redmine_api_key']}
31
  reply = Net::HTTP.start($settings['redmine_host'], $settings['redmine_port']) do |http|
32
    if data
33
      if !method || method == 'POST'
34
        puts "POST #{path}" if verbose
35
        http.request_post(path,data.to_json, hdrs)
36
      elsif method == 'PUT'
37
        # code for PUT here
38
        puts "PUT #{path}" if verbose
39
        http.send_request 'PUT', path, JSON.unparse(data), hdrs
40
      end
41
    else
42
      puts "GET #{path}" if verbose
43
      http.request_get(path, hdrs)
44
    end
45
  end
46
  return reply
47
end
48

    
49
#-----------------------------------------------------------------------------------------------------
50
# REST get from Redmine API
51
#-----------------------------------------------------------------------------------------------------
52
def rm_get path, entity, msg
53

    
54
  rmp = nil
55
  re = rm_request path
56
  chk (re.code!='200'), msg + "\n#{re.code} #{re.msg}\n\n"
57
  rmp = JSON.parse(re.body)[entity] rescue nil
58
  chk !rmp, msg + "\n#{re.body.inspect}\n\n"
59
  return rmp
60

    
61
end
62

    
63
#-----------------------------------------------------------------------------------------------------
64
# REST create to Redmine API
65
#-----------------------------------------------------------------------------------------------------
66
def rm_create path, entity, data, msg
67

    
68
  rmp = nil
69
  re = rm_request path, entity => data
70
  chk (re.code!='201'), msg + "\n#{re.code} #{re.msg}\n\n"
71
  rmp = JSON.parse(re.body)[entity] rescue nil
72
  chk !rmp, msg + "\n#{re.body.inspect}\n\n"
73
  return rmp
74

    
75
end
76

    
77
#-----------------------------------------------------------------------------------------------------
78
# REST update to Redmine API
79
#-----------------------------------------------------------------------------------------------------
80
def rm_update path, data, msg
81

    
82
  re = rm_request path, data, 'PUT'
83
  puts re.body
84
  chk (re.code!='200'), msg + "\n#{re.code} #{re.msg}\n\n"
85
  return true
86

    
87
end
88

    
89

    
(1-1/3)