【发布时间】:2018-06-01 02:05:06
【问题描述】:
我想将 ruby 变量替换为转义的 shell 字符串。
这是我开始的,它有效:
<<~`SHELL`
curl -s "#{CANVAS_URI}/api/v1/courses/#{COURSE_ID}/quizzes/#{quiz_id}/questions" \
-X POST \
-H "Content-Type: application/json" \
-d '{ "question": { "question_type": "multiple_choice_question", "question_text": "<p>Is everything clear?</p>", "points_possible": 1, "answers": [ { "text": "I read and understood", "html": "", "weight": 100 }, { "text": "I have questions", "comments_html": "<p>Please post your questions to a discussion (in course navigation).</p>", "weight": 0 } ] } }' \
-H "Authorization: Bearer #{CANVAS_TOKEN}"
SHELL
但是,我希望 discussion(在 -d JSON 中)成为链接。并且无法让它工作:
myJson = %Q|{ "question": { "question_type": "multiple_choice_question", "question_text": "<p>Is everything clear?</p>", "points_possible": 1, "answers": [ { "text": "I read and understood", "html": "", "weight": 100 }, { "text": "I have questions", "comments_html": "<p>Please post your questions to a <a href="#{CANVAS_URI}/courses/#{COURSE_ID}/discussion_topics">discussion</a>.</p>", "weight": 0 } ] } }|
<<~`SHELL`
curl -s "#{CANVAS_URI}/api/v1/courses/#{COURSE_ID}/quizzes/#{quiz_id}/questions" \
-X POST \
-H "Content-Type: application/json" \
-d '#{myJson}' \
-H "Authorization: Bearer #{CANVAS_TOKEN}"
SHELL
<<~`SHELL`
curl -s "#{CANVAS_URI}/api/v1/courses/#{COURSE_ID}/quizzes/#{quiz_id}/questions" \
-X POST \
-H "Content-Type: application/json" \
-d "#{myJson}" \
-H "Authorization: Bearer #{CANVAS_TOKEN}"
SHELL
我被困在两个 langs 的逃脱之间。
整个事情是在 ruby 中发布嵌套 json 的解决方法。 Flat jsons 适用于大多数 http gem,但是很少需要嵌套 json,恐怕大多数 gem 都没有针对它进行测试(我记得尝试过 http.rb 这样做,但即使是 json 数组也是non trivial there .
【问题讨论】: