Skip to content

Instantly share code, notes, and snippets.

@ZephiroRB
Created February 19, 2018 16:44
Show Gist options
  • Save ZephiroRB/fdfe875f6ee5a31f444c82bc7cffe314 to your computer and use it in GitHub Desktop.
Save ZephiroRB/fdfe875f6ee5a31f444c82bc7cffe314 to your computer and use it in GitHub Desktop.
Manipulate file json with Rails
def get_user_json(utoken)
File.read("#{path_user_json}/#{utoken}.json")
end
def delete_user_json(utoken)
FileUtils.rm("#{path_user_json}/#{utoken}.json")
end
def empty_user_json(utoken)
begin
FileUtils.touch("#{path_user_json}/#{utoken}.json")
rescue => e
create_user_json(utoken, {})
end
end
def create_user_json(utoken, data)
File.open("#{path_user_json}/#{utoken}.json","w+") do |f|
f.write(data.to_json)
end
end
def path_user_json
Rails.root.join('public','jsons')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment