Class: Tinylinks::CLI
- Inherits:
-
Thor
- Object
- Thor
- Tinylinks::CLI
- Defined in:
- lib/tinylinks/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add(url) ⇒ Object
- #edit(id) ⇒ Object
- #list ⇒ Object
- #login ⇒ Object
- #logout ⇒ Object
- #search(query) ⇒ Object
- #show(id) ⇒ Object
- #tags ⇒ Object
- #untagged ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
7 8 9 |
# File 'lib/tinylinks/cli.rb', line 7 def self.exit_on_failure? true end |
Instance Method Details
#add(url) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/tinylinks/cli.rb', line 86 def add(url) body = {url: url} body[:title] = [:title] if [:title] body[:description] = [:description] if [:description] body[:tags] = [:tags].split(",").map(&:strip) if [:tags] data = client.post("/links", body) say formatter.link(data["link"]) end |
#edit(id) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/tinylinks/cli.rb', line 99 def edit(id) body = {} body[:title] = [:title] if [:title] body[:description] = [:description] if [:description] body[:tags] = [:tags].split(",").map(&:strip) if [:tags] if body.empty? say_error error_colorizer.red("No changes specified. Use --title, --description, or --tags.") exit 1 end data = client.patch("/links/#{id}", body) say formatter.link(data["link"]) end |
#list ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/tinylinks/cli.rb', line 66 def list params = {} params[:tags] = [:tags] if [:tags] params[:filter] = [:filter] if [:filter] params[:sort] = [:sort] if [:sort] params[:page] = [:page] if [:page] data = client.get("/links", params) say formatter.link_list(data) end |
#login ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tinylinks/cli.rb', line 32 def login auth = Auth.new if auth.logged_in? say "Already logged in." return end say "Starting device authorization..." auth.login do |event, url| if event == :open_browser say "Opening browser for authorization..." say "If it doesn't open, visit: #{url}" system("open", url) end end say "Login successful!" rescue RuntimeError => e say_error error_colorizer.red("Login failed: #{e.}") exit 1 end |
#logout ⇒ Object
54 55 56 57 |
# File 'lib/tinylinks/cli.rb', line 54 def logout Auth.new.logout say "Logged out." end |
#search(query) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/tinylinks/cli.rb', line 116 def search(query) params = {q: query} params[:page] = [:page] if [:page] data = client.get("/search", params) say formatter.link_list(data) end |
#show(id) ⇒ Object
77 78 79 80 |
# File 'lib/tinylinks/cli.rb', line 77 def show(id) data = client.get("/links/#{id}") say formatter.link(data["link"]) end |
#tags ⇒ Object
125 126 127 128 129 130 |
# File 'lib/tinylinks/cli.rb', line 125 def params = {} params[:sort_by] = [:sort] if [:sort] data = client.get("/tags", params) say formatter.(data) end |
#untagged ⇒ Object
134 135 136 137 138 139 |
# File 'lib/tinylinks/cli.rb', line 134 def untagged params = {} params[:page] = [:page] if [:page] data = client.get("/untagged", params) say formatter.link_list(data) end |
#version ⇒ Object
142 143 144 |
# File 'lib/tinylinks/cli.rb', line 142 def version say "tinylinks #{VERSION}" end |