class DevFromCli
This is the main class
Public Instance Methods
help()
click to toggle source
# File lib/devfromcli.rb, line 51 def help puts '' puts <<~HELP_INFO devfromcli [version 0.1.0] Usage: devfromcli <command> Where <command> is either start or help devfromcli start opens the interactive prompt devfromcli help displays this help If you have any issue, discover a bug please open a ticket on github: https://github.com/OPARA-PROSPER/DEV-from-CLI/issues/new HELP_INFO end
introduction()
click to toggle source
This method outputs a welcome message and prompts for inputs depending on what they want to achieve for using the program
@return oh_my_dev
# File lib/devfromcli.rb, line 29 def introduction puts 'Welcome to DEV-from-CLI ๐๐' puts '' puts <<~ABOUT_OH_MY_DEV DEV-from-CLI is a command line tool that allows you interact with Dev.to platform ABOUT_OH_MY_DEV puts "\n[1] View the latest articles on DEV.to\n[2] View articles using tags\n[3] Create article draft\n[4] Exist" print "\n> " user_initial_input = STDIN.gets.chomp response_output if user_initial_input == '1' create_post_draft if user_initial_input == '3' puts "\nWow! did you make a mistake? Anyways, see you soon ๐ช" if user_initial_input == '4' return unless user_initial_input == '2' print "\nEnter special tag> " user_tag = STDIN.gets.chomp response_output user_tag if user_tag end
Private Instance Methods
create_post_draft()
click to toggle source
Creates article draft on Dev.to by making a http POST request to the articles endpoint @return String
# File lib/devfromcli.rb, line 104 def create_post_draft print "\nFollow the prompt to create an article\nEnter your Dev.to API key> " api_key=STDIN.gets.chomp print 'Enter article Title> ' title = STDIN.gets.chomp print 'Enter tags> ' tag1 = STDIN.gets.chomp print 'Enter draft content> ' content = STDIN.gets.chomp body = { article: { title: title, published: false, body_markdown: content, tags: [tag1] } } uri = URI.parse('https://dev.to/api/articles') http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri) request['Content-Type'] = 'application/json' request['api-key'] = api_key http.use_ssl = (uri.scheme == 'https') request.body = body.to_json puts "\nCreating your article draft โกโก\n" response = http.request(request) puts "\nThere was an error: #{response}" if response.msg != 'Created' puts 'Your article was successfully created' response_json = JSON.parse(response.body) puts "URL: #{response_json['url']}" end
fetch_articles(tag = '')
click to toggle source
Sends http GET request to the Dev.to backend API
# File lib/devfromcli.rb, line 71 def fetch_articles(tag = '') uri = URI.parse("https://dev.to/api/articles?tag=#{tag}") response = Net::HTTP.get_response(uri) JSON.parse(response.body) end
response_output(tag = '')
click to toggle source
Output's response from Dev.to to users based on the choice from the welcome prompt. @return String
# File lib/devfromcli.rb, line 81 def response_output(tag = '') articles = fetch_articles tag articles.each do |item| puts "\n******************************".blue puts " #{'Title:'.yellow} #{item['title']}" puts " #{'Author:'.yellow} #{item['user']['name']} | #{'Created:'.yellow} #{item['readable_publish_date']} | #{'Tags:'.yellow} #{item['tags']}" puts " #{'Url:'.yellow} #{item['url']}" puts " #{'Description:'.yellow} #{item['description']}" puts '*******************************'.blue print "\nView more articles? [y/n] " input = STDIN.gets.chomp possible_input = %w[Y y] next if possible_input.include? input puts "\nSee you next time ๐๐" break end end