Class: Synrp::SynRP

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

Instance Method Summary collapse

Instance Method Details

#calculate_remaining_availabilities(yearWeek) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/synrp/synrp.rb', line 130

def calculate_remaining_availabilities(yearWeek)
  avails = get_availabilities(yearWeek)
  avails_minus_requests = avails.map do |person_id, minutes|
    person = find_person_by_id(person_id)
    reqs_of_person = get_allocation_requests_of_person(yearWeek, person_id)
    remaining_minutes = minutes - sum_minutes_of_requests(reqs_of_person)
    { person => remaining_minutes }
  end.inject(&:merge)
  avails_minus_requests.to_a.sort_by { |av| -av[1] }
end

#connect(site, username, password) ⇒ Object



53
54
55
56
57
58
# File 'lib/synrp/synrp.rb', line 53

def connect(site, username, password)
  $conn = Net::HTTP.new(site, 80)
  resp, data = $conn.post("/web/j_spring_security_check", "j_username=#{username}&j_password=#{password}", {})
  cookie = resp.response['set-cookie']
  $headers = { "Cookie" => cookie }
end

#find_person_by_id(id) ⇒ Object



85
86
87
# File 'lib/synrp/synrp.rb', line 85

def find_person_by_id(id)
  get_persons.find {|p| p["id"].to_s == id.to_s }
end

#find_person_id_by_displayname(person_name) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/synrp/synrp.rb', line 70

def find_person_id_by_displayname(person_name)
  person = get_persons.find do |p|
    # replace weird whitespace with new one before comparing
    p["displayName"].gsub(/\302\240/, ' ').match(person_name)
  end

  unless person
    person = get_persons.find do |p|
      p["lastName"].gsub(/\302\240/, ' ').match(/#{person_name}/i)
    end
  end

  person_id = person ? person["id"] : nil
end

#find_project_id_by_project_name(pname) ⇒ Object



65
66
67
68
# File 'lib/synrp/synrp.rb', line 65

def find_project_id_by_project_name(pname)
  project = get_projects.find {|p| p["name"] == pname }
  project_id = project ? project["id"] : nil
end

#get_allocation_requests(yearWeek) ⇒ Object



106
107
108
109
# File 'lib/synrp/synrp.rb', line 106

def get_allocation_requests(yearWeek)
  response = $conn.get("/web/allocation-requests.json?yearWeek=#{yearWeek}", $headers)
  JSON.parse response.body
end

#get_allocation_requests_of_person(yearWeek, person_id) ⇒ Object



111
112
113
114
# File 'lib/synrp/synrp.rb', line 111

def get_allocation_requests_of_person(yearWeek, person_id)
  reqs = get_allocation_requests(yearWeek)
  reqs.find_all { |req| req["person"]["id"].to_s == person_id.to_s }
end

#get_assignments(yearWeek) ⇒ Object



116
117
118
119
# File 'lib/synrp/synrp.rb', line 116

def get_assignments(yearWeek)
  response = $conn.get("/web/assignments.csv?yearWeek=#{yearWeek}", $headers)
  assignments = response.body.split("\n")[1..-1].map { |as| as.split("|") }
end

#get_assignments_grouped(yearWeek) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/synrp/synrp.rb', line 121

def get_assignments_grouped(yearWeek)
  grouped_as = Hash.new
  get_assignments(yearWeek).map do |fname, lname, proj, days|
    grouped_as[proj] = [] unless grouped_as[proj]
    grouped_as[proj] << "#{fname} #{lname}"
  end
  grouped_as
end

#get_availabilities(yearWeek) ⇒ Object



94
95
96
97
98
# File 'lib/synrp/synrp.rb', line 94

def get_availabilities(yearWeek)
  response = $conn.get("/web/availabilities.json?yearWeek=#{yearWeek}", $headers)
  av = JSON.parse response.body
  without_invisible = av.reject { |k,v| !find_person_by_id(k)["visible"] }
end

#get_availabilities_persons(yearWeek) ⇒ Object

get availabilities with resolved person, not their IDs



101
102
103
104
# File 'lib/synrp/synrp.rb', line 101

def get_availabilities_persons(yearWeek)
  avails = get_availabilities(yearWeek)
  avails.map { |k,v| { find_person_by_id(k)["displayName"] => "#{v / 60.0} h" } }.inject(&:merge)
end

#get_personsObject



60
61
62
63
# File 'lib/synrp/synrp.rb', line 60

def get_persons
  response = $conn.get("/web/persons.json", $headers)
  JSON.parse response.body
end

#get_projectsObject



89
90
91
92
# File 'lib/synrp/synrp.rb', line 89

def get_projects
  response = $conn.get("/web/projects.json", $headers)
  JSON.parse response.body
end

#read_configObject



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

def read_config
  begin
    return YAML.load_file(File.expand_path("~/.synRP_config.yaml"))
  rescue
    begin
      current_dir = File.dirname(File.expand_path(__FILE__))
      return YAML.load_file(current_dir + "/config.yaml")
    rescue
    end
  end
  return {}
end

#request_res(yearWeek, project, person, minutes, comment = '', username = 'synrp cli') ⇒ Object



149
150
151
152
153
# File 'lib/synrp/synrp.rb', line 149

def request_res(yearWeek, project, person, minutes, comment='', username='synrp cli')
  params = "yearWeek=#{yearWeek}&project=#{project}&person=#{person}&minutes=#{minutes}&comment=#{comment}&by=#{username}"
  puts "Requesting #{params}"
  $conn.post("/web/allocation-requests/", params, $headers)
end

#setup(opts) ⇒ Object



18
19
20
# File 'lib/synrp/synrp.rb', line 18

def setup(opts)
  opts = verify_opts_present(opts, [ :site, :username, :password ])
end

#sum_minutes_of_requests(reqs) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/synrp/synrp.rb', line 141

def sum_minutes_of_requests(reqs)
  sum = 0
  reqs.each do |req|
    sum += req["minutes"]
  end
  sum
end

#verify_opts_present(opts, req_opts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/synrp/synrp.rb', line 22

def verify_opts_present(opts, req_opts)
  config_opts = read_config
  req_opts.each do |r_opt|
    unless opts[r_opt]
      if config_opts[r_opt.to_s]
        opts[r_opt] = config_opts[r_opt.to_s]
      elsif r_opt == :password
        opts[:password] = ask("Enter Password: ") {|q| q.echo = "*"}
        puts
      else
        $stderr.puts "--#{r_opt} option is required"
        exit 1
      end
    end
  end
  opts
end