-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.rb
executable file
·63 lines (48 loc) · 1.67 KB
/
tracker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
59
60
61
62
63
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'nokogiri'
require 'json'
require 'yaml'
require 'time'
config = YAML.load_file('config.yml')
@api_key = config['api_key']
@base_url = config['server_url']
json_url = 'tracker.json'
url = "https://#@base_url/#{json_url}?api_key=#{@api_key}"
uri = URI(url)
response = Net::HTTP.get(uri)
@tracker_json = JSON.parse(response)['tracker']
def build_empty_tracker_xml
build_tracker_empty = Nokogiri::XML::Builder.new { |xml|
xml.items {
xml.item('uuid' => 'string', 'arg' => "#@base_url", 'type' => 'string') {
xml.title('Currently there is no running tracker')
}
}
}
puts build_tracker_empty.to_xml
end
def build_tracker_xml
ticket_url = "time_entries/#{@tracker_json['tracking_time_entry']['id']}.json"
url_ticket = "https://#@base_url/#{ticket_url}?api_key=#{@api_key}"
ticket_uri = URI(url_ticket)
ticket_response = Net::HTTP.get(ticket_uri)
ticket_json = JSON.parse(ticket_response)['time_entry']
date = Time.parse("#{@tracker_json['tracking_time_entry']['since']}").to_s.split(' ')[0]
time = Time.parse("#{@tracker_json['tracking_time_entry']['since']}").to_s.split(' ')[1]
build_tracker = Nokogiri::XML::Builder.new { |xml|
xml.items {
xml.item('uuid' => 'string', 'arg' => "#{@base_url}/time_entries/#{@tracker_json['tracking_time_entry']['id']}", 'type' => 'string') {
xml.title("#{ticket_json['project_name']} - #{@tracker_json['tracking_time_entry']['minutes']} Minutes")
xml.subtitle("Started Tracker on #{date} at #{time}")
}
}
}
puts build_tracker.to_xml
end
if response == '{"tracker":{}}'
build_empty_tracker_xml
else
build_tracker_xml
end