ruby - How can I parse just the bug count from the PivotalTracker API? -
i'm issuing request pivotaltracker api of bugs given project, bug severity. need count of bugs (i.e. 10 critical bugs), i'm getting of raw data each bug in xml format. xml data has bug count @ top, have scroll tons of data count.
to solve issue, i'm trying parse xml display bug count, i'm not sure how that. i've experimented nokogiri , rexml, seems can parse actual xml files, not xml http request.
here code (the access token been replaced *'s security reasons):
require 'net/http' require 'rexml/document' prompt = '> ' puts "what id of project want data from?" print prompt project_id = stdin.gets.chomp() puts "what type of bugs want get?" print prompt type = stdin.gets.chomp() def bug(project_id, type) net = net::http.new("www.pivotaltracker.com") request = net::http::get.new("/services/v3/projects/#{project_id}/stories?filter=label%3aqa-#{type}") request.add_field("x-trackertoken", "*******************") net.read_timeout = 10 net.open_timeout = 10 response = net.start |http| http.request(request) end puts response.code print response.read_body end bug(project_id, type) like said, request printing bug count , of raw data each individual bug terminal window, want print bug count.
the api documentation shows total bug count attribute of xml response's top-level node, stories.
using nokogiri example, try replacing print response.read_body with
xml = nokogiri::xml.parse(response.body) puts "bug count: #{xml.xpath('/stories/@total')}" naturally you'll need add require 'nokogiri' @ top of code well.
Comments
Post a Comment