Class: Tinylinks::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tinylinks/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (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] = options[:title] if options[:title]
  body[:description] = options[:description] if options[:description]
  body[:tags] = options[:tags].split(",").map(&:strip) if options[: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] = options[:title] if options[:title]
  body[:description] = options[:description] if options[:description]
  body[:tags] = options[:tags].split(",").map(&:strip) if options[: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

#listObject



66
67
68
69
70
71
72
73
74
# File 'lib/tinylinks/cli.rb', line 66

def list
  params = {}
  params[:tags] = options[:tags] if options[:tags]
  params[:filter] = options[:filter] if options[:filter]
  params[:sort] = options[:sort] if options[:sort]
  params[:page] = options[:page] if options[:page]
  data = client.get("/links", params)
  say formatter.link_list(data)
end

#loginObject



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 
  auth = Auth.new
  if auth.logged_in?
    say "Already logged in."
    return
  end

  say "Starting device authorization..."
  auth. 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.message}")
  exit 1
end

#logoutObject



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] = options[:page] if options[: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

#tagsObject



125
126
127
128
129
130
# File 'lib/tinylinks/cli.rb', line 125

def tags
  params = {}
  params[:sort_by] = options[:sort] if options[:sort]
  data = client.get("/tags", params)
  say formatter.tags(data)
end

#untaggedObject



134
135
136
137
138
139
# File 'lib/tinylinks/cli.rb', line 134

def untagged
  params = {}
  params[:page] = options[:page] if options[:page]
  data = client.get("/untagged", params)
  say formatter.link_list(data)
end

#versionObject



142
143
144
# File 'lib/tinylinks/cli.rb', line 142

def version
  say "tinylinks #{VERSION}"
end