Class: Readwise::CLI::Document::CreateCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/readwise/cli/document/create_command.rb

Instance Method Summary collapse

Methods inherited from BaseCommand

#execute, #initialize

Constructor Details

This class inherits a constructor from Readwise::CLI::BaseCommand

Instance Method Details

#add_options(opts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/readwise/cli/document/create_command.rb', line 18

def add_options(opts)
  opts.on("-f", "--html-file=FILE", "HTML file path") do |file|
    options[:file] = file
  end

  opts.on("--title=TITLE", "Document title") do |title|
    options[:title] = title
  end

  opts.on("--author=AUTHOR", "Document author") do |author|
    options[:author] = author
  end

  opts.on("-u", "--url=URL", "Source URL (defaults to https://example.com/<filename>)") do |url|
    unless valid_url?(url)
      puts "Error: Invalid URL format. Please provide a valid URL."
      exit 1
    end
    options[:url] = url
  end

  opts.on("--summary=SUMMARY", "Document summary") do |summary|
    options[:summary] = summary
  end

  opts.on("--notes=NOTES", "Personal notes") do |notes|
    options[:notes] = notes
  end

  opts.on("--location=LOCATION", "Document location: #{Readwise::Constants::DOCUMENT_LOCATIONS.join(', ')} (default: new)") do |location|
    unless Readwise::Constants::DOCUMENT_LOCATIONS.include?(location)
      puts "Error: Invalid location. Must be one of: #{Readwise::Constants::DOCUMENT_LOCATIONS.join(', ')}"
      exit 1
    end
    options[:location] = location
  end

  opts.on("--category=CATEGORY", "Document category: #{Readwise::Constants::DOCUMENT_CATEGORIES.join(', ')}") do |category|
    unless Readwise::Constants::DOCUMENT_CATEGORIES.include?(category)
      puts "Error: Invalid category. Must be one of: #{Readwise::Constants::DOCUMENT_CATEGORIES.join(', ')}"
      exit 1
    end
    options[:category] = category
  end

  opts.on("--tags=TAGS", "Comma-separated list of tags") do |tags|
    options[:tags] = tags.split(',').map(&:strip)
  end

  opts.on("--image-url=URL", "Image URL") do |image_url|
    options[:image_url] = image_url
  end

  opts.on("--published-date=DATE", "Published date (ISO 8601 format)") do |date|
    unless valid_iso8601_date?(date)
      puts "Error: Invalid date format. Please provide a valid ISO 8601 date (e.g., 2023-12-25T10:30:00Z)."
      exit 1
    end
    options[:published_date] = date
  end

  opts.on("--[no-]clean-html", "Clean HTML (default: true)") do |clean|
    options[:should_clean_html] = clean
  end

  opts.on("--saved-using=SOURCE", "Saved using source (default: cli)") do |source|
    options[:saved_using] = source
  end
end


10
11
12
# File 'lib/readwise/cli/document/create_command.rb', line 10

def banner
  "Usage: readwise document create [options]"
end

#descriptionObject



14
15
16
# File 'lib/readwise/cli/document/create_command.rb', line 14

def description
  "Sends HTML content to Readwise Reader API"
end

#run(args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/readwise/cli/document/create_command.rb', line 96

def run(args)
  html_file = options[:file]
  html_content = read_file(html_file) if html_file

  document_params = build_document_params(html_content, html_file)

  handle_api_error do
    client = get_api_client
    document_create = Readwise::DocumentCreate.new(**document_params)
    document = client.create_document(document: document_create)

    puts "Document created successfully!"
    puts "ID: #{document.id}"
    puts "Title: #{document.title}"
    puts "Location: #{document.location}"
    puts "URL: #{document.url}" if document.url
  end
end

#validate_arguments(args) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/readwise/cli/document/create_command.rb', line 88

def validate_arguments(args)
  unless options[:file] || options[:url]
    puts "Error: File path or URL is required"
    show_help
    exit 1
  end
end