1
1
require_relative '../base_command'
2
+ require_relative '../../constants'
3
+ require 'uri'
4
+ require 'date'
2
5
3
6
module Readwise
4
7
class CLI
5
8
module Document
6
9
class CreateCommand < BaseCommand
7
- CATEGORIES = %w[ article email rss highlight note pdf epub tweet video ] . freeze
8
10
def banner
9
11
"Usage: readwise document create [options]"
10
12
end
@@ -27,6 +29,10 @@ def add_options(opts)
27
29
end
28
30
29
31
opts . on ( "-u" , "--url=URL" , "Source URL (defaults to https://example.com/<filename>)" ) do |url |
32
+ unless valid_url? ( url )
33
+ puts "Error: Invalid URL format. Please provide a valid URL."
34
+ exit 1
35
+ end
30
36
options [ :url ] = url
31
37
end
32
38
@@ -38,17 +44,17 @@ def add_options(opts)
38
44
options [ :notes ] = notes
39
45
end
40
46
41
- opts . on ( "--location=LOCATION" , "Document location: new, later, archive, feed (default: new)" ) do |location |
42
- unless %w[ new later archive feed ] . include? ( location )
43
- puts "Error: Invalid location. Must be one of: new, later, archive, feed "
47
+ opts . on ( "--location=LOCATION" , "Document location: #{ Readwise :: Constants :: DOCUMENT_LOCATIONS . join ( ', ' ) } (default: new)" ) do |location |
48
+ unless Readwise :: Constants :: DOCUMENT_LOCATIONS . include? ( location )
49
+ puts "Error: Invalid location. Must be one of: #{ Readwise :: Constants :: DOCUMENT_LOCATIONS . join ( ', ' ) } "
44
50
exit 1
45
51
end
46
52
options [ :location ] = location
47
53
end
48
54
49
- opts . on ( "--category=CATEGORY" , "Document category: #{ CATEGORIES . join ( ', ' ) } " ) do |category |
50
- unless CATEGORIES . include? ( category )
51
- puts "Error: Invalid category. Must be one of: #{ CATEGORIES . join ( ', ' ) } "
55
+ opts . on ( "--category=CATEGORY" , "Document category: #{ Readwise :: Constants :: DOCUMENT_CATEGORIES . join ( ', ' ) } " ) do |category |
56
+ unless Readwise :: Constants :: DOCUMENT_CATEGORIES . include? ( category )
57
+ puts "Error: Invalid category. Must be one of: #{ Readwise :: Constants :: DOCUMENT_CATEGORIES . join ( ', ' ) } "
52
58
exit 1
53
59
end
54
60
options [ :category ] = category
@@ -63,6 +69,10 @@ def add_options(opts)
63
69
end
64
70
65
71
opts . on ( "--published-date=DATE" , "Published date (ISO 8601 format)" ) do |date |
72
+ unless valid_iso8601_date? ( date )
73
+ puts "Error: Invalid date format. Please provide a valid ISO 8601 date (e.g., 2023-12-25T10:30:00Z)."
74
+ exit 1
75
+ end
66
76
options [ :published_date ] = date
67
77
end
68
78
@@ -104,6 +114,20 @@ def run(args)
104
114
105
115
private
106
116
117
+ def valid_url? ( url )
118
+ uri = URI . parse ( url )
119
+ uri . is_a? ( URI ::HTTP ) || uri . is_a? ( URI ::HTTPS )
120
+ rescue URI ::InvalidURIError
121
+ false
122
+ end
123
+
124
+ def valid_iso8601_date? ( date )
125
+ DateTime . iso8601 ( date )
126
+ true
127
+ rescue Date ::Error
128
+ false
129
+ end
130
+
107
131
def build_document_params ( html_content , html_file )
108
132
document_params = {
109
133
html : html_content ,
0 commit comments