如何从Pivotal Tracker API中仅检索特定类型活动的信息?

时间:2014-07-14 16:35:01

标签: ruby json parsing iteration pivotaltracker

我从Pivotal Tracker API获取JSON信息,我需要获取某些信息,而不是所有原始数据。为此,我使用JSON.parse()将JSON转换为Ruby数组哈希。

现在,我需要遍历此数组并仅返回相关的哈希值。

例如,我只想返回具有嵌套哈希值的primary_resources哈希:

"story_type" => "feature"

为此,我编写了这段代码(response.body是从GET请求返回的数据):

@data = response.body
parsed = JSON.parse(@data)
puts parsed['primary_resources']['story_type']['feature']

当我运行脚本时,我收到此错误:

no implicit conversion of String into Integer (TypeError)

它似乎正在迭代哈希数组并查找数组的整数(如array[3]array[0]),但这对我没有帮助。我需要在主资源哈希中返回所有具有嵌套哈希:kind => story的哈希值。

我也试过这样做:

parsed.each do |entry|
  puts entry['primary_resources']['story_Type']['feature']
end

我得到了同样的错误。

这是我的完整代码:

require 'json'
require 'net/http'
require 'open-uri'
require 'openssl'
require 'active_support'

prompt = '> '
puts "What is the id of the Project you want to get data from?"
print prompt
project_id = STDIN.gets.chomp()
puts "What is the beginning date you want to return from? ex: 2014-07-02"
print prompt
date1 = STDIN.gets.chomp()
puts "What is the end date you want to return from? ex: 2014-07-16"
print prompt
date2 = STDIN.gets.chomp()



def scope(project_id, date1, date2)
  uri = URI.parse("https://www.pivotaltracker.com/services/v5/projects/#{project_id}/activity?occurred_after=#{date1}T01:00:15Z&occurred_before=#{date2}T01:00:15Z&fields=primary_resources")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new(uri.request_uri)
  request.add_field("X-TrackerToken", "*****************")
  response = http.request(request)
  @data = response.body
  parsed = JSON.parse(@data)
  puts parsed['primary_resources']['story_type']['feature']
  # parsed.each do |entry|
    # puts entry['primary_resources']['kind']['story']
  # end

  # puts parsed['primary_resources'].select { |r| r['story_type'].eql? 'feature' }
end

scope(project_id, date1, date2)

以下是一些JSON响应,没有解析(完整响应太长,而且对于其他15个用户故事的响应真的相同):

What is the id of the Project you want to get data from?
> 961142
What is the beginning date you want to return from? ex: 2014-07-02
> 2014-07-02
What is the end date you want to return from? ex: 2014-07-16
> 2014-07-03
[
   {
    "primary_resources": [
      {
        "kind": "story",
        "id": 74313670,
        "name": "User can make an image fit inside the grid when viewing image detail and save it to case template. BUILD 146",
        "story_type": "bug",
        "url": "https://www.pivotaltracker.com/story/show/74313670"
      }
    ],
    "guid": "961142_3419",
    "project_version": 3419
  },

以下是解析后的一些JSON响应(仅出于与上述相同的原因显示第一个故事):

What is the id of the Project you want to get data from?
> 961142
What is the beginning date you want to return from? ex: 2014-07-02
> 2014-07-02
What is the end date you want to return from? ex: 2014-07-16
> 2014-07-03
{"primary_resources"=>[{"kind"=>"story", "id"=>74313670, "name"=>"User can make an image fit inside the grid when viewing image detail and save it to case template. BUILD 146", "story_type"=>"bug", "url"=>"https://www.pivotaltracker.com/story/show/74313670"}], "guid"=>"961142_3419", "project_version"=>3419}

如何遍历此哈希数组并仅返回“story_type”=>“feature”?

1 个答案:

答案 0 :(得分:0)

因此,根据the API documentation,您的代码接收的是一系列活动(或“活动类型资源”),每个活动都可能包含其影响的资源的子数组(在“primary_resources”中)

我假设您想要的程序输出是活动的数组,它与“特征”类型的故事相关,而不是受影响的故事本身的数组。如果这是真的,试试这个:

feature_story_activities = parsed.select do |activity|
  activity.has_key?('primary_resources') &&
    activity['primary_resources'].find do |resource|
    resource['story_type'].eql?('feature')
  end
end

puts feature_story_activities

这会将feature_story_activities所有活动放在

  • 指定它们影响的资源列表,并

  • 影响至少一个特征类型的故事。