我有一个名为import.rb
的脚本,它将json内容从url导入草稿目录。
require 'fileutils'
require 'json'
# Load JSON data from source
# (Assuming the data source is a json file on your file system)
data = JSON.parse('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec')
# Proceed to create post files if the value array is not empty
array = data["user"]
if array && !array.empty?
# create the `_drafts` directory if it doesn't exist already
drafts_dir = File.expand_path('./_drafts', __dir__)
FileUtils.mkdir_p(posts_dir) unless Dir.exist?(drafts_dir)
# iterate through the array and generate draft-files for each entry
# where entry.first will be the "content" and entry.last the "title"
array.each do |entry|
File.open(File.join(drafts_dir, entry.last), 'wb') do |draft|
draft.puts("---\n---\n\n#{entry.first}"
end
end
end
运行ruby _scripts/import.rb
时出现_scripts/import.rb:20: syntax error, unexpected keyword_end, expecting ')'
end
错误。我将行从推荐形式更改为data = JSON.parse('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec')
。原始为data = JSON.parse(File.read(your_json_source.json))
。请帮助我。
答案 0 :(得分:1)
问题是19行末尾缺少)
:
...
array.each do |entry|
File.open(File.join(drafts_dir, entry.last), 'wb') do |draft|
draft.puts("---\n---\n\n#{entry.first}")
end
end
它应该解决该问题:)