解析ruby中的httparty get响应

时间:2015-02-12 22:45:47

标签: ruby json api parsing

我无法弄清楚如何解析此响应。我只试图从中提取一个计数。

更新: 我该如何解析这个XML响应?

这是我的API连接信息

我使用Ruby irb,使用HTTParty

require 'httparty'

api_key = "6e569b3d-3457-9485-edb2-eccd27272dbf"
secret_key = "MDVkYTQzMDgtOWRiOC04NDULTRkYWUtODQwOTc5ZDFkZTQ3NTZhOG1NWQtNmMzMi0xYjE0LWYMzgtNDc0YzU0MWYxYmUx"

def urlncode(string)
  URI.escape(string, Regexp.new("[Generating an API Signature^#{URI::PATTERN::UNRESERVED}]"))
end

salt = rand(10000000000).to_s

hash = OpenSSL::HMAC.digest('sha256', secret_key, salt)

signature = urlncode(Base64.encode64(hash))

# controller / action
api_path = "/Tickets/TicketCount"

url = "http://support.myorganization.org/api/index.php?e=#{api_path}"

response = HTTParty.get("#{url}&apikey=#{api_key}&salt=#{salt}&signature=#{signature}").parsed_response

我的回答:(截断)

response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ticketcount>\n<departments>\n<department id=\"0\">\n<totalitems><![CDATA[11]]></totalitems>\n<lastactivity><![CDATA[1423657406]]></lastactivity>\n<totalunresolveditems><![CDATA[11]]></totalunresolveditems>\n<ticketstatus id=\"1\" lastactivity=\"1423657406\" totalitems=\"6\" />\n<ticketstatus id=\"2\" lastactivity=\"1422566730\" totalitems=\"5\" />\n<tickettype id=\"1\" lastactivity=\"1423657406\" totalitems=\"11\" totalunresolveditems=\"11\" />\n<ownerstaff id=\"0\" lastactivity=\"1423657406\" totalitems=\"7\" totalunresolveditems=\"7\" />\n<ownerstaff id=\"4\" lastactivity=\"1420825202\" totalitems=\"2\" totalunresolveditems=\"2\" />\n<ownerstaff id=\"17\" lastactivity=\"1422452400\" totalitems=\"1\" totalunresolveditems=\"1\" />\n<ownerstaff id=\"26\" lastactivity=\"1422566730\" totalitems=\"1\"

当我尝试做的时候:

response['ticket count']

我明白了:(显然不是所有数据都包括在内)

irb(main):029:0> data['ticketcount']
=> "ticketcount"

0 个答案:

没有答案