forked from benstein/ruby-pivotal-tracker
-
Notifications
You must be signed in to change notification settings - Fork 7
/
pivotal_tracker.rb
205 lines (173 loc) · 5.6 KB
/
pivotal_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
162
163
164
165
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
196
197
198
199
200
201
202
203
204
205
require 'rubygems'
require 'hpricot'
require 'net/https'
require 'uri'
require 'cgi'
##
# Pivotal Tracker API Ruby Wrapper
##
class Tracker
def initialize(project_id = '123', token = '45a6a078f67d9210d2fba91f8c484e7b', ssl=true)
@project_id, @token, @ssl = project_id, token, ssl
protocol = @ssl ? 'https' : 'http'
port = @ssl ? '443' : '80'
@base_url = "#{protocol}://www.pivotaltracker.com:#{port}/services/v1/projects"
end
def project
resource_uri = URI.parse("#{@base_url}/#{@project_id}")
response = net_http(resource_uri).start do |http|
http.get(resource_uri.path, {'Token' => @token})
end
validate_response(response.body)
doc = Hpricot(response.body).at('project')
{ :name => doc.at('name').innerHTML,
:iteration_length => doc.at('iteration_length').innerHTML,
:week_start_day => doc.at('week_start_day').innerHTML,
:point_scale => doc.at('point_scale').innerHTML
}
end
def stories
resource_uri = URI.parse("#{@base_url}/#{@project_id}/stories")
response = net_http(resource_uri).start do |http|
http.get(resource_uri.path, {'Token' => @token})
end
validate_response(response.body)
doc = Hpricot(response.body)
@stories = []
doc.search('stories > story').each do |story|
@stories << story_to_hash(story)
end
@stories
end
# would ideally like to pass a size, aka :all to limit search
def find(filters = {})
uri = "#{@base_url}/#{@project_id}/stories"
unless filters.empty?
uri << "?filter="
filters.each do |key, value|
uri << CGI::escape("#{key}:\"#{value}\"")
end
end
resource_uri = URI.parse(uri)
response = net_http(resource_uri).start do |http|
http.get(resource_uri.path, {'Token' => @token})
end
validate_response(response.body)
doc = Hpricot(response.body)
@stories = []
doc.search('stories > story').each do |story|
@stories << story_to_hash(story)
end
@stories
end
def find_story(id)
resource_uri = URI.parse("#{@base_url}/#{@project_id}/stories/#{id}")
response = net_http(resource_uri).start do |http|
http.get(resource_uri.path, {'Token' => @token, 'Content-Type' => 'application/xml'})
end
validate_response(response.body)
story_xml_to_hash(response.body)
end
def create_story(story)
story_xml = build_story_xml(story)
resource_uri = URI.parse("#{@base_url}/#{@project_id}/stories")
response = net_http(resource_uri).start do |http|
http.post(resource_uri.path, story_xml, {'Token' => @token, 'Content-Type' => 'application/xml'})
end
validate_response(response.body)
story_xml_to_hash(response.body)
end
def update_story(story)
story_xml = build_story_xml(story)
resource_uri = URI.parse("#{@base_url}/#{@project_id}/stories/#{story[:id]}")
response = net_http(resource_uri).start do |http|
http.put(resource_uri.path, story_xml, {'Token' => @token, 'Content-Type' => 'application/xml'})
end
validate_response(response.body)
story_xml_to_hash(response.body)
end
def delete_story(story_id)
resource_uri = URI.parse("#{@base_url}/#{@project_id}/stories/#{story_id}")
response = net_http(resource_uri).start do |http|
http.delete(resource_uri.path, {'Token' => @token})
end
validate_response(response.body)
story_id
end
def add_comment(story_id, text)
resource_uri = URI.parse("#{@base_url}/#{@project_id}/stories/#{story_id}/notes")
comment_xml = "<note><text>#{text}</text></note>"
response = net_http(resource_uri).start do |http|
http.post(resource_uri.path, comment_xml, {'Token' => @token, 'Content-Type' => 'application/xml'})
end
validate_response(response.body)
doc = Hpricot(response.body).at('note')
{ :id => doc.at('id').innerHTML.to_i,
:text => doc.at('text').innerHTML,
:author => doc.at('author').innerHTML,
:date => doc.at('date').innerHTML }
end
private
def build_story_xml(story)
story_xml = "<story>"
story.each do |key, value|
story_xml << "<#{key}>#{remove_xml_tags(value.to_s)}</#{key}>"
end
story_xml << "</story>"
end
def validate_response(body)
response = Hpricot(body).at('response')
if response[:success]=='false'
raise TrackerException.new((response/'message').innerHTML)
end
end
def remove_xml_tags(xml)
xml.gsub(/<\/?[^>]*>/, "")
end
def net_http(uri)
h = Net::HTTP.new(uri.host, uri.port)
h.use_ssl = @ssl
h.verify_mode = OpenSSL::SSL::VERIFY_NONE
h
end
def story_xml_to_hash(xml)
doc = Hpricot(xml).at('story')
story_to_hash(doc)
end
# Converts a story xml element to a hash.
#
def story_to_hash(story)
container_list_to_hash(story.containers)
end
# Converts a list of hpricot containers to either :key => value pairs or
# to :key => { ... } pairs, depending on whether elements are leaf nodes.
#
def container_list_to_hash(containers)
hash = {}
containers.each do |container|
value = nil
if container.containers.empty?
# terminal element, add to hash
value = cast(container['type'], container.innerHTML)
else
# recurse
value = container_list_to_hash(container.containers)
end
name = container.name
hash[name.to_sym] = value
end
hash
end
# Casts xml type to ruby type.
#
def cast(type, value)
case type
when 'integer'
Integer(value)
else
value
end
end
end
class TrackerException < Exception
end