Created
February 28, 2021 03:53
-
-
Save vonthecreator/c3924c95c2cd4dd7a49c674316cdc33a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# script for posting on dev.to | |
# using api-key | |
require 'net/http' | |
require 'json' | |
def read_file(fname) | |
unless File.file?("#{fname}") | |
return nil | |
else | |
if !File.extname("#{fname}").eql? ".md" | |
return nil | |
else | |
f = File.open("#{fname}") | |
file_data = f.read | |
f.close | |
return file_data | |
end | |
end | |
rescue => e | |
puts "Error #{e}" | |
end | |
def create_post(fname, key) | |
if read_file(fname).eql? nil | |
puts "read error #{fname}" | |
else | |
post_body = read_file(fname) | |
uri = URI('https://dev.to/api/articles') | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json', 'api-key' => key) | |
req.body = {article: {body_markdown: post_body}}.to_json | |
res = http.request(req) | |
puts "res #{res.body}" | |
end | |
rescue => e | |
puts "failes #{e}" | |
end | |
# create_post( <filename>, <api_key> ) | |
create_post(ARGV[0], ARGV[1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment