devfromcli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/devfromcli +9 -0
  3. data/lib/devfromcli.rb +130 -0
  4. metadata +52 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 814ec5f9eed63578a4417c50e3a19cb8d768fa833c8712c44c5ce4ba4deaa5f4
4
+ data.tar.gz: 8fda073d658676a5a2e03b9d5a4ab5cef5a2956336246f0c07da92a099770c45
5
+ SHA512:
6
+ metadata.gz: 14dbaf2b035a6a49b8ea45b9b51581a33cb4dfc227c84537816b036e78bdea5a3fa7a9d41bc4b5d0df75e402d3bef58024af015ef9d29e65941464de2b1f2c00
7
+ data.tar.gz: 6dfcbb5aefbe5c724bef5f8830f5e3654d253dddc5576f7374657104bf55be3f0ee81d0971db69f200d0689518564d2703f8283ce5c828c6bbabfe14dd008174
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'devfromcli'
4
+
5
+ gem_instance = DevFromCli.new
6
+
7
+ gem_instance.introduction if ARGV.first == 'start'
8
+ gem_instance.introduction if ARGV.length == 0
9
+ gem_instance.help if ARGV.first == 'help'
@@ -0,0 +1,130 @@
1
+ require 'dotenv'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+ Dotenv.load
6
+
7
+ ##
8
+ # Extends the +String+ class by adding utility methods
9
+ # for string coloring
10
+ class String
11
+ def yellow
12
+ "\e[33m#{self}\e[0m"
13
+ end
14
+
15
+ def blue
16
+ "\e[34m#{self}\e[0m"
17
+ end
18
+ end
19
+
20
+ ##
21
+ # This is the main class
22
+ class DevFromCli
23
+ ##
24
+ # This method outputs a welcome message and prompts
25
+ # for inputs depending on what they want to achieve
26
+ # for using the program
27
+ #
28
+ # @return oh_my_dev
29
+ def introduction
30
+ puts 'Welcome to DEV-from-CLI ๐ŸŽ‰๐ŸŽ‰'
31
+ puts ''
32
+ puts <<~ABOUT_OH_MY_DEV
33
+ DEV-from-CLI is a command line tool that allows you interact with Dev.to platform
34
+ ABOUT_OH_MY_DEV
35
+
36
+ puts "\n[1] View the latest articles on DEV.to\n[2] View articles using tags\n[3] Create article draft\n[4] Exist"
37
+
38
+ print "\n> "
39
+ user_initial_input = STDIN.gets.chomp
40
+ response_output if user_initial_input == '1'
41
+ create_post_draft if user_initial_input == '3'
42
+ puts "\nWow! did you make a mistake? Anyways, see you soon ๐Ÿ˜ช" if user_initial_input == '4'
43
+
44
+ return unless user_initial_input == '2'
45
+
46
+ print "\nEnter special tag> "
47
+ user_tag = STDIN.gets.chomp
48
+ response_output user_tag if user_tag
49
+ end
50
+
51
+ def help
52
+ puts ''
53
+ puts <<~HELP_INFO
54
+ devfromcli [version 0.1.0]
55
+
56
+ Usage: devfromcli <command>
57
+
58
+ Where <command> is either start or help
59
+
60
+ devfromcli start opens the interactive prompt
61
+ devfromcli help displays this help
62
+
63
+ If you have any issue, discover a bug please open a ticket on github:
64
+ https://github.com/OPARA-PROSPER/DEV-from-CLI/issues/new
65
+ HELP_INFO
66
+ end
67
+
68
+ private
69
+ ##
70
+ # Sends http GET request to the Dev.to backend API
71
+ def fetch_articles(tag = '')
72
+ uri = URI.parse("https://dev.to/api/articles?tag=#{tag}")
73
+ response = Net::HTTP.get_response(uri)
74
+ JSON.parse(response.body)
75
+ end
76
+
77
+ ##
78
+ # Output's response from Dev.to to users based
79
+ # on the choice from the welcome prompt.
80
+ # @return String
81
+ def response_output(tag = '')
82
+ articles = fetch_articles tag
83
+ articles.each do |item|
84
+ puts "\n******************************".blue
85
+ puts " #{'Title:'.yellow} #{item['title']}"
86
+ puts " #{'Author:'.yellow} #{item['user']['name']} | #{'Created:'.yellow} #{item['readable_publish_date']} | #{'Tags:'.yellow} #{item['tags']}"
87
+ puts " #{'Url:'.yellow} #{item['url']}"
88
+ puts " #{'Description:'.yellow} #{item['description']}"
89
+ puts '*******************************'.blue
90
+ print "\nView more articles? [y/n] "
91
+ input = STDIN.gets.chomp
92
+ possible_input = %w[Y y]
93
+ next if possible_input.include? input
94
+
95
+ puts "\nSee you next time ๐Ÿ˜Ž๐Ÿ˜Ž"
96
+ break
97
+ end
98
+ end
99
+
100
+ ##
101
+ # Creates article draft on Dev.to by making
102
+ # a http POST request to the articles endpoint
103
+ # @return String
104
+ def create_post_draft
105
+ print "\nFollow the prompt to create an article\nEnter your Dev.to API key> "
106
+ api_key=STDIN.gets.chomp
107
+ print 'Enter article Title> '
108
+ title = STDIN.gets.chomp
109
+ print 'Enter tags> '
110
+ tag1 = STDIN.gets.chomp
111
+ print 'Enter draft content> '
112
+ content = STDIN.gets.chomp
113
+ body = { article: { title: title, published: false, body_markdown: content, tags: [tag1] } }
114
+
115
+ uri = URI.parse('https://dev.to/api/articles')
116
+ http = Net::HTTP.new(uri.host, uri.port)
117
+ request = Net::HTTP::Post.new(uri.request_uri)
118
+ request['Content-Type'] = 'application/json'
119
+ request['api-key'] = api_key
120
+ http.use_ssl = (uri.scheme == 'https')
121
+ request.body = body.to_json
122
+ puts "\nCreating your article draft โšกโšก\n"
123
+
124
+ response = http.request(request)
125
+ puts "\nThere was an error: #{response}" if response.msg != 'Created'
126
+ puts 'Your article was successfully created'
127
+ response_json = JSON.parse(response.body)
128
+ puts "URL: #{response_json['url']}"
129
+ end
130
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devfromcli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Opara Prosper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ devfromcli allows users to fetch latest articles on the dev.to platform (users
15
+ articles and latest articles from other authors, fetch articles based on their
16
+ interest using tags, and quickly put together an article draft for latter)
17
+ email:
18
+ - oparaprosper79@gmail.com
19
+ executables:
20
+ - devfromcli
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - bin/devfromcli
25
+ - lib/devfromcli.rb
26
+ homepage: https://github.com/OPARA-PROSPER/DEV-from-CLI
27
+ licenses:
28
+ - MIT
29
+ metadata:
30
+ homepage_url: https://github.com/OPARA-PROSPER/DEV-from-CLI
31
+ source_code_url: https://github.com/OPARA-PROSPER/DEV-from-CLI
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.1.2
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: devfromcli is a tool that allows users to interact with the Dev.to platfrom
51
+ the CLI
52
+ test_files: []