如何将awesome_print用于发出HTTP请求的Ruby文件

时间:2016-01-05 03:04:02

标签: ruby

这是我的代码:

require 'net/https'

uri = URI('https://api.clever.com/v1.1/sections')

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

request = Net::HTTP::Get.new(uri.request_uri)
request.add_field 'Authorization', 'Bearer DEMO_TOKEN'

response = http.request(request)
puts response.body

问题是我的代码输出很粗,很难在终端读取。我试图用很棒的印刷品来清理它,但它不起作用......这就是我正在尝试的:

require 'net/https'
require 'awesome_print'

uri = URI('https://api.clever.com/v1.1/sections')

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

request = Net::HTTP::Get.new(uri.request_uri)
request.add_field 'Authorization', 'Bearer DEMO_TOKEN'

response = http.request(request)
ap response.body

但它并没有按照我需要的方式进行格式化。知道发生了什么吗?

3 个答案:

答案 0 :(得分:2)

问题是您需要打印Hash,而不是原始String。因此,使用JSON.parse(response.body)可以解决您的问题。

或者,使用ppjson,它们都来自stdlib。

require 'net/https'
require 'pp'
require 'json'

uri = URI('https://api.clever.com/v1.1/sections')
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
  request = Net::HTTP::Get.new(uri)
  request["authorization"] = "Bearer DEMO_TOKEN"
  http.request(request) do |response|
    pp JSON.parse(response.body)
  end
end

但最终,我建议使用pry进行调试。它只会使生活更容易10倍于debuging。

gem install pry

然后将上面的代码更改为:

require 'net/https'
require 'pry'
require 'json'

uri = URI('https://api.clever.com/v1.1/sections')
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
  request = Net::HTTP::Get.new(uri)
  request["authorization"] = "Bearer DEMO_TOKEN"
  http.request(request) do |response|
    res = JSON.parse(response.body)
    binding.pry
  end
end

在您的终端中运行该文件后,它会暂停您放置binding.pry的位置。然后输入res,您将看到格式良好的哈希值。

pry玩得开心!

答案 1 :(得分:1)

awesome print以易于阅读的格式打印您的Ruby数据结构(Hash,Array等)。不适用于HTML!

如果您想以易于阅读的方式格式化HTML,请查看Nokogiri。例如:

require 'nokogiri'

# your response html
html = response.body

doc = Nokogiri::XML(html,&:noblanks)
puts doc.to_xhtml(indent:4)

答案 2 :(得分:1)

将您的response.body解析为JSON并使用内置于更高版本JSON中的pretty_generate()函数。

require 'net/https'
require 'json'

uri = URI('https://api.clever.com/v1.1/sections')

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

request = Net::HTTP::Get.new(uri.request_uri)
request.add_field 'Authorization', 'Bearer DEMO_TOKEN'

response = http.request(request)
myjson = JSON.parse(response.body)
puts JSON.pretty_generate(myjson)

这将为您提供输出:

{
  "data": {
    "course_name": "Fine Arts, Class 703",
    "course_number": "703",
    "created": "2014-02-26T21:15:38.324Z",
    "district": "4fd43cc56d11340000000005",
    "grade": "7",
    "last_modified": "2015-09-30T21:08:09.877Z",
    "name": "Fine Arts, Class 703 - 703 - A. Ortiz (Section 3)",
    "period": "7",
    "school": "530e595026403103360ff9ff",
    "sis_id": "674",
    "students": [
      "530e5960049e75a9262cff59",
      "530e5960049e75a9262cff99",
      "530e5961049e75a9262cffd5",
      "530e5961049e75a9262d001c",
      "530e5961049e75a9262d008a",
      "530e5962049e75a9262d0144",
      "530e5962049e75a9262d0155",
      "530e5962049e75a9262d015e",
      "530e5963049e75a9262d0200",
      "530e5963049e75a9262d022d",
      "530e5963049e75a9262d023a",
      "530e5964049e75a9262d0275",
      "530e5964049e75a9262d029b",
      "530e5964049e75a9262d02c0",
      "530e5964049e75a9262d02de",
      "530e5965049e75a9262d034a",
      "530e5965049e75a9262d0354",
      "530e5965049e75a9262d03c7",
      "530e5966049e75a9262d0419",
      "530e5966049e75a9262d046d",
      "530e5966049e75a9262d0489",
      "530e5967049e75a9262d0560",
      "530e5967049e75a9262d05b4",
      "530e5967049e75a9262d05bb",
      "530e5968049e75a9262d0621",
      "530e5968049e75a9262d0637"
    ],
    "subject": "arts and music",
    "teacher": "530e5955d50c310f36112bec",
....
.... 
# I have not post full output but it's pretty good and well structured