Class: ImgurUp::ImgurUp

Inherits:
Object
  • Object
show all
Defined in:
lib/imgur_up/imgur_up.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ ImgurUp

Returns a new instance of ImgurUp.



7
8
9
# File 'lib/imgur_up/imgur_up.rb', line 7

def initialize(config_path)
  @config_path = File.expand_path(config_path)
end

Instance Method Details

#needs_configuration?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/imgur_up/imgur_up.rb', line 11

def needs_configuration?
  %w(client_id client_secret refresh_token album).any? do |key|
    config[key].nil?
  end
end

#prompt_for_configurationObject



17
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
# File 'lib/imgur_up/imgur_up.rb', line 17

def prompt_for_configuration
  config["client_id"]     = prompt("Enter your client ID", config["client_id"])
  config["client_secret"] = prompt("Enter your client secret", config["client_secret"])

  self.imgur = Imgur.new(config["client_id"], config["client_secret"])

  puts "Log on to the following URL to obtain a PIN."
  puts imgur.pin_request_url
  pin = prompt("Enter PIN")
  authorization = imgur.authorize(pin)
  config["refresh_token"] = authorization["refresh_token"]

  puts "Your albums:"
  albums = imgur.albums
  albums.each_with_index do |album, index|
    puts "#{index+1}: #{album["title"]}"
  end

  default_index = albums.find_index { |album| album["id"] == config["album"] }
  album_index =
    if default_index.nil?
      prompt("Select the album to upload new files to").to_i - 1
    else
      prompt("Select the album to upload new files to", default_index + 1).to_i - 1
    end
  config["album"] = albums[album_index]["id"]

  save_config
end

#watch(directories) ⇒ Object



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
# File 'lib/imgur_up/imgur_up.rb', line 47

def watch(directories)
  directories = directories.map { |dir| File.expand_path(dir) }
  directories.each do |dir|
    ::ImgurUp.logger.info "Listening to #{dir}"
  end

  listener = Listen.to(directories, only: /\.(?:jpg|png|gif)$/i) do |_, added, _|
    threads = added.map do |path|
      ::ImgurUp.logger.info "File added: #{path}"

      Thread.new(path) do |path|
        response = imgur.upload(path, config["album"])
        ::ImgurUp.logger.info "Link for #{path}: #{response["link"]}"

        response["link"]
      end
    end

    threads.each do |thread|
      link = thread.value

      Clipboard.copy(link)
      TerminalNotifier.notify("File uploaded, link copied.")
    end
  end
  listener.start
end